yegle
yegle

Reputation: 5875

Paramiko: failed to re-use ssh session when connecting to Cisco C2960 switcher

I'm trying to write a simple script to connect Cisco C2960 switcher.I just can't figure out how to re-use the ssh session to execute more than two commands.

There's a discussion on SO,
Persistent ssh session to Cisco router
but none of the answers provided there can solve my problem.

Here's my code:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxx',username='xxx',password='xxx',allow_agent=False)
stdin, stdout, stderr = ssh.exec_command('show version')
stdin, stdout, stderr = ssh.exec_command('sh mac brief')

Results in:

Traceback (most recent call last):
  File "./test.py", line 10, in <module>  
    stdin, stdout, stderr = ssh.exec_command('sh mac brief')
  File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 363, in exec_command
    chan = self._transport.open_session()
  File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 658, in open_session
    return self.open_channel('session')
  File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 746, in open_channel
    raise e
EOFError

Upvotes: 1

Views: 2154

Answers (2)

Anesh
Anesh

Reputation: 194

invoke_shell() is best when interacting with Cisco IOS, i tried other fucntions in paramiko but all of them throw buggy EOF file errors

Upvotes: 1

Mr_Pink
Mr_Pink

Reputation: 109442

I answered this on the referenced SO question, but did you try using invoke_shell()?

I've seen many reports that some Cisco devices only allow one command execution before closing the connection (this may be configurable somewhere in the device though). In this case, you need to start a shell, and work interactively (or pseudo-interactively like with pexpect), or create a script to send as a single command.

Upvotes: 0

Related Questions