ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.59k stars 1.3k forks source link

How to correctly connect to console server port using terminal_server similar to putty #2130

Closed ghost closed 3 years ago

ghost commented 3 years ago

My setup is similar to the question described here: https://stackoverflow.com/questions/55136892/python-connect-to-network-device-through-terminal-using-password

But, unfortunately after many different attempts I'm still getting a ValueError(f"Unable to find prompt: {prompt}")

When I login to putty with my personal username and password, then run the python script, it enters the device username and password correctly and I get access to the device, but without logging in with my credentials to putty first I get the prompt not found. Not sure why.

Here's the code:

portNumber = 46
conserver_ip = '10.x.x.x'
conserver_username = 'myUsername' + ":port" + portNumber + '@' + conserver_ip
conserver_password = 'myPassword'

console_server = {
    'host': conserver_ip,
    'username': conserver_username,
    'password': conserver_password,
    'device_type': 'terminal_server',
    'port': '22',
        #'global_delay_factor': 10,
}

net_connect = ConnectHandler(**console_server)
net_connect.enable()

output = net_connect.find_prompt() # without this I get NetmikoTimeoutException: Timed-out reading channel, data not available.

new_device_username = 'deviceUsername'
new_device_password = 'devicePassword'

#send username:
net_connect.write_channel(new_device_username)
net_connect.write_channel('\n')
time.sleep(1)

output = net_connect.read_channel()

#send password:
net_connect.write_channel(new_device_password)
net_connect.write_channel('\n')
output = net_connect.read_channel()

#redispatch when logged in and use as normal:
redispatch(net_connect, device_type='cisco_ios')
output = net_connect.find_prompt()

net_connect.send_command("show running-config", use_textfsm=True)

net_connect.disconnect()

Heres the log:


DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:Received global request "hostkeys-00@openssh.com"
DEBUG:paramiko.transport:Rejecting "hostkeys-00@openssh.com" global request from server.
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:Pattern is: 
DEBUG:netmiko:_read_channel_expect read_data: 

DEBUG:netmiko:Pattern found:  

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 

DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: b'\n'
ERROR:NetworkDeviceConfigAutomation:Exception on /save_device_config/46 [GET]
Traceback (most recent call last):
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\env\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\env\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\env\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\env\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\env\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\env\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\NetworkDeviceConfigAutomation\views.py", line 96, in save_device_config
    print(net_connect.find_prompt())
  File "C:\Users\benyamin.jeizan\source\repos\NetworkDeviceConfigAutomation\env\lib\site-packages\netmiko\base_connection.py", line 1188, in find_prompt
    raise ValueError(f"Unable to find prompt: {prompt}")
ValueError: Unable to find prompt: 
INFO:werkzeug:127.0.0.1 - - [29/Jan/2021 15:33:14] "GET /save_device_config/46 HTTP/1.1" 500 -
ktbyers commented 3 years ago

I don't see anything at all from your terminal server? Looking at the log there is never a read_channel() where you actually get any data besides white space?

ghost commented 3 years ago

I don't see anything at all from your terminal server? Looking at the log there is never a read_channel() where you actually get any data besides white space?

Hi Kirk, Correct, the first connection is not getting any data from the console_server. I'm not quite sure what else I need to add after the ConnectHandler(**console_server) I've tried adding extra read and write_channel() statements at the top but it still can't find the prompt. I can see that it tries to make a connection, but it cannot read anything from the console_server.

Edit: I figured it out thanks to eoprede on https://github.com/ktbyers/netmiko/issues/1062

All I needed to add was a net_connect.write_channel("\r") after net_connect = ConnectHandler(**console_server) to get some kind of response back by entering a blank line in the terminal server.