Modify script, that will work with multiple device from file

Hi python community,
I’m new on python and trying to write script, that will connect multiple device and put config from file. Script must be simple as it can. :slight_smile: I spend lot of time to trying to do this, but stuck with loops, with one IP works fine. Maybe someone can modify script, that will work with multiple device from file(device_ip.txt). Thx.

from scrapli.driver.core import IOSXEDriver
import getpass
from scrapli import Scrapli

username = 'cisco'
password = getpass.getpass("Enter password: ")
enable_password = getpass.getpass("Enter enable password: ")

ip = "10.10.0.1"

device = {
    "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",
            ],
        },
    },
}

connection = Scrapli(**device)
connection.open()
output = connection.send_configs_from_file("config_changes.txt")
print(output.result)
connection.close()

code with loop, and erros that I’m getting

from scrapli.driver.core import IOSXEDriver
import getpass
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:
        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",
                    ],
                },
            },
        }

        connection = Scrapli(**Router)
        connection.open()
        output = connection.send_configs_from_file("config_changes.txt")
        print(output.result)
connection.close()

Enter password:
Enter enable password:
Traceback (most recent call last):
File “/home/user/Python-Net-Eng/send_commands_from_file_to_multiple_dev.py”, line 32, in
connection.open()
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/driver/base/sync_driver.py”, line 89, in open
self.transport.open()
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/transport/plugins/paramiko/transport.py”, line 78, in open
self.socket.open()
File “/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/transport/base/base_socket.py”, line 129, in open
raise ScrapliConnectionNotOpened(“failed to determine socket address family for host”)
scrapli.exceptions.ScrapliConnectionNotOpened: failed to determine socket address family for host
(.venv) user@python3lab:~/Python-Net-Eng$

suggest you print repr(IP) that is about to be used so you know what host is failing. Does the IP make sense? Does it need spaces striping off etc?

1 Like

Your suggestion was good and showed that when in .txt file is more than 1IP(more than 1 line), then after IP is \n, if i leave only 1 IP in .txt then it works. Maybe you now what is the best way to solve this? Thx

Enter password: 
Enter enable password: 
'10.100.0.126\n'
Traceback (most recent call last):
  File "/home/user/Python-Net-Eng/send_commands_from_file_to_multiple_dev.py", line 32, in <module>
    connection.open()
  File "/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/driver/base/sync_driver.py", line 89, in open
    self.transport.open()
  File "/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/transport/plugins/paramiko/transport.py", line 78, in open
    self.socket.open()
  File "/home/user/Python-Net-Eng/.venv/lib/python3.11/site-packages/scrapli/transport/base/base_socket.py", line 129, in open
    raise ScrapliConnectionNotOpened("failed to determine socket address family for host")
scrapli.exceptions.ScrapliConnectionNotOpened: failed to determine socket address family for host
(.venv) user@python3lab:~/Python-Net-Eng$

I think all you need to do is use strip() on the IP.

for IP in routes:
    IP = IP.strip()
1 Like

yeee, it works. THX

can you help with other my problem " Unable to collect ‘sh tech-support’ from Cisco ? :slight_smile: