Python 获取主机ip与hostname
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
->基础环境
Linux:ubuntu 16.04
Python ; 2.7
->修改hostname
1:$sudo hostname 2tong-slavetwo
2:$sudo vi /etc/hostname
2tong-slavetwo
3:$sudo vi /etc/hosts
127.0.0.1 localhost 2tong-slavetwo
127.0.1.1 2tong-slavetwo
->Python 环境下输出ip hostname
>>>import socket
>>>hostname = socket.gethostname()
>>>print hostname
2tong-slavetwo
>>>ip = socket.gethostbyname(hostname)
>>>print ip
127.0.0.1
>>>ipList = socket.gethostbyname_ex(name)
>>>print ipList
('localhost', ['2tong-slavetwo', '2tong-slavetwo'], ['127.0.0.1', '127.0.1.1']
->札记
import socket后,会从/etc/hostname中寻找hostname,而后前往/etc/hosts中查找ip
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献2条内容
所有评论(0)