python linux下IP操作函数
2015-09-02 17:07
573 查看
import socket import fcntl import struct def get_ip_address(ifname='eth0'): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ipaddr = socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]))[20:24]) return ipaddr def get_netmask(ifname='eth0'): return socket.inet_ntoa(fcntl.ioctl( socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 35099, struct.pack('256s', ifname))[20:24]) def get_default_gateway_linux(): with open("/proc/net/route") as fh: for line in fh: fields = line.strip().split() if fields[1] != '00000000' or not int(fields[3], 16) & 2: continue return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16))) def get_dns(dns_number="1"): counter = 0 with open("/etc/resolv.conf") as f: content = f.readlines() for line in content: if "nameserver " in line: counter= counter+1 if (str(counter) == dns_number): return line[11:len(line)-1];
相关文章推荐
- centos6.5搭建流媒体服务器
- linux for循环
- linux-pam
- 【Linux命令详解】8、Linux其他命令―(tty、startx、type)
- 如何通过SSH反向隧道,访问NAT后面的Linux服务器?
- linux rc.sysinit文件详解
- Linux启动过程详解(inittab、rc.sysinit、rcX.d、rc.local)
- Linux软件管理——yum命令详解
- 在Linux下查看环境变量
- linux学习笔记0.2---gstreamer命令
- centos安装pil,windows64位pil,及导入字库报错问题
- 打通windows和Linux下的传输问题解决只能使用SSH协议下的22端口来传输文件(Openssh for windows)
- Linux脚本-chmod
- linux之间建立信任关系
- Linux VFS文件系统之创建文件
- no terminal colors in Linux
- Linux jstack分析cpu占用100%
- linux 安装tree命令,非yum安装方式
- Linux 学习笔记3----Gstreamer 编写播放mp3程序。
- linux 下svn服务器配置步骤