Linux Python Gets the code for the IP address

Source: Internet
Author: User
There are 2 situations in which you need to obtain an IP address in the LNMP one-click installation package: if the server only has a private network address and no public address, the IP (that is, the private network address) cannot be used to determine the location of the server, so take its gateway addressUsed to determine whether the server in the country or abroad (script in order to enable domestic users to quickly download, yum source automatically set to 163, this situation needs to obtain a gateway address); If the server has a public network address, the acquired IP address can be used to directly determine the location of the server.

Get the server IP, if there is a public address to take the public address, no public address to take the private network URL
Here's how I used the shell to get the local IP script:

ip= ' Ifconfig | grep ' inet addr: ' | Cut-d:-f2 | Grep-v ^10\. | Grep-v ^192\.168 | Grep-v ^172\. | \grep-v ^127\. | awk ' {print '} ' | awk ' {print;exit} ' [!-n ' $IP '] && ip= ' ifconfig | grep ' inet addr: ' | Cut-d:-f2 | Grep-v ^127\. | \awk ' {print $} ' | awk ' {print;exit} '

Python version: get_local_ip.py:

#!/usr/bin/env pythonimport socketdef get_local_ip (): "" Returns the actual IP of the local machine. This code figures out what source address would is used if some traffic were to be sent out to some well known address on The Internet. In this case, a Google DNS server was 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 ', ())  (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

Access to the public address directly, no public address to obtain the gateway address (used to determine the location of IP address):get_public_ip.py

#!/usr/bin/env pythonimport re,urllib2class 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 ()

Determine server IP location: get_ip_area.py

#!/usr/bin/env python#coding:utf-8try: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 dataexcept:print ' usage:%s IP '% sys.argv[0 ]
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.