Skip to content

pyats login#

from pyats.topology import loader
from pprint import pprint
import os


testbed = loader.load('./testbed_topology.yaml')
device = testbed.devices['R1']
device.connect()

sh_plt = device.execute('show platform')
print(sh_plt)
testbed_topology.yaml
devices:
  R1-Router:
    alias: R1
    os: "iosxr"
    credentials:
      default:
        username: cisco
        password: cisco
    connections:
      cli:
        protocol: telnet
        ip: "192.168.1.1"
        arguments:
          init_config_commands:
            - line default exec-timeout 0 0
            - line default absolute-timeout 0
            - line default session-timeout 0
unicon_login.py
from unicon import Connection


dev = Connection(
    hostname='n7k2-1',
                 start=['telnet 172.27.114.43 2037',
                        'telnet 172.27.114.43 2038'],
                 credentials={'default': {'username': 'admin', 'password': 'Cisc0123'}},
                 os='nxos')
dev.connect()
set_hostname.py
from unicon import Connection


ip_list = []
usr_list = []
passwd_list = []

dev_list = []
host_list = []


for num,(ip,usr,passwd) in enumerate(zip(ip_list,usr_list,passwd_list)):
    dev = "dev" + str(num)
    devive = Connection(
        hostname="R",
        learn_hostname=True,
        start=["telnet " + f"{ip}"],
        credentials={"default": {"username": f"{usr}", "password": f"{passwd}"}},
        os="iosxr",
        log_stdout=False,
        logfile=False,
        learn_os="True",
        prompt_recovery=True
    )
    dev_list.append(devive)

def connect_device(dev_list, host_list):
    for dev, hostname in zip(dev_list,host_list):
        dev.connect()
        dev.configure(hostname)
        print("set " + f"{hostname}" OK)

connect_device(dev_list, host_list)