这篇文章里我们主要分享了python中shell 根据 ip 获取 hostname 或根据 hostname 获取 ip的代码,具体介绍如下。
笔者有时候需要根据hostname获取ip
比如根据machine.company.com 获得ip 10.173.14.117
方法1:利用 socket 模块 里的 gethostbyname 函数
代码如下,使用socket模块
>>> import socket>>> socket.gethostbyname("mand, shell=True) fileName = fileDir + ip + '.hostname' readFile = open(fileName,'r') hostnameInfo = str(readFile.readline().strip('\n')) readFile.close() subprocess.call('rm '+ fileName, shell=True) print "=========%s hostname is %s========" %(ip,hostnameInfo) return hostnameInfo下面分享一则简单的windows下python 获取主机名的代码示例,我是win10系统,待会可以试试:
环境:windows10 64位 + python2.7
代码如下:
import sockethostName = socket.gethostname()运行结果如下:
>> import socket>>> hostName = socket.gethostname()>>> print hostNameLAPTOP-H7MGGAAT总结
以上就是本文关于python shell根据ip获取主机名代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
python自动裁剪图像代码分享
Python算法之图的遍历
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!