1

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
Community
  • 1
  • 1
yegle
  • 5,795
  • 6
  • 39
  • 61

2 Answers2

1

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

Anesh
  • 194
  • 1
  • 13
0

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.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • I have to use `pexpect` instead of `paramiko` :-( And BTW what's the option to enable multiple command for IOS? thanks :-) – yegle Dec 01 '11 at 03:39
  • No, you can use `pexpect` with paramiko to script working with an interactive prompt. I don't work with IOS myself, I am just guessing that there may be an option to enable multiple command. – JimB Dec 01 '11 at 11:57