SavindraSingh
SavindraSingh

Reputation: 961

Python - connect to network device through Terminal using Password

My goal is to connect to a network device through a proxy/terminal server. When I connect using Putty I enter:

username:ttyS1@terminalservername

where username is the user name the number after :ttyS is the port number to which we want to connect, and terminalservername is the name of the proxy server. I have tried the code I got from these two links Link1 and Link2

from netmiko import ConnectHandler
import time
from netmiko import redispatch

jumpserver = {'device_type': 'terminal_server','ip': 'x.x.x.x','username': 'name','password': 'pass','global_delay_factor':5}

net_connect = ConnectHandler(**jumpserver)
print net_connect.find_prompt()

net_connect.write_channel('command to access router')
time.sleep(1)
net_connect.read_channel()

redispatch(net_connect, device_type='arista_eos')
net_connect.send_command('show hostname')

When I run the script, it is already on the terminal server and unable to connect further to the network device.

Can someone please suggest how to connect to a network device through a proxy/terminal server as PUTTY does and get the hostname of the device.

Added sample screen shot of Putty, which I want simulate using Python

I got below code from one of my colleagues. I can now reach the device connected on a particular port but I am unable to login. When I enter the username at login prompt it is asking for Old password, then it asks for Password again. I do not understand why this is happening because I am able to login using same account and password successfully using Putty. Only through the Python script it asks for Old Password and then asking for Password indefinitely. Here is the code:

import time
from netmiko import ConnectHandler, redispatch

zpe_username = "serviceaccount"
zpe_password = "xxxxxxx"
zpe_hostname = "TerminalServerName"
console_username = zpe_username + ":ttyS" + "1"
console_server = {
    "host": zpe_hostname,
    "username": console_username,
    "password": zpe_password,
    "device_type": "terminal_server",
}
print("ssh " + console_username + "@" + zpe_hostname)

net_connect = ConnectHandler(**console_server)
net_connect.write_channel(zpe_username + "\n")
time.sleep(1)
password_prompt = net_connect.read_channel()
net_connect.write_channel(zpe_password + "\n")
time.sleep(1)

redispatch(net_connect, device_type='arista_eos')
device_type = net_connect.device_type
device_prompt = net_connect.base_prompt
print(device_type, device_prompt)

Upvotes: 2

Views: 3735

Answers (2)

SavindraSingh
SavindraSingh

Reputation: 961

Below thing worked for me after adding net_connect.enable():

import time
from netmiko import ConnectHandler, redispatch

zpe_username = "serviceaccount"
zpe_password = "xxxxxxx"
zpe_hostname = "TerminalServerName"
console_username = zpe_username + ":ttyS" + "1"
console_server = {
    "host": zpe_hostname,
    "username": console_username,
    "password": zpe_password,
    "device_type": "terminal_server",
}
print("ssh " + console_username + "@" + zpe_hostname)

net_connect = ConnectHandler(**console_server)
net_connect.enable()
net_connect.write_channel(zpe_username + "\n")
time.sleep(1)
password_prompt = net_connect.read_channel()
net_connect.write_channel(zpe_password + "\n")
time.sleep(1)

redispatch(net_connect, device_type='arista_eos')
device_type = net_connect.device_type
device_prompt = net_connect.base_prompt
print(device_type, device_prompt)

Upvotes: 2

Avneesh Srivastava
Avneesh Srivastava

Reputation: 103

Did you try the Paramiko library? I see support for HTTP Proxy through it. I used it a while ago while I was behind corporate proxy.

This is what I'd followed.

Upvotes: 2

Related Questions