In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the python SSH remote login device how to achieve the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python SSH remote login device how to achieve the article will have a harvest, let's take a look at it.
There are two main modules that support SSH protocol in python: Paramiko and netmiko. Netmiko module is used in this experiment.
The netmikko module is a third-party module of python and needs to be downloaded and installed using pip. (in the case of networking)
After downloading, enter the Python 3.10 interpreter. If import netmiko does not report an error, it means that Netmiko is installed successfully.
Experimental topology
Bridge the LSW1 of ensp with the local virtual network card loopback0, and simulate bridging your computer to the following topology network.
Experimental purpose:
Log in to the four switches LSW2-LSW5 one by one through the SSH of the netmiko module and configure STP for them one by one. The default mode is MSTP.
Experimental script
Complete code
From netmiko import ConnectHandlerfor i in range (20jue 24): SW= {'device_type':'huawei',' ip':'10.0.1.'+str (I), 'username':'python',' password':'123456' } connect=ConnectHandler (* * SW) print ('\ nlogin to switch'+ SW ['ip'] +' -') config_commands= ['stp mode stp','dis stp | include CIST Global Info'] output=connect.send_config_set (config_commands) print (output) connect.save_config ()
1. Import its link library function ConnectHandler () from the netmiko module to enable SSH login to network devices.
From netmiko import ConnectHandler
two。 Create a dictionary SW that contains four required values: 'device_type',' 'ip',' username', and 'password'.' Device_type' is used to specify the device type. Here we use Huawei's device, so 'device_type'' is' huawei''.
For i in range (20jue 24): SW= {'device_type':'huawei',' ip':'10.0.1.'+str (I), 'username':'python',' password':'123456',}
Since we are going to land four devices here, we need to use the for loop.
3. Call the ConnectHandler () function, borrow the for loop, SSH the dictionary SW, assign it to the variable connect, and note that the * * in front of the SW2 is identified.
Connect=ConnectHandler (* * SW)
Add'* 'and' * 'before the parameter in Python to indicate the dynamic parameter.
Adding'*'to the parameter indicates that multiple real parameter values can be stored in the array.
Adding'* 'before the parameter indicates that the parameter is accepted to be converted to a dictionary type.
4. If SSH login is successful, you will be prompted to log in successfully
Print ('\ nlog in to the switch successfully'+ SW ['ip'] +' -')
5. Create a list called config_commands as parameters with elements of commands that need to be executed on the switch.
Config_commands= ['stp mode stp','dis stp | include CIST Global Info']
6. Then, with the list of config_commands you just created as a parameter, call the send_config_set () function of ConnectHandler () to configure the SW using the above command and print out the configuration process.
Output=connect.send_config_set (config_commands) print (output)
7. Finally, save the device configuration with save_config ()
Connect.save_config ()
Experimental results
As can be seen from the above results, netmiko has entered additional inputs: system view and return for us.
Code optimization
If there are a large number of switches whose management ip addresses are not regular, it is no longer flexible and flexible to log in to the device directly with the for Loop Management ip address. At this point, we can put the ip addresses of all switches into a file called ip_list, and log in to the device one by one by reading each line of ip address in the file through the for loop readlines ().
Complete script
From netmiko import ConnectHandlerwith open ('ip_list.txt') as f: for line in f.readlines (): host=line.strip () SW= {' device_type':'huawei', 'ip':host, # host is a variable Don't be careless with quotation marks' username':'python', 'password':'123456' } connect=ConnectHandler (* * SW) print ('\ nlog in to the switch successfully'+ SW ['ip'] +' -') config_commands= ['stp mode stp' 'dis stp | include CIST Global Info'] output=connect.send_config_set (config_commands) print (output) connect.save_config ()
Note: ip_list should be in the same folder as the python script.
Result
This is the end of the article on "how to implement SSH remote login devices in python". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to realize the SSH remote login device in python". If you still want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.