I am writing a script for automating the delivery of files to Cisco devices, and when the device is not configured as an SCP server, I receive an error that kills my entire script.
I have added two exceptions for the other script-killing exceptions, but when I try and add it for this SCP error, I receive the following error:
try:
ssh_client =paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Attempting SSH connection with "+ip)
ssh_client.connect(hostname=ip,username=user,password=passw)
print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Successfully established SSH connection with "+ip)
scp = SCPClient(ssh_client.get_transport(), progress = progress)
print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Transferring file to "+ip)
scp.put(src, remote_path=dst)
print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Successfully transferred "+src+" to "+ip)
ssh_client.close()
print((time.strftime('%d-%b-%Y %H:%M:%S'))+" - Closed SSH session with "+ip)
print("###########")
f = open("scpaudit.txt","a")
f.write(time.strftime('%d-%b-%Y %H:%M:%S')+" "+user+" transferred "+src+" to "+ip)
f.close()
except paramiko.ssh_exception.AuthenticationException:
print("-> ERROR: Auth error for "+user+" on "+ip)
print("#########")
f = open("scpaudit.txt","a")
f.write(time.strftime('%d-%b-%Y %H:%M:%S')+" ERROR: Authentication Error for "+user+" to "+ip)
f.close()
except paramiko.ssh_exception.NoValidConnectionsError:
print("-> ERROR: Unable to connect to "+ip)
print("#########")
f = open("scpaudit.txt","a")
f.write(time.strftime('%d-%b-%Y %H:%M:%S')+" ERROR: Unable to connect to "+ip)
f.close()
When I encounter a device that does not have the SCP Server enabled, I received the following error:
Traceback (most recent call last):
File "scpscript.py", line 100, in <module>
yes_or_no("I confirm that the above information is correct and I would like to proceed. ")
File "scpscript.py", line 93, in yes_or_no
return session()
File "scpscript.py", line 65, in session
scp.put(src, remote_path=dst)
File "/usr/local/lib/python2.7/dist-packages/scp.py", line 158, in put
self._recv_confirm()
File "/usr/local/lib/python2.7/dist-packages/scp.py", line 363, in _recv_confirm
raise SCPException(asunicode(msg[1:]))
scp.SCPException: Administratively disabled.
So I then add the following exception to the end (ninja edit at 07:15 - copied the wrong exception to stackoverflow):
except scp.SCPException:
print("-> SCP administratively disabled on remote device "+ip)
...which then results in the following error:
Traceback (most recent call last):
File "scpscript.py", line 100, in <module>
yes_or_no("I confirm that the above information is correct and I would like to proceed. ")
File "scpscript.py", line 93, in yes_or_no
return session()
File "scpscript.py", line 85, in session
except scp.SCPException(asunicode(msg[:])):
AttributeError: 'SCPClient' object has no attribute 'SCPException'
Out of ideas. Is it possible that the lines in try
could be organized better? I appreciate any feedback! Thank you.
You have a conflict between scp
module name and your local scp
variable.
One possible solution is to change the name of your variable. Following the ssh_client
pattern, you can use scp_client
:
scp_client = SCPClient(ssh_client.get_transport(), progress = progress)
scp_client.put(src, remote_path=dst)