I am working on a python script that goes to each switch on our network and issues a TFTP command to copy to run, which backs up the current switch configuration. I am in windows and am using the paramiko library in Python 2.7.
The script itself is quite simple, all it does is create a directory called "Backups" if it does not already exist, and another directory called date date, and then use this directory for TFTP. And it starts the TFTP server. Then it just issues a copy command via ssh.
This is the problem I'm trying to overcome during connectSwitch (). In particular, on the second ssh.exec_command ('xxxx'). If you do not know the switches, copy running-config tftp, this is the first command sent, the switch asks for the host, and the second command is sent, which contains the host IP address. The third command is the directory in which you want the file to be.
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()
My error occurs in connectSwitch () in its entirety below.
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
Does anyone have any ideas about this. I canโt learn a lot about paramiko bug documentation, so if someone knows where to find it, leave it alone. net / paramiko / docs / seems to have been offline recently.
but even using https://web.archive.org/web/20140425222451/http://www.lag.net/paramiko/docs/ , there seems to be not much here.
Thank you guys so much!
EDIT: changed the first "sendtoCRT" to no longer include: \ because it is not needed when you execute the command in three steps. However, this also apparently changed my mistake
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')
I also found more documents.
http://docs.paramiko.org/en/1.15/api/ssh_exception.html