i'm working on python script goes each switch in our network, , issues copy running-config tftp command, backs running configuration of switch. i'm on windows , utilizing paramiko library in python 2.7.
the script pretty simple, create directory called "backups" if 1 doesn't exist already, , directory named today's date, , uses directory tftp. , starts tftp server. issues copy command via ssh.
this issue trying overcome during connectswitch(). on second ssh.exec_command('x.x.x.x'). if don't know switches, copy running-config tftp, first command sent, switch asks host, , second command sent, contains host ip. , third command directory want file located.
import paramiko import getpass import os import time 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 password: ') def writefile(text, host):#writes switches backup file fl = open(host+".txt", 'w') fl.write(text) def openips():#opens "ips" file fi = open('ips.txt', 'r') content = fi.readlines() fi.close() print len(content) makedirbackup() #creates "backup" dir makedir() #creates directory based , named on todays date item in content: response = os.system("ping -n 1 " +item) if response == 0: print item + "ping status: success" print "backing config..." connectswitch(uuser, upass, item.strip('\n')) #ssh connection switch else: print item + "ping status: fail" def makedir():#creates directory based , 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 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 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 occurring in connectswitch() in full, 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 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 anyone have ideas this. can't find error documentation in paramiko, if knows find it, lag. net/paramiko/docs/ seems have been taken offline while ago.
but using https://web.archive.org/web/20140425222451/http://www.lag.net/paramiko/docs/, there doesn't seem here.
thanks guys!
edit: changed first "sendtocrt" no longer include :\, because don't need when doing command in 3 steps. seemingly changed error
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 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') also found more docs.
alright guys, didn't firgure out why getting these errors. did find work around. after creating sshclient, using connect, can can invoke_shell, , opens channel, doesn't close after send through it, great. below updated connectswitch code, utilizes work around.
def connectswitch(uuser, upass, host):#ssh connection 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"
Comments
Post a Comment