首页 > 代码库 > 获取主机信息(python)
获取主机信息(python)
#!/usr/bin/env python from subprocess import Popen,PIPE import urllib,urllib2 import shlex,re def getIfconfig(): p = Popen([‘ifconfig‘],stdout=PIPE) return p.stdout.read().split(‘\n\n‘) def parseIfconfig(data): macaddr = data.split(‘\n‘)[0].split()[-1] ipaddr = data.split(‘\n‘)[1].split()[1].split(‘:‘)[1] return macaddr,ipaddr for 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 = s def get_uuid(): data = {} p = Popen([‘dmidecode‘],stdout=PIPE) s = p.stdout.readlines() a = [] line_in = False for 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 break vendor = [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 data def get_version(): data = {} 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 data def get_hostname(): data = {} cmd = ‘hostname -f‘ p = Popen(shlex.split(cmd), stdout=PIPE) s = p.stdout.readline().strip(‘\n‘) data[‘hostname‘] = s return data def get_uname(): data = {} 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‘] = os return data
本文出自 “muzinan的技术博客” 博客,谢绝转载!
获取主机信息(python)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。