python获取linux本机IP
#!/usr/bin/env python
#encoding: utf-8
#description: get local ip address import os
import socket, fcntl, struct def get_ip():
#注意外围使用双引号而非单引号,并且假设默认是第一个网卡,特殊环境请适当修改代码
out = os.popen("ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}' | head -1").read()
print out #另一种方法, 只需要指定网卡接口, 我更倾向于这个方法
def get_ip2(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24]) if __name__ == '__main__':
get_ip()
print get_ip2('eth0')
print get_ip2('lo')
python获取linux本机IP的更多相关文章
- 使用 python 获取 Linux 的 IP 信息(通过 ifconfig 命令)
我们可以使用 python 代码通过调用 ifconfig 命令来获取 Linux 主机的 IP 相关信息,包括:网卡名称.MAC地址.IP地址等. 第一种实现方式: #!/usr/bin/pytho ...
- Linux 项目 shell 自动获取报告本机IP (1) | 通过shell 自动获取报告本机IP
由于电脑设置静态IP经常出现链接不上网络,动态IP又非常不方便,故有了这个想法并实现 原理: Linux,包含PC机器,树莓派等,通过shell 自动获取报告本机IP | 通过 Mutt+Msmtp ...
- 获取Linux下的IP地址 java代码
/** * 获取Linux下的IP地址 * * @return IP地址 * @throws SocketException */ public static String getLinuxLocal ...
- python获取到本机的公网IP
5行代码获取到本机的公网IP from urllib.request import urlopen import re text = str(urlopen("http://txt.go.s ...
- 用Python获取Linux资源信息的三种方法
方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeReso ...
- 使用Python获取计算机名,ip地址,mac地址等等
获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...
- 使用 python 获取 Linux 系统信息(通过dmidecode命令)
通过 dmidecode 命令可以获取到 Linux 系统的包括 BIOS. CPU.内存等系统的硬件信息,这里使用 python 代码来通过调用 dmidecode 命令来获取 Linux 必要的系 ...
- 使用Python获取Linux系统的各种信息
哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码 ...
- 使用 Python 获取 Linux 系统信息
探索platform模块 platform模块在标准库中,它有很多运行我们获得众多系统信息的函数.让我们运行Python解释器来探索它们中的一些函数,那就从platform.uname()函数开始吧: ...
随机推荐
- I wrote a JSONHelper extension
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonso ...
- 【POJ 1719】 Shooting Contest (二分图匹配)
题目链接 把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了. #include <cstdio> #include <cstring> #include <algo ...
- bzoj 2668 费用流
我们可以把初始状态转化为目标状态这一约束转化为将黑子移动到目标状态所需要的最少步数. 除了初始点和目标点之外,剩下的点如果被经过那么就会被交换两次,所以我们将一个点拆成3个点,a,b,c,新建附加源点 ...
- IOS工程自动打包并发布脚本实现
http://blog.csdn.net/ccf0703/article/details/8588667 文章首发地址:http://webfrogs.me/2013/02/18/ios-automa ...
- UVALIVE 3562 Remember the A La Mode!
费用流 建图很简单直接上代码 #include <map> #include <set> #include <list> #include <cmath> ...
- DRF视图集的使用
# 原创,转载请留言联系 如果要把同一种http请求方法的多个接口放在同一个视图中,比如:查询多条数据和一条数据都是get请求,放在同一个视图里,应该怎么做呢??? 如果直接放在一起,会怎样呢? # ...
- vim操作大全
# 转自 https://blog.csdn.net/weixin_37657720/article/details/80645991 曾经使用了两年多的Vim,手册也翻过一遍.虽然现在不怎么用vim ...
- 带着问题学git
序 作为git新手,常见的git clone,push,commit命令已经足够完成一次代码的发布,但是如果不幸碰到问题往往会束手无策,利用网络问答解决了之后也不知其所以然.所以,做一次好奇宝宝吧! ...
- 提取windows用户明文密码
前段时间mimikatz热传,主要是因为可以直接提取当前登录用户明文密码. 其实,有个更厉害的神器,无需那么多命令操作,一个命令搞定: C:\>wce -w WCE v1.3beta (Wind ...
- HDU 多校1.2
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...