Python to obtain the command line output result, python command line
This example describes how to obtain the command line output result using Python. We will share this with you for your reference. The details are as follows:
Python gets the command line output results and filters the results to find what you need!
The following uses obtaining the MAC address and IP address of the local machine as an example!
# Coding: GB2312import OS, re # execute command, and return the outputdef execCmd (cmd): r = OS. popen (cmd) text = r. read () r. close () return text # write "data" to file-filenamedef writeFile (filename, data): f = open (filename, "w") f. write (data) f. close () # obtain the MAC address and IP address of the computer if _ name _ = '_ main _': cmd = "ipconfig/all" result = execCmd (cmd) pat1 = "Physical Address [\.] +: ([\ w-] +) "pat2 =" IP Address [\.] + :([\. \ d] +) "MAC = re. findall (pat1, result) [0] # Find mac ip = re. findall (pat2, result) [0] # Find IP print ("MAC = % s, IP = % s" % (MAC, IP ))
Running result:
E: \ Program \ Python> del. pyMAC = 00-1B-77-CD-62-2B, IP = 192.168.1.110E: \ Program \ Python>