Hi guys,
I can’t get command output from comamand show tech-support from cisco devices. The below code works while collecting logs for small outputs like ‘show version’ etc. But the script hangs when trying to run tech-support. I understand it is related to read_time, but I can’t find solution(i’m python beginner ).
import getpass
import re
import datetime
from pathlib import Path
from scrapli import Scrapli
username = 'cisco'
password = getpass.getpass("Enter password: ")
enable_password = getpass.getpass("Enter enable password: ")
with open('device_ip.txt') as routers:
for IP in routers:
IP = IP.strip()
Router = {
"host": IP,
"auth_username": username,
"auth_password": password,
"auth_secondary": enable_password,
"auth_strict_key": False,
"transport": "paramiko",
"platform": "cisco_iosxe",
"transport_options": {
"paramiko": {
"kex_algorithms": [
"diffie-hellman-group-exchange-sha256",
"diffie-hellman-group14-sha256",
"diffie-hellman-group-exchange-sha1",
"diffie-hellman-group14-sha1",
],
},
},
}
print(repr(IP))
connection = Scrapli(**Router)
connection.open()
print ('Connecting to ' + IP)
print('-'*79)
output = connection.send_commands_from_file("command_list.txt")
#output = connection.send_configs_from_file("config_changes.txt")
print(output.result)
# Extract the hostname from the tech configuration
hostname = re.search(r"hostname (.+)", output.result)
if hostname:
hostname = hostname.group(1)
else:
hostname = "unknown_hostname"
# Create the backup directory if it doesn't exist
backup_dir = Path(".")
backup_dir.mkdir(parents=True, exist_ok=True)
# Save the output to a file with the date and hostname
# Save the output to a file with the date and hostname
current_date = datetime.datetime.now().strftime("%Y-%m-%d")
file_name = f"{current_date}_{hostname}.txt"
file_path = backup_dir / file_name
with file_path.open("w") as f:
f.write(output.result)
print(f"Saved config to: {file_path}")
connection.close()
Enter password:
Enter enable password:
‘10.10.104.1’
Connecting to 10.10.104.1
Traceback (most recent call last):
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/transport/plugins/paramiko/transport.py”, line 275, in read
buf: bytes = self.session_channel.recv(65535)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/paramiko/channel.py”, line 697, in recv
out = self.in_buffer.read(nbytes, self.timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/paramiko/buffered_pipe.py”, line 150, in read
self._cv.wait(timeout)
File “/usr/lib/python3.11/threading.py”, line 324, in wait
gotit = waiter.acquire(True, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/decorators.py”, line 76, in _signal_raise_exception
return _handle_timeout(transport=transport, logger=logger, message=message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/decorators.py”, line 141, in _handle_timeout
raise ScrapliTimeout(message)
scrapli.exceptions.ScrapliTimeout: timed out sending input to device
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “/home/user/Python-Net-Eng/test.py”, line 39, in
output = connection.send_commands_from_file(“command_list.txt”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/driver/network/sync_driver.py”, line 353, in send_commands_from_file
return super().send_commands_from_file(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/driver/generic/sync_driver.py”, line 283, in send_commands_from_file
return self.send_commands(
^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/driver/network/sync_driver.py”, line 300, in send_commands
responses = super().send_commands(
^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/driver/generic/sync_driver.py”, line 236, in send_commands
response = self._send_command(
^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/decorators.py”, line 306, in decorate
result = wrapped_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/driver/generic/sync_driver.py”, line 144, in _send_command
raw_response, processed_response = self.channel.send_input(
^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/decorators.py”, line 242, in decorate
return wrapped_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/channel/sync_channel.py”, line 492, in send_input
buf += self._read_until_prompt()
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/channel/sync_channel.py”, line 137, in _read_until_prompt
read_buf.write(self.read())
^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/channel/sync_channel.py”, line 69, in read
buf = self.transport.read()
^^^^^^^^^^^^^^^^^^^^^
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/transport/plugins/paramiko/transport.py”, line 282, in read
raise ScrapliConnectionError(msg) from exc
scrapli.exceptions.ScrapliConnectionError: encountered EOF reading from transport; typically means the device closed the connection
(.venv) user@python3lab:~/Python-Net-Eng$