python 获取本机环境信息
一、函数
1.socket.gethostname():不带任何参数,返回一个字符串(主机名),通常不完整。比如csm.example.com 只会返回csm
2.socket.getfqdn():带一个参数,返回完整主机名
二、代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import sys,socketdef getipaddrs(hostname): """Given a host name,perform a standard (forward) lookup and return a list of ip addressfor that host.""" result=socket.getaddrinfo(hostname,None,0,socket.SOCK_STREAM) return [x[4][0] for x in result]#calling gethostname() returns the name of the local machinehostname=socket.gethostname()print "hostname is:",hostname#try to get the fully qualified name:print "Fully_qualified name:",socket.getfqdn(hostname)try: print "IP address:", ", ".join(getipaddrs(hostname))except socket.gaierror,e: print "error" |
三、执行结果
1 2 3 4 | [root@csm testpython]# python host.pyhostname is: csmFully_qualified name: dragoneyes-PC.workgroupIP address: 192.168.155.9 |
目录 返回
首页