Be familiar with Python's method of segmenting and obtaining information on character text.
Article structure:
(1) The information of ifconfig command output is analyzed.
(2) in two ways to analyze the output to obtain the system's IP information, including: Network card name, IP address and MAC address
1. Ifconfig command Output Information analysis
First:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/89/43/wKioL1gOBceAJ5P-AADYHlDS9MM795.png "title=" Ifconfig.png "alt=" Wkiol1gobceaj5p-aadyhlds9mm795.png "/>
For example, we need to count the three information is the network card name, eth0,eth0:1,eth1 the three network card, as for the ' Lo ' network card will not be counted. For each NIC, you also need to record the MAC address and IP address.
See the above information, it is the information of each network card as a paragraph, and all the card at the top of the line to start, the paragraph is replaced by a blank line. According to these characteristics, the whole piece of information of each NIC can be used as an object of analysis. You can usually split all characters and save them as a list of elements.
There is another common approach, because not every system's ifconfig command output will be separated by a blank line as a paragraph, so the method mentioned above is not appropriate. The output of all ifconfig is judged by line, then the information of each NIC is formed into an element, and finally the information of the three net card is saved as a list.
2. Here are two ways to implement this:
Method (1):
#!/usr/bin/env pythonfrom subprocess import popen, pipedef getdata (): p = popen ([' Ifconfig '], stdout=pipe, stderr=pipe) data = p.stdout.read (). Split ("\ n") return [i for i in data if i and not i.startswith (' lo ')] def parsedata (data): dic = {} for lines in data : devname = lines.split (' \ n ') [0].split () [0] ipaddr = lines.split (' \ n ') [1].split () [1].split (': ') [1] macaddr = lines.split (' \ n ') [0].split () [ -1] dic[devname ] = [ipaddr,&nBsp; macaddr] return dic if __name__ == "__main__": data = getdata () print parsedata (data)
Validation results:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/46/wKiom1gOEAnDUDX-AAAjp7U_7-4540.png "title=" 13_ Ip.png "alt=" Wkiom1goeandudx-aaajp7u_7-4540.png "/>
In the GetData () function, use "\ n" to P.stdout.read () read out the string is divided, basically is a list of the information stored in the network card, and then use the column to generate the empty element and the ' lo ' NIC elements removed.
In the Parsedata (data) function, each time the for loop gets the entire piece of information for a net card, and then divides the whole piece of information with ' \ n ', it can be directly judged.
Finally, the results are saved in the dictionary, using the network card masterpiece as the Dictionary key, the IP address and the MAC address list as value.
Method (2):
#!/usr/bin/env python from subprocess import Popen, pipedef getip (): p = popen ([' Ifconfig '], stdout= Pipe, stderr=pipe) stdout, stderr = p.communicate () return [i for i in stdout.split (' \ n ') if i] def genip (data): lines = [] new_line = ' for line in data: if line[0].strip (): lines.append (New_line) new_line = line + ' \ n ' &nbSp;else: new_line += line + ' \ n ' lines.append (new_line) lines = [i for i in lines if i and not i.starswith (' lo ')] return lines def parsedata (data): dic = {} for lines in data: devname = lines.split (' \ n ') [0].split () [0] macaddr = lines.split (' \ n ') [0].split () [-1] ipaddr = lines.split (' \ n ') [1].split () [1].split (': ') [1] dic[devname] = [ipaddr, macaddr] return dic if __name__ == "__main__": data = getip () data_list = genip (data) print parsedata (Data_list)
Output Result:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/46/wKiom1gOGNvAKrhYAAAqvQCZVfs076.png "title=" Ipnew.png "alt=" Wkiom1gognvakrhyaaaqvqczvfs076.png "/>
In the GetIP () function, simply split the string output from the ifconfig command with ' \ n ' and save each row to the list.
The list returned by GetIP () is then processed in the GENIP (data) function, primarily to return one to the entire segment of the net card
Information as a list of elements. Also in determining whether the string is shelf, using the Boolean value of Line[0].strip (), if the character is still true after processing by the strip () method, then the line string is shelf, which is the beginning of the information paragraph of a net card. If it is a new net card information, then the string saved in the New_line, as an element added to the lines list to save. If Line[0].strip () is false, then this line is a space indentation, it is the information inside the network card paragraph, should be added to the New_line to save, and so on to read the next network card name of the line appears, and then add New_line to lines.
Finally, since the Genip () function has returned a list, each element of the table is the information of a net card, so it can be processed as a parsedata () in the method (1), and eventually a dictionary is returned. The result is the same as the method (1).
Summarize:
The method (1) is more concise and easy to understand, compared with the two methods of implementation. However, the implementation of the method (2) is more common, it is a string-by-line traversal, according to the information of the network card structure, and finally a net card information is integrated into a string new_line, here the method (2) and Method (1) began the same, you can use the Parsedata () function to do the final processing.
This article is from the "dayandnight" blog, make sure to keep this source http://hellocjq.blog.51cto.com/11336969/1865211
Python collects IP information