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)