Lab com switch com ip na lan - objetivo e ler o sh run identificar o ip e pingar para ele python 3.9


import paramiko


router_ip = "192.168.1.230"

router_username = "admin"

router_password = "admin"



def ssh_connect(command):

    ssh = paramiko.SSHClient()

    # Load SSH host keys.

    ssh.load_system_host_keys()

    # Add SSH host key automatically if needed.

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # Connect to router using username/password authentication.

    ssh.connect(router_ip,username=router_username, password=router_password,look_for_keys=False )

    # Run command.

    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)

    result  = ssh_stdout.readlines()

    return result




verifica = ssh_connect("sh run int vlan 1")

interface = []

if verifica:

        for line in verifica:

            result =line.replace(' ',':')

            

            if 'address' in line:

                interface_name = result.split(':')[3] #divide em index

                interface.append(interface_name)# cria lista

                ip = " ".join(interface) #transforma em string

                print(ip)

                testa = ssh_connect(f"ping {ip}")

                print (testa)



>>> 

=============================================================== RESTART: C:/Users/User/Desktop/PYTHONSCRIPTS/descobrindo-gateway.py ==============================================================

192.168.1.230

['\r\n', 'Type escape sequence to abort.\r\n', 'Sending 5, 100-byte ICMP Echos to 192.168.1.230, timeout is 2 seconds:\r\n', '!!!!!\r\n', 'Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms']