python、paramiko、ssh 异常 ssh 会话未激活
python, paramiko, ssh exception ssh session not active
我正在编写一个 python 脚本,该脚本会转到我们网络中的每个交换机,并发出一个副本 运行-config TFTP 命令,备份 运行 配置的开关。我正在使用 windows 并使用 Python 2.7 中的 paramiko 库。
脚本本身非常简单,它所做的就是创建一个名为 "Backups" 的目录(如果尚不存在)和另一个名为今天的日期的目录,然后将该目录用于 TFTP。并启动 TFTP 服务器。然后它只是通过 ssh 发出复制命令。
我试图解决的这个问题是在 connectSwitch() 期间。具体在第二个 ssh.exec_command('x.x.x.x') 上。如果您不了解交换机,copy 运行-config tftp,是发送的第一个命令,交换机询问主机,然后发送第二个命令,其中包含主机 IP。第三个命令是您希望文件所在的目录。
import paramiko
import getpass
import os
import time
from datetime import date
paramiko.util.log_to_file("filename.log")
d = date.today()
filename = d.strftime("%Y.%m.%d")
UUser = "first.last"
print UUser
UPass = getpass.getpass('Enter your Password: ')
def writeFile(text, Host):#Writes to the Switches backup File
fl = open(Host+".txt", 'w')
fl.write(text)
def openIPs():#Opens the "IPs" file
fi = open('IPs.txt', 'r')
content = fi.readlines()
fi.close()
print len(content)
makeDirBackUp() #Creates "Backup" Dir
makeDir() #Creates a Directory based and named on todays date
for item in content:
response = os.system("ping -n 1 " +item)
if response == 0:
print item + "PING STATUS: SUCCESS"
print "BACKING UP CONFIG..."
connectSwitch(UUser, UPass, item.strip('\n')) #SSH connection to Switch
else:
print item + "PING STATUS: FAIL"
def makeDir():#Creates a Directory based and named on todays date
if not os.path.exists(filename):
os.makedirs(filename)
os.chdir(filename)
def makeDirBackUp():#Creates "Backup" Dir
if not os.path.exists("Backups"):
os.makedirs("Backups")
os.chdir("Backups")
def connectSwitch(UUser, UPass, Host):#SSH connection to Switch
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(Host, port=22, username=UUser, password=UPass)
print "Command #1"
sendtoCRT = 'copy running-config tftp'
stdin, stdout, stderr = ssh.exec_command(sendtoCRT)
print "Command #1 sent"
print sendtoCRT
time.sleep(1)
print "readlines for Command #1"
print "Command #2"
stdin, stdout, stderr = ssh.exec_command('10.10.10.10')
time.sleep(1)
print "Command #2 Sent"
print "Command #3"
stdin, stdout, stderr = ssh.exec_command('Backups/'+filename+'/'+Host)
time.sleep(1)
print "Command #3 Sent"
def startTFTP():
sendToOS="tftpd32.exe"
exe1 = os.system(sendToOS)
sendToOS='\n'
exe = os.system(sendToOS)
exe = os.system(sendToOS)
startTFTP()
openIPs()
我的错误发生在下面的 connectSwitch() 中。
Traceback (most recent call last):
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 98, in <module> openIPs()
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 32, in openIPs connectSwitch(UUser, UPass, item.strip('\n')) #SSH connection to Switch
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 68, in connectSwitch stdin, stdout, stderr = ssh.exec_command('138.86.51.189')
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\client.py", line 341, in exec_command chan = self._transport.open_session()
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 615, in open_session max_packet_size=max_packet_size)
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 696, in open_channel raise SSHException('SSH session not active')
paramiko.ssh_exception.SSHException: SSH session not active
任何人对此有任何想法。我在 paramiko 中找不到太多关于错误文档的信息,所以如果有人知道在哪里可以找到它,请延迟。 net/paramiko/docs/ 之前好像下线了
但即使使用https://web.archive.org/web/20140425222451/http://www.lag.net/paramiko/docs/,这里似乎也没有多少。
非常感谢你们!
编辑:将第一个 "sendtoCRT" 更改为不再包含 :\,因为在分三步执行命令时不需要它。然而,这似乎也改变了我的错误
Traceback (most recent call last):
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 98,in <module> openIPs()
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 32, in openIPs connectSwitch(UUser, UPass, item.strip('\n')) #SSH connection to Switch
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 68, in connectSwitch stdin, stdout, stderr = ssh.exec_command('138.86.51.189')
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\client.py", line 341, in exec_command chan = self._transport.open_session()
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 615, in open_session max_packet_size=max_packet_size)
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 740, in open_channel raise e
paramiko.ssh_exception.ChannelException: (4, 'Resource shortage')
我还找到了更多文档。
好的,伙计们,我不知道为什么会收到这些错误。但我确实找到了解决方法。创建 SSHClient 后,使用连接,您可以 Invoke_Shell,它会打开一个通道,在您通过它发送内容后它不会关闭,这很棒。下面是我更新的 connectSwitch 代码,它利用了这个解决方法。
def connectSwitch(UUser, UPass, Host):#SSH connection to Switch
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(Host, port=22, username=UUser, password=UPass)
print "\n\nNewInvoke Shell\n"
chan = ssh.invoke_shell()
resp = chan.recv(9999)
print resp
print chan.send_ready()
chan.send('copy running-config tftp\n')
time.sleep(3)
resp = chan.recv(9999)
print resp
chan.send('138.86.51.189\n')
time.sleep(3)
resp = chan.recv(9999)
print resp
chan.send('Backups/'+filename+'/'+Host+'\n')
time.sleep(3)
resp = chan.recv(9999)
print resp
print"\nEnd invoke Shell\n\n"
我正在编写一个 python 脚本,该脚本会转到我们网络中的每个交换机,并发出一个副本 运行-config TFTP 命令,备份 运行 配置的开关。我正在使用 windows 并使用 Python 2.7 中的 paramiko 库。
脚本本身非常简单,它所做的就是创建一个名为 "Backups" 的目录(如果尚不存在)和另一个名为今天的日期的目录,然后将该目录用于 TFTP。并启动 TFTP 服务器。然后它只是通过 ssh 发出复制命令。
我试图解决的这个问题是在 connectSwitch() 期间。具体在第二个 ssh.exec_command('x.x.x.x') 上。如果您不了解交换机,copy 运行-config tftp,是发送的第一个命令,交换机询问主机,然后发送第二个命令,其中包含主机 IP。第三个命令是您希望文件所在的目录。
import paramiko
import getpass
import os
import time
from datetime import date
paramiko.util.log_to_file("filename.log")
d = date.today()
filename = d.strftime("%Y.%m.%d")
UUser = "first.last"
print UUser
UPass = getpass.getpass('Enter your Password: ')
def writeFile(text, Host):#Writes to the Switches backup File
fl = open(Host+".txt", 'w')
fl.write(text)
def openIPs():#Opens the "IPs" file
fi = open('IPs.txt', 'r')
content = fi.readlines()
fi.close()
print len(content)
makeDirBackUp() #Creates "Backup" Dir
makeDir() #Creates a Directory based and named on todays date
for item in content:
response = os.system("ping -n 1 " +item)
if response == 0:
print item + "PING STATUS: SUCCESS"
print "BACKING UP CONFIG..."
connectSwitch(UUser, UPass, item.strip('\n')) #SSH connection to Switch
else:
print item + "PING STATUS: FAIL"
def makeDir():#Creates a Directory based and named on todays date
if not os.path.exists(filename):
os.makedirs(filename)
os.chdir(filename)
def makeDirBackUp():#Creates "Backup" Dir
if not os.path.exists("Backups"):
os.makedirs("Backups")
os.chdir("Backups")
def connectSwitch(UUser, UPass, Host):#SSH connection to Switch
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(Host, port=22, username=UUser, password=UPass)
print "Command #1"
sendtoCRT = 'copy running-config tftp'
stdin, stdout, stderr = ssh.exec_command(sendtoCRT)
print "Command #1 sent"
print sendtoCRT
time.sleep(1)
print "readlines for Command #1"
print "Command #2"
stdin, stdout, stderr = ssh.exec_command('10.10.10.10')
time.sleep(1)
print "Command #2 Sent"
print "Command #3"
stdin, stdout, stderr = ssh.exec_command('Backups/'+filename+'/'+Host)
time.sleep(1)
print "Command #3 Sent"
def startTFTP():
sendToOS="tftpd32.exe"
exe1 = os.system(sendToOS)
sendToOS='\n'
exe = os.system(sendToOS)
exe = os.system(sendToOS)
startTFTP()
openIPs()
我的错误发生在下面的 connectSwitch() 中。
Traceback (most recent call last):
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 98, in <module> openIPs()
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 32, in openIPs connectSwitch(UUser, UPass, item.strip('\n')) #SSH connection to Switch
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 68, in connectSwitch stdin, stdout, stderr = ssh.exec_command('138.86.51.189')
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\client.py", line 341, in exec_command chan = self._transport.open_session()
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 615, in open_session max_packet_size=max_packet_size)
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 696, in open_channel raise SSHException('SSH session not active')
paramiko.ssh_exception.SSHException: SSH session not active
任何人对此有任何想法。我在 paramiko 中找不到太多关于错误文档的信息,所以如果有人知道在哪里可以找到它,请延迟。 net/paramiko/docs/ 之前好像下线了
但即使使用https://web.archive.org/web/20140425222451/http://www.lag.net/paramiko/docs/,这里似乎也没有多少。
非常感谢你们!
编辑:将第一个 "sendtoCRT" 更改为不再包含 :\,因为在分三步执行命令时不需要它。然而,这似乎也改变了我的错误
Traceback (most recent call last):
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 98,in <module> openIPs()
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 32, in openIPs connectSwitch(UUser, UPass, item.strip('\n')) #SSH connection to Switch
File "C:\Users\first.last\Documents\Backups\paramikoConnect.py", line 68, in connectSwitch stdin, stdout, stderr = ssh.exec_command('138.86.51.189')
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\client.py", line 341, in exec_command chan = self._transport.open_session()
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 615, in open_session max_packet_size=max_packet_size)
File "C:\Python27\lib\site-packages\paramiko-1.15.2-py2.7.egg\paramiko\transport.py", line 740, in open_channel raise e
paramiko.ssh_exception.ChannelException: (4, 'Resource shortage')
我还找到了更多文档。
好的,伙计们,我不知道为什么会收到这些错误。但我确实找到了解决方法。创建 SSHClient 后,使用连接,您可以 Invoke_Shell,它会打开一个通道,在您通过它发送内容后它不会关闭,这很棒。下面是我更新的 connectSwitch 代码,它利用了这个解决方法。
def connectSwitch(UUser, UPass, Host):#SSH connection to Switch
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(Host, port=22, username=UUser, password=UPass)
print "\n\nNewInvoke Shell\n"
chan = ssh.invoke_shell()
resp = chan.recv(9999)
print resp
print chan.send_ready()
chan.send('copy running-config tftp\n')
time.sleep(3)
resp = chan.recv(9999)
print resp
chan.send('138.86.51.189\n')
time.sleep(3)
resp = chan.recv(9999)
print resp
chan.send('Backups/'+filename+'/'+Host+'\n')
time.sleep(3)
resp = chan.recv(9999)
print resp
print"\nEnd invoke Shell\n\n"