1.获取内网地址(私有地址)

linux下获得本机的取得内网和外网地址,linux获取

1.赢得内网地址(私有地址)

ifconfig -a

2.收获外网地址(公网地址)

curl members.3322.org/dyndns/getip

 

Linux下Python获取IP地址的代码,linux下获得本机的拿走内网和外网地址。
1.获得内网地址(私有地址) ifconfig -a 2.获得外网地址(公网地址) curl
members.3322.org/dyndns/geti…

获取IP方式,获取ip

澳门金沙国际 ,桥接:局域网/路由器内获取IP,IP地址产生变化,

NAT格局恐怕仅主机方式(Host-only):把本机电脑当做转换机去获取IP,无法禁止采用,IP不会更换,

互连网—属性—更改适配器设置—

VMnet1–>Hostonly(不能够上国财经高校网)

VMnet8–>NAT

 

桥接:局域网/路由器内获取IP,IP地址产生变化,
NAT情势或然仅主机情势(Host-only):把本机电脑当做转换机去取得I…

Linux下Python获取IP地址的代码,linuxpython获取ip

《lnmp一键安装包》中须要取得ip地址,有2种状态:假若服务器唯有私网地址没有公网地址,这些时候取得的IP(即私网地址)无法用来判断服务器的岗位,于是取其网关地址用来判定服务器在国内依旧外国(脚本为了使国内用户飞快下载,yum源自动设置成163,这一个景况就必要取得网关地址);如若服务器有公网地址,那时获取的IP地址可用来间接判断服务器地理地方。

得到服务器IP,如若有公网地址就取公网地址,没有公网地址就取私网网址
上面是从前自身用shell来得到当地IP脚本:

IP=`ifconfig | grep 'inet addr:' | cut -d: -f2 | grep -v ^10\. | grep -v ^192\.168 | grep -v ^172\. | \
grep -v ^127\. | awk '{print $1}' | awk '{print;exit}'`

[ ! -n "$IP" ] && IP=`ifconfig | grep 'inet addr:' | cut -d: -f2 | grep -v ^127\. | \
awk '{print $1}' | awk '{print;exit}'`

Python版:get_local_ip.py:

#!/usr/bin/env python
import socket
def Get_local_ip():
 """
 Returns the actual ip of the local machine.
 This code figures out what source address would be used if some traffic
 were to be sent out to some well known address on the Internet. In this
 case, a Google DNS server is used, but the specific address does not
 matter much. No traffic is actually sent.
 """
 try:
  csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  csock.connect(('8.8.8.8', 80))
  (addr, port) = csock.getsockname()
  csock.close()
  return addr
 except socket.error:
  return "127.0.0.1"

if __name__ == "__main__":
 local_IP = Get_local_ip() 
 print local_IP

有公网地址直接得到,没有公网地址就收获网关地址(用于判断IP地址的地理地点):get_public_ip.py

#!/usr/bin/env python
import re,urllib2
class Get_public_ip:
 def getip(self):
  try:
   myip = self.visit("http://www.whereismyip.com/")
  except:
   try:
    myip = self.visit("http://www.ip138.com/ip2city.asp")
   except:
    myip = "So sorry!!!"
  return myip
 def visit(self,url):
  opener = urllib2.urlopen(url)
  if url == opener.geturl():
   str = opener.read()
  return re.search('\d+\.\d+\.\d+\.\d+',str).group(0)

if __name__ == "__main__":
 getmyip = Get_public_ip()
 print getmyip.getip()

判定服务器IP地理地点: get_ip_area.py

#!/usr/bin/env python
#coding:utf-8
try:
 import sys,urllib2,json
 apiurl = "http://ip.taobao.com/service/getIpInfo.php?ip=%s" % sys.argv[1] 
 content = urllib2.urlopen(apiurl).read()
 data = json.loads(content)['data']
 code = json.loads(content)['code']
 if code == 0:
  print data['country_id']
 else:
  print data
except:
 print "Usage:%s IP" % sys.argv[0]

《lnmp一键安装包》中供给获得ip地址,有2种情景:
假设服务器唯有私网地址没有公网地址 ,…

ifconfig -a

2.取得外网地址(公网地址)

curl members.3322.org/dyndns/getip

 

相关文章