Apr-20-2020, 06:06 PM
Hello, I have a script that get the bkp config from devices listed on a file.
The script run ok until find a down node, when a down node is present on devices list the script stops and an error appear on the screen.
My goal is to make the script ignore the unresponsive device and run agains the next IP on the list.
My script is this:
*********************************************************************************************
import sys
import os
import cmd
import datetime
import paramiko
import time
from keyboard import press
from getpass import getpass
now = datetime.datetime.now()
username = "secret"
password = "secret"
#start FOR ...in
f = open('fortigate_list')
for ip in f.readlines():
ip = ip.strip()
filename_prefix ='bkp_fortigate_' + ip
SESSION = paramiko.SSHClient()
SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SESSION.connect(ip,port=9922,
username=username,
password=password,
look_for_keys=False,
allow_agent=False)
DEVICE_ACCESS = SESSION.invoke_shell()
DEVICE_ACCESS.send(b'config system console\n')
time.sleep(1)
DEVICE_ACCESS.send(b'set output standard\n')
time.sleep(1)
DEVICE_ACCESS.send(b'end\n')
time.sleep(1)
DEVICE_ACCESS.send(b'show full-configuration\n')
time.sleep(10)
DEVICE_ACCESS.send(b'show system global\n')
time.sleep(1)
output = DEVICE_ACCESS.recv(9999999)
print output
filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)
ff = open(filename, 'a')
ff.write(output)
ff.close()
DEVICE_ACCESS.close()
print ip
f.close()
**********************************************************************************************
The error is:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Traceback (most recent call last):
File "ignoraerro", line 30, in <module>
allow_agent=False)
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 349, in connect
retry_on_signal(lambda: sock.connect(addr))
File "/usr/local/lib/python2.7/dist-packages/paramiko/util.py", line 283, in retry_on_signal
return function()
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 349, in <lambda>
retry_on_signal(lambda: sock.connect(addr))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 11] Resource temporarily unavailable
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Can you please help me to improve the script so it can skip the error and keep running the script against the next devices?
The script run ok until find a down node, when a down node is present on devices list the script stops and an error appear on the screen.
My goal is to make the script ignore the unresponsive device and run agains the next IP on the list.
My script is this:
*********************************************************************************************
import sys
import os
import cmd
import datetime
import paramiko
import time
from keyboard import press
from getpass import getpass
now = datetime.datetime.now()
username = "secret"
password = "secret"
#start FOR ...in
f = open('fortigate_list')
for ip in f.readlines():
ip = ip.strip()
filename_prefix ='bkp_fortigate_' + ip
SESSION = paramiko.SSHClient()
SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SESSION.connect(ip,port=9922,
username=username,
password=password,
look_for_keys=False,
allow_agent=False)
DEVICE_ACCESS = SESSION.invoke_shell()
DEVICE_ACCESS.send(b'config system console\n')
time.sleep(1)
DEVICE_ACCESS.send(b'set output standard\n')
time.sleep(1)
DEVICE_ACCESS.send(b'end\n')
time.sleep(1)
DEVICE_ACCESS.send(b'show full-configuration\n')
time.sleep(10)
DEVICE_ACCESS.send(b'show system global\n')
time.sleep(1)
output = DEVICE_ACCESS.recv(9999999)
print output
filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)
ff = open(filename, 'a')
ff.write(output)
ff.close()
DEVICE_ACCESS.close()
print ip
f.close()
**********************************************************************************************
The error is:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Traceback (most recent call last):
File "ignoraerro", line 30, in <module>
allow_agent=False)
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 349, in connect
retry_on_signal(lambda: sock.connect(addr))
File "/usr/local/lib/python2.7/dist-packages/paramiko/util.py", line 283, in retry_on_signal
return function()
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 349, in <lambda>
retry_on_signal(lambda: sock.connect(addr))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 11] Resource temporarily unavailable
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Can you please help me to improve the script so it can skip the error and keep running the script against the next devices?