DEV Community

Amr ElHusseiny
Amr ElHusseiny

Posted on

3 1

Python's Netmiko template

The following is a ready to go netlike template , you just need to choose your way of authentication and the device type , you can find the supported devices on both the documentation and the Git for the Netmiko library :

Netmiko Source Code : Netmiko Github
API reference (must check for more functionality) : Netmiko Doc

# netmiko_starter.py 
from netmiko import ConnectHandler

device_dictionary = {
                    'device_type': 'DEVICE_TYPE',  
                    'host': 'DEVICE_IP_ADDRESS',
                    'username': 'USERNAME',
                    'password' : 'PASSWORD',
                    # 'verbose': False ,
                    # 'session_log': 'log.txt', 
                    # 'global_delay_factor': 2, 
                }
net_connect = ConnectHandler(**device_dictionary)
output = net_connect.send_command("show version")
net_connect.disconnect()
Enter fullscreen mode Exit fullscreen mode

Comments on the code :

  • uncomment verbose if you want to disable Netmiko's Logs .
  • uncomment session_log to debug the connection failures .
  • uncomment _ global_delay_factor_ to add delay when waiting for commands output , very useful if the devices are slow , note that this is not in seconds but in multiple , so "2" mean multiply the default delay by 2 and so on

For more functionality , like the types of devices supported , please check the API docs .

Also check the textfsm templates part of the guide , this helps a lot with parsing the output instead of using the normal Regex way , it supports a lot of well known devices outputs .

good luck

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay