H3C Switch python command issued script

Source: Internet
Author: User

The new era of network engineering ability to become more important than the ability to knock command, the big tide has been unable to change. I've been working on Python's Netmiko as an SSH method recently. Share the first script and let's do some research.

The direction is simple, through the Python open method reads the IP address inside the iplist.txt, these IP is the switch IP address which the command needs to be issued. Then read what commands are required to be issued in the cmdlist. Connect to the switch automatically via Python's Netmiko ssh

h3c_check.py

Coding:utf-8

Import time
From Netmiko Import *
Import Sys
Import OS
Import Getpass

#读取iplist the IP address inside the. txt document
IPList = open (' Iplist.txt ', ' r ', encoding = ' ASCII ')
IP_ADDR = Iplist.readlines ()
Iplist.close ()

cmdlist = open (' Cmdlist.txt ', ' r ', encoding = ' ASCII ')
Cmd_line = Cmdlist.readlines ()
Cmdlist.close ()

#遍历ip_addr这个列表里面的ip进行连接
For IP in ITER (IP_ADDR):
Print (IP)
Try
S5130 = {
' Device_type ': ' Hp_comware ',
' IP ': IP,
' username ': ' h3c ',
' Password ': ' H3C ',
}

    net_connect = ConnectHandler(**S5130)    for cmd in iter(cmd_line):        cmd_result = net_connect.send_command(cmd)        print(‘-----------------------------------------------------------------‘)        print(cmd)        print(cmd_result)        print(‘-----------------------------------------------------------------‘)        print(‘‘)    net_connect.disconnect()except (EOFError, NetMikoTimeoutException):    print(‘Can not connect to Device‘)except (EOFError, NetMikoAuthenticationException):    print(‘username/password wrong!‘)

Iplist.txt

192.168.11.112
192.168.11.110

Cmdlist.txt

Display VLAN
Display version
Display IP Inter Brief

H3C Switch python command issued script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.