8

I have search on this site and multiple other locations but I have been unable to resolve my problem of connecting and maintaining ssh session after one command. Below is my current code:

#!/opt/local/bin/python

import os  

import pexpect

import paramiko

import hashlib

import StringIO

while True:

      cisco_cmd = raw_input("Enter cisco router cmd:")

      ssh = paramiko.SSHClient()

      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

      ssh.connect('192.168.221.235', username='nuts', password='cisco', timeout =  30)

      stdin, stdout, stderr = ssh.exec_command(cisco_cmd)

      print stdout.read()

      ssh.close()

      if  cisco_cmd == 'exit': break

I can run multiple commands but for every commands a new ssh session is created. The above program does not work when I need to configuration mode because ssh session is not reused.Any assistance in resolving this matter is greatly appreciated.

JimB
  • 104,193
  • 13
  • 262
  • 255
msudi
  • 169
  • 1
  • 1
  • 8

4 Answers4

7

I used Exscript instead of paramiko and I am now able to get persistent session on IOS device.

#!/opt/local/bin/python
import hashlib
import Exscript

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              # Prompt the user for his name and password
conn = SSH2()                       # We choose to use SSH2
conn.connect('192.168.221.235')     # Open the SSH connection
conn.login(account)                 # Authenticate on the remote host
conn.execute('conf t')              # Execute the "uname -a" command
conn.execute('interface Serial1/0')
conn.execute('ip address 114.168.221.202 255.255.255.0')
conn.execute('no shutdown')
conn.execute('end')
conn.execute('sh run int Serial1/0')
print conn.response

conn.execute('show ip route')
print conn.response

conn.send('exit\r')                 # Send the "exit" command
conn.close()                        # Wait for the connection to close
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
msudi
  • 169
  • 1
  • 1
  • 8
1

You need to create, connect and close connection outside the while loop.

Vlad H
  • 3,629
  • 1
  • 19
  • 13
1

Your loop does that

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.221.235', username='nuts', password='cisco', timeout =  30)
while True:
      cisco_cmd = raw_input("Enter cisco router cmd:")
      stdin, stdout, stderr = ssh.exec_command(cisco_cmd)
      print stdout.read()
      if  cisco_cmd == 'exit': break
ssh.close()

Move the initialisation and setup outside the loop. EDIT: Moved close()

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • 1
    `ssh.close()` shouldn't be in the loop either. – Vlad H Mar 08 '11 at 20:40
  • I am able to connect and successfully run the first command.I have been unable to setup a connection that persists in order for multiple related commands can be run. – msudi Mar 09 '11 at 19:06
1

The above program does not work when I need to configuration mode because ssh session is not reused

Your ssh session will be reused once you move the connect and close outside of the loop, but each exec_command() happens in a new shell (through a new channel), and are unrelated. You will need to format your commands so that they don't require any state from the shell.

If I remember correctly, some Cisco devices only allow a single exec, and then close the connection. In that case, you will need to use invoke_shell(), and work interactively using the pexpect module (which you already have imported, but aren't using).

JimB
  • 104,193
  • 13
  • 262
  • 255
  • Thanks,still trying to invoke_shell() to no avail. I will report back in a day or two when I fix my issue. – msudi Mar 09 '11 at 19:09