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 ]