Python to obtain the command line output result, python command line

Source: Internet
Author: User

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>

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.