Sujet : Can't trap paramiko runtime trace-back error
De : ujlain (at) *nospam* gmail.com (Vinode Singh Ujlain)
Groupes : comp.lang.pythonDate : 22. May 2024, 08:06:59
Autres entêtes
Message-ID : <mailman.45.1716402365.2909.python-list@python.org>
References : 1
User-Agent : Mozilla Thunderbird
When running the code below , I get error as enumerated below. Why am I not able to trap this paramiko runtime traceback in try-except block ?
Exception (client): Error reading SSH protocol banner
Traceback (most recent call last):
File "/home/uzi/.local/lib/python3.8/site-packages/paramiko/transport.py", line 2327, in _check_banner
buf = self.packetizer.readline(timeout)
File "/home/uzi/.local/lib/python3.8/site-packages/paramiko/packet.py", line 381, in readline
buf += self._read_timeout(timeout)
File "/home/uzi/.local/lib/python3.8/site-packages/paramiko/packet.py", line 626, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/uzi/.local/lib/python3.8/site-packages/paramiko/transport.py", line 2143, in run
self._check_banner()
File "/home/uzi/.local/lib/python3.8/site-packages/paramiko/transport.py", line 2331, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
SSH error: No existing session
importparamiko
importtime
defexecute():
try:
# Define the server and credentials
ip='p.q.r.s'
un, up, po='name', "passwd", 22
bto, sto=60, 60
ssh_client=paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, port=po, username=un, password=up, banner_timeout=bto, timeout=sto)
shell=ssh_client.invoke_shell()
defsend_command(command, wait_time=1):
shell.send(command+'\n')
time.sleep(wait_time) # Give the command some time to execute
whilenotshell.recv_ready():
time.sleep(0.1)
returnshell.recv(8192).decode()
output=send_command('date')
print("Output:\n", output)
delssh_client
exceptparamiko.SSHExceptionase:
print(f"SSH error: {e}")
exceptExceptionase:
print(f"Error: {e}")
finally:
# Close the SSH client connection
delssh_client
execute()
-- Warm Regards,Vinode Singh Ujlain | https://www.linkedin.com/in/ujlain/------------------------------------------------------------------------