1

Please pardon me as I am a very new to any programming language. I have around 25 network devices combination of cisco, juniper, linux etc which i need to remotely access and run some basic cli commands to get the output. Individually SSHing in to the devices will take long time. Can some tell me where to start this basic script?

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • You can do that using paramiko (https://github.com/paramiko/paramiko) and `subprocess.run` – Fred Feb 20 '19 at 11:50

2 Answers2

1

Try the following:

pip install paramiko

then in your script:

import base64
import paramiko
key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('ssh.example.com', username='strongbad', password='thecheat')

def run_command(command)
    stdin, stdout, stderr = client.exec_command(command)
    for line in stdout:
        print('... ' + line.strip('\n'))
    return True

run_command('ls')
run_command('cd..')
run_command('apt-get update')


client.close()
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
1

You can use Netmiko or NAPALM.

These two python libraries support almost all different vendor devices.

https://napalm.readthedocs.io/en/latest/index.html

https://pynet.twb-tech.com/blog/automation/netmiko.html

alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
Vijay Shetty
  • 901
  • 10
  • 11