[英]Backup router config using python
我正在使用python和paramiko。 我正在嘗試使用python備份網絡設備路由器。 下面是我的腳本。 但是我收到錯誤
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
您可以嘗試以下方法; 我看到exec_command()對某些路由器失敗,但是我不能保證它適合您的問題:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.