2

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)
Conflate
  • 27
  • 6
SavindraSingh
  • 878
  • 13
  • 39

2 Answers2

2

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.

  • I will try that tomorrow. Not sure if http proxy will be helpful because we are not using http, we are just trying to connect to device through a specific port through terminal server. As given in the screen shot in the question. – SavindraSingh Mar 18 '19 at 11:30
  • I assume the title is a little misleading then, also as from your screenshot I assume you're trying to create a tunnel to the device? If so please take a look at [SSH Tunelling](https://stackoverflow.com/questions/8169739/how-to-create-a-ssh-tunnel-using-python-and-paramiko) – Avneesh Srivastava Mar 19 '19 at 08:23
  • @AvneeshSrivastava Link is dead –  Jan 29 '21 at 23:30
2

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)
SavindraSingh
  • 878
  • 13
  • 39