Get host information (python)

Source: Internet
Author: User

Tag:python    host information    

#!/usr/bin/env pythonfrom subprocess import popen,pipeimport urllib,urllib2import  shlex,redef getifconfig ():         p = popen ([' Ifconfig '],stdout=pipe)         return p.stdout.read (). Split (' \ n ') def parseifconfig (data):         macaddr = data.split ( ' \ n ') [0].split () [ -1]        ipaddr = data.split (' \ n ') [1]. Split () [1].split (': ') [1]        return macaddr,ipaddrfor i  in [i.strip ()  for i in getifconfig ()  if i and not  I.startswith (' lo ')]:s = {}s[' macaddr '] = parseifconfig (i) [0]s[' ipaddr '] =  Parseifconfig (i) [1]data = sdef get_uuid ():d ata = {}p = popen ([' Dmidecode '], Stdout=pipe) S = p.stdoUt.readlines () a = []line_in = falsefor i in s:         if i.startswith (' system information '):                 line_in = True                 continue         if line_in:                 a.append (I.strip ())                  if i.startswith (' \ n '):                         line_in =  False                      &nBsp;  breakvendor = [i for i in a if i.startswith (' Product ') ]sn = [i for i in a if i.startswith (' UUID: ')]data[' vendor '] =  vendor[0].split (':  ') [1]data[' SN '] = sn[0].split (':  ') [1]return datadef  Get_version ():d ata = {}number = re.compile (R ' \d.\d ', re. M) cmd =  ' cat /etc/issue '         p = popen ( Shlex.split (cmd),  stdout=pipe)         s =  P.stdout.readlines () [0:] #osver  = number.findall (s[0]) data[' osver '] = s[0].strip (' \ n ') Return datadef get_hostname ():d ata = {}cmd =  ' hostname -f '          p = popen (Shlex.split (cmd),  stdout=pipe)          s = p.stdout.readline (). strip (' \ n ') data[' hostname '] = sreturn datadef get_uname ():d ata = {}number =  Re.compile (R ' x\d+\_+\d\d ')         cmd =  ' uname -a '         p = popen (Shlex.split (cmd),  stdout=PIPE)          s = p.stdout.readlines () os = s[0].split () [0] System = number.findall (S[0]) data[' system '] = system[0]data[' OS '] = osreturn  data


This article from "Muzinan Technology blog" blog, declined reprint!

Get host information (python)

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.