0

Im using python and paramiko. Im trying to backup a network device router using python. Below is my script. However im getting error

import paramiko
import sys
import time

HOST = "10.11.214.143"
USER = "admin"
PASS = "passwd"


client1=paramiko.SSHClient()
client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client1.connect(HOST,username=USER,password=PASS)
  print "SSH connection to %s established" %HOST

#copying config to tftp
stdin, stdout, stderr = client1.exec_command('copy nvram:startup-config tftp: 10.11.214.144')
print stdout.read()


Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    client1.exec_command('copy nvram:startup-config tftp:')
  File "C:\Python27\lib\site-packages\paramiko\client.py", line 436, in exec_command
    chan = self._transport.open_session(timeout=timeout)
  File "C:\Python27\lib\site-packages\paramiko\transport.py", line 716, in open_session
    timeout=timeout)
  File "C:\Python27\lib\site-packages\paramiko\transport.py", line 800, in open_channel
    raise SSHException('SSH session not active')
SSHException: SSH session not active
NJones
  • 11
  • 2
  • 5
  • I don't know if it's your case but I know for sure that for some Cisco routers the' exec_command()' method is not working, at least for some versions of Paramiko. I had to use the lower level 'invoke_shell()' API to interact with them. – Alberto Re May 30 '17 at 11:31
  • Can you please advise the modifications required in my script – NJones May 30 '17 at 11:34
  • Have a look at [https://stackoverflow.com/questions/36490989/how-to-keep-ssh-session-not-expired-using-paramiko](https://stackoverflow.com/questions/36490989/how-to-keep-ssh-session-not-expired-using-paramiko) – Błotosmętek May 30 '17 at 11:42

1 Answers1

0

You can try the following approach; I saw exec_command() failing against some routers, but I can't assure it fits your problem:

import paramiko
import sys
import time

HOST = "10.11.214.143"
USER = "admin"
PASS = "passwd"


client1=paramiko.SSHClient()
client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client1.connect(HOST,username=USER,password=PASS)
print "SSH connection to %s established" %HOST

console = client1.invoke_shell()
console.recv(65535)

console.send("copy nvram:startup-config tftp: 10.11.214.144\n")

# Adjust sleep as it's needed by your command
time.sleep(.5)
console.recv(65535)

# Eventually quit gracefully with the right command
console.send("quit\n")
time.sleep(.5)

client1.close()
Alberto Re
  • 514
  • 3
  • 9
  • `copy nvram:startup-config tftp: 10.11.214.144\r\n ^\r\n% Invalid input detected at '^' marker.\r\n\r\nR2>` – NJones May 30 '17 at 11:54
  • Got error while copying.. i think syntax or we might also have to specify the file name for copying – NJones May 30 '17 at 11:55
  • Address or name of remote host and Destination file name can we specify these 2 parameter in command itself. It asks for this parameter when manually executing this command – NJones May 30 '17 at 11:57
  • Sorry but I couldn't help you for the specific command(s) to issue on the remote... – Alberto Re May 30 '17 at 11:58
  • can we specify the paramter in script itself – NJones May 30 '17 at 12:23
  • You can specify the file name in the command like `tftp://10.11.22.33/filename` It will still ask you for confirmation, but you can suppress it using the command `file prompt quiet` https://networkengineering.stackexchange.com/a/3792/51961 – datagutten Nov 22 '18 at 08:24