question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Netmiko code to enter Juniper CLI is not robust enough

See original GitHub issue

Hello,

I am trying to make Netmiko work with my Juniper EX4200-24P device, but I am always getting:

  File "/tmp/test/venv/lib/python3.9/site-packages/paramiko/channel.py", line 699, in recv
    out = self.in_buffer.read(nbytes, self.timeout)
  File "/tmp/test/venv/lib/python3.9/site-packages/paramiko/buffered_pipe.py", line 164, in read
    raise PipeTimeout()
paramiko.buffered_pipe.PipeTimeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/base_connection.py", line 573, in _read_channel_expect
    new_data = self.remote_conn.recv(MAX_BUFFER)
  File "/tmp/test/venv/lib/python3.9/site-packages/paramiko/channel.py", line 701, in recv
    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/test/pokus.py", line 23, in <module>
    ssh = ConnectHandler(**juniper)
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/ssh_dispatcher.py", line 326, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/juniper/juniper.py", line 20, in __init__
    return super().__init__(*args, **kwargs)
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/base_connection.py", line 350, in __init__
    self._open()
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/base_connection.py", line 356, in _open
    self._try_session_preparation()
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/base_connection.py", line 792, in _try_session_preparation
    self.session_preparation()
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/juniper/juniper.py", line 26, in session_preparation
    self.set_terminal_width(command=cmd, pattern=r"Screen width set to")
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/base_connection.py", line 1131, in set_terminal_width
    output = self.read_until_pattern(pattern=pattern)
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/base_connection.py", line 655, in read_until_pattern
    return self._read_channel_expect(*args, **kwargs)
  File "/tmp/test/venv/lib/python3.9/site-packages/netmiko/base_connection.py", line 583, in _read_channel_expect
    raise NetmikoTimeoutException(
netmiko.ssh_exception.NetmikoTimeoutException: Timed-out reading channel, data not available.

I’ve tried putting several values for delay_factor, turning off global_cmd_verify, setting max_loops to different values, nothing helped. I did try to turn on the Netmiko debugging, looks to me it never enters the CLI mode, but I don’t understand why. I am perfectly able to run the very same command when I connect with the same user manually via SSH.

The code I am trying with:

from netmiko import ConnectHandler

import logging
logging.basicConfig(filename='test.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

juniper = {
    'device_type': 'juniper',
    'host': '192.168.100.9',
    'username': 'root',
    'password': '<password here>',
    'global_cmd_verify': False
}

ssh = ConnectHandler(**juniper)
result = ssh.send_command('show system uptime', delay_factor=5)
print(result)

Debug log:

DEBUG:paramiko.transport:starting thread (client mode): 0x6c45a2e0
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.9.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_6.9
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_6.9)
DEBUG:paramiko.transport:=== Key exchange possibilities ===
DEBUG:paramiko.transport:kex algos: curve25519-sha256@libssh.org, ecdh-sha2-nistp256, ecdh-sha2-nistp384, ecdh-sha2-nistp521, diffie-hellman-group-exchange-sha256, diffie-hellman-group-exchange-sha1, diffie-hellman-group14-sha1, diffie-hellman-group1-sha1
DEBUG:paramiko.transport:server key: ssh-rsa, ssh-dss, ecdsa-sha2-nistp256, ssh-ed25519
DEBUG:paramiko.transport:client encrypt: chacha20-poly1305@openssh.com, aes128-ctr, aes192-ctr, aes256-ctr, aes128-gcm@openssh.com, aes256-gcm@openssh.com, arcfour256, arcfour128, aes128-cbc, 3des-cbc, blowfish-cbc, cast128-cbc, aes192-cbc, aes256-cbc, arcfour, rijndael-cbc@lysator.liu.se
DEBUG:paramiko.transport:server encrypt: chacha20-poly1305@openssh.com, aes128-ctr, aes192-ctr, aes256-ctr, aes128-gcm@openssh.com, aes256-gcm@openssh.com, arcfour256, arcfour128, aes128-cbc, 3des-cbc, blowfish-cbc, cast128-cbc, aes192-cbc, aes256-cbc, arcfour, rijndael-cbc@lysator.liu.se
DEBUG:paramiko.transport:client mac: umac-64-etm@openssh.com, umac-128-etm@openssh.com, hmac-sha2-256-etm@openssh.com, hmac-sha2-512-etm@openssh.com, hmac-sha1-etm@openssh.com, umac-64@openssh.com, umac-128@openssh.com, hmac-sha2-256, hmac-sha2-512, hmac-sha1, hmac-md5-etm@openssh.com, hmac-ripemd160-etm@openssh.com, hmac-sha1-96-etm@openssh.com, hmac-md5-96-etm@openssh.com, hmac-md5, hmac-ripemd160, hmac-ripemd160@openssh.com, hmac-sha1-96, hmac-md5-96
DEBUG:paramiko.transport:server mac: umac-64-etm@openssh.com, umac-128-etm@openssh.com, hmac-sha2-256-etm@openssh.com, hmac-sha2-512-etm@openssh.com, hmac-sha1-etm@openssh.com, umac-64@openssh.com, umac-128@openssh.com, hmac-sha2-256, hmac-sha2-512, hmac-sha1, hmac-md5-etm@openssh.com, hmac-ripemd160-etm@openssh.com, hmac-sha1-96-etm@openssh.com, hmac-md5-96-etm@openssh.com, hmac-md5, hmac-ripemd160, hmac-ripemd160@openssh.com, hmac-sha1-96, hmac-md5-96
DEBUG:paramiko.transport:client compress: none, zlib@openssh.com
DEBUG:paramiko.transport:server compress: none, zlib@openssh.com
DEBUG:paramiko.transport:client lang: <none>
DEBUG:paramiko.transport:server lang: <none>
DEBUG:paramiko.transport:kex follows: False
DEBUG:paramiko.transport:=== Key exchange agreements ===
DEBUG:paramiko.transport:Kex: curve25519-sha256@libssh.org
DEBUG:paramiko.transport:HostKey: ssh-ed25519
DEBUG:paramiko.transport:Cipher: aes128-ctr
DEBUG:paramiko.transport:MAC: hmac-sha2-256
DEBUG:paramiko.transport:Compression: none
DEBUG:paramiko.transport:=== End of kex handshake ===
DEBUG:paramiko.transport:kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-ed25519 host key for 192.168.100.9: b'1c6aaca4a56f19df7bf1d5c00032a6a2'
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:read_channel: 

DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: Last login: Sat Jan  1 22:57:02 2022 from 192.168.100.27
--- JUNOS 15.1R7.9 built 2018-09-11 05:42:39 UTC


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'
DEBUG:netmiko:read_channel: 

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

EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 00;9
H
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 

EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 65H


EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: [1
000;97H
EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 1H


EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: [1
000;153H
EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: ;
177H
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 

EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 10
00;233H
EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: ;2
57H
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 


EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: [10
00;313H
EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 000
;337H
EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 1H

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

EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 417
H

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

EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: 00;
473H
EBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:read_channel: H

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'
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'set cli screen-width 511\n'
DEBUG:netmiko:Pattern is: Screen width set to
DEBUG:netmiko:_read_channel_expect read_data: set cli screen-width 511

DEBUG:netmiko:_read_channel_expect read_data: root@ex4200-24p:RE:0% 
DEBUG:netmiko:_read_channel_expect read_data: 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
DEBUG:netmiko:_read_channel_expect read_data: 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 

DEBUG:netmiko:_read_channel_expect read_data: root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 

DEBUG:netmiko:_read_channel_expect read_data: root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
DEBUG:netmiko:_read_channel_expect read_data: 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
DEBUG:netmiko:_read_channel_expect read_data: 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 

DEBUG:netmiko:_read_channel_expect read_data: root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 

DEBUG:netmiko:_read_channel_expect read_data: root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 

DEBUG:netmiko:_read_channel_expect read_data: root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
root@ex4200-24p:RE:0% 
DEBUG:netmiko:_read_channel_expect read_data: 
root@ex4200-24p:RE:0% set cli screen-width 511
set: Syntax Error.
root@ex4200-24p:RE:0% 
DEBUG:netmiko:write_channel: b'exit\n'
DEBUG:paramiko.transport:EOF in transport thread

Thanks a lot for any hint ! Vladimir

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ktbyerscommented, Jan 4, 2022

I think the issue is that you are logging in with the root account and Netmiko needs to switch to Junos CLI.

And Netmiko needs to execute this:

https://github.com/ktbyers/netmiko/blob/develop/netmiko/juniper/juniper.py#L41

And Netmiko’s code here probably needs improved as I think it fails to enter the Junos CLI properly. So that code probably needs improved. I will flag this as a bug so someone can try to improve this.

0reactions
ktbyerscommented, Apr 14, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

netmiko.juniper.juniper API documentation
Implement methods for interacting with Juniper Networks devices. methods. Overrides several methods for ... Not required if <code>host</code> is provided.
Read more >
DAY ONE: JUNOS® PyEZ COOKBOOK | Juniper Networks
Automate your network tasks with Junos PyEZ scripts that save time, energy, and effort. You don't have to be a coder to take...
Read more >
netmiko 4.1.2 - PythonFix.com
Multi-vendor library to simplify legacy CLI connections to network devices. ... Netmiko code to enter Juniper CLI is not robust enough ...
Read more >
Netmiko Tutorial! How to automate config on a Juniper router
Netmiko is a multi-vendor library that simplifies Paramiko SSH connections to network devices. In today's video we will take a high-level ...
Read more >
Parsing Junos XML with Python - Packet Pushers
So having downloaded and installed his module into my Python modules directory, I coded up a few scripts that use Netmiko to connect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found