Python suitable for network siege lion learning——split() and join() in actual combat

Split (), join (), input (), the principle and usage of the list here will not repeat the
split (), join (), input (), list: principle and usage


This article mainly describes how the four functions are used in the actual network operation and maintenance environment.

Girl, I want to help you carry the switch!

Insert picture description here

Application scenario

In large and medium-sized companies, the division of IP addresses is generally regular. For example, a company has a 10-story building, and the IP subnet on the first floor is 192.168.1.0/24, and the IP subnet on the second floor is 192.168. .2.0/24, the third floor is 192.168.3.0/24, and so on. Now you need to make a small program that allows the user to enter any IP address belonging to the company's intranet, and then let the Python script tell the user which floor the IP address belongs to.

The idea is as follows

Because the first segment of the company's intranet IP address is 192, the second segment is 168, and the fourth segment does not affect our judgment of the floor regardless of whether the user enters any IP address. In other words, we can only judge which floor is from the third segment of the IP address, but how do we tell Python to determine which number belongs to the third segment of the IP address? Then we can use split( ) Convert the IP address (character string) entered by the user into a list, and then point to the third segment of the IP address through the index of the list. The code is as follows.

Insert picture description here192.108.1.0 Assign but to the variable floor, then call the split() method on the variable, and assign the return value to another variable floor_list. Note that the "." in the split() brackets represents the separator, which is used to slice the string. Because the IP address is written with 4 numbers separated by 3 ".", so the separator here is ".". After that, we can query the third element of the list by indexing floor1_list[2] to get the number in the third paragraph of the IP address, which is the number 1 here, which represents the first floor.

python script

After knowing how to use split() to obtain the number in the third paragraph of the IP address, return to the previous requirement: let the user enter any IP address belonging to the company's intranet, and then let Python tell the user which IP address belongs to Floors. The script code is as follows:

# coding=utf-8
ip=input('请输入要查询的IP地址:')
ip_list=ip.split('.')
print('该IP地址属于'+ip_list[2]+'楼')

Insert picture description hereInsert picture description here

join()

A simple command for configuring an IP address on a Huawei device interface
Insert picture description here
is missing a newline character (Enter key) at the end of each command. Use the join() function to add \n to the end of each command.
Insert picture description here
Add line feed (enter key) to match the command into the device.

Guess you like

Origin blog.csdn.net/weixin_44309905/article/details/114916885