下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
python shell 根据 ip 获取 hostname 或根据 hostname 获取 ip
前言
方法1:利用 socket 模块 里的 gethostbyname 函数
>>> import socket >>> socket.gethostbyname("www.baidu.com") '61.135.169.125' >>> socket.gethostbyname("rs.xidian.edu.cn") '202.117.119.1'
方法2 利用 shell 中 hostname 命令
def getHostName(ip): command = 'java -jar %s %s "hostname > %s.hostname"' %(remoteCmdLoca,ip,ip) result = subprocess.call(command,shell=True) command = '%s -q -r -pw passwd %s [email protected]%s:/root' % (pscpLoca,pscpLoca,shell=True) command = '%s -q -r -pw passwd [email protected]%s:/root/%s.hostname %s' %(pscpLoca,fileDir) result = subprocess.call(command,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
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。