#!/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的更多相关文章

  1. 使用 python 获取 Linux 的 IP 信息(通过 ifconfig 命令)

    我们可以使用 python 代码通过调用 ifconfig 命令来获取 Linux 主机的 IP 相关信息,包括:网卡名称.MAC地址.IP地址等. 第一种实现方式: #!/usr/bin/pytho ...

  2. Linux 项目 shell 自动获取报告本机IP (1) | 通过shell 自动获取报告本机IP

    由于电脑设置静态IP经常出现链接不上网络,动态IP又非常不方便,故有了这个想法并实现 原理: Linux,包含PC机器,树莓派等,通过shell 自动获取报告本机IP  | 通过 Mutt+Msmtp ...

  3. 获取Linux下的IP地址 java代码

    /** * 获取Linux下的IP地址 * * @return IP地址 * @throws SocketException */ public static String getLinuxLocal ...

  4. python获取到本机的公网IP

    5行代码获取到本机的公网IP from urllib.request import urlopen import re text = str(urlopen("http://txt.go.s ...

  5. 用Python获取Linux资源信息的三种方法

    方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeReso ...

  6. 使用Python获取计算机名,ip地址,mac地址等等

    获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...

  7. 使用 python 获取 Linux 系统信息(通过dmidecode命令)

    通过 dmidecode 命令可以获取到 Linux 系统的包括 BIOS. CPU.内存等系统的硬件信息,这里使用 python 代码来通过调用 dmidecode 命令来获取 Linux 必要的系 ...

  8. 使用Python获取Linux系统的各种信息

    哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码 ...

  9. 使用 Python 获取 Linux 系统信息

    探索platform模块 platform模块在标准库中,它有很多运行我们获得众多系统信息的函数.让我们运行Python解释器来探索它们中的一些函数,那就从platform.uname()函数开始吧: ...

随机推荐

  1. HDU 1141 Factstone Benchmark (数学 )

    题目链接 Problem Description Amtel has announced that it will release a 128-bit computer chip by 2010, a ...

  2. 关于集合的size的操作

    1.创建集合: 创建指定大小的集合:(大小为5) db.createCollection(}) 2.插入五条数据: > db.colle1.insert({name:}) WriteResult ...

  3. Linux中短横线(-)小记

    在Linux中短横线(-)可以表示输出流,具体用法如下. 搭配cat cat - 如果指定cat的文件为-,表示从标准输入读取(和直接使用cat,好像没什么区别) 搭配| echo 123 | cat ...

  4. LeetCode 4 Median of Two Sorted Array

    There  are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  5. [TJOI2007]segment

    题目描述 在一个 n*n 的平面上,在每一行中有一条线段,第 i 行的线段的左端点是(i, L(i)),右端点是(i, R(i)),其中 1 ≤ L(i) ≤ R(i) ≤ n. 你从(1, 1)点出 ...

  6. delphi.memory.分配及释放---New/Dispose, GetMem/FreeMem及其它函数的区别与相同,内存分配函数

    来自:http://www.cnblogs.com/qiusl/p/4028437.html?utm_source=tuicool&utm_medium=referral ---------- ...

  7. kafka 分区数

    Kafka的分区,相当于把一个Topic再细分成了多个通道(对应 多个线程) 部署的时候尽量做到一个消费者(线程)对应一个分区. 如何确定Kafka的分区数,key和consumer线程数,以及不消费 ...

  8. MySQL 基础内容

    创建数据库 对于表的操作需要先进入库 use 库名: -- 创建一个名为 inana_db 的数据库,数据库字符编码指定为 utf8create database inana_db character ...

  9. AC日记——平衡树练习 codevs 4244

    4244 平衡树练习 思路: 有节操的人不用set也不用map: 代码: #include <cstdio> #include <cstring> #include <i ...

  10. [libgdx游戏开发教程]使用Libgdx进行游戏开发(4)-素材管理

    游戏中总是有大量的图像资源,我们通常的做法是把要用的图片做成图片集,这样做的好处就不多说了.直接来看怎么用. 这里我们使用自己的类Assets来管理它们,让这个工具类作为我们的资源管家,从而可以在任何 ...