Recently, the crawler project was sealed, the server bound a number of IP, thinking how to use multiple IPs, think of the idea of setting up a proxy server, but for some reasons, so the use of dynamic change the server export IP to use the way multiple IP.
First get the server bound IP
Public static void getlocalips () {try { for (Enumeration<networkinterface> en = networkinterface.getnetworkinterfaces (); En.hasmoreelements ();) { Networkinterface intf = en.nextelement (); string name = intf.getname (); if (!name.contains ("Docker") && !name.contains ("Lo") { for ( Enumeration<inetaddress> enumipaddr = intf.getinetaddresses (); Enumipaddr.hasmoreelements ();) { inEtaddress inetaddress = enumipaddr.nextelement (); if (! Inetaddress.isloopbackaddress ()) { string ipaddress = inetaddress.gethostaddress (). toString (); if (! Ipaddress.contains ("::") && !ipaddress.contains ("0:0:") && ! Ipaddress.contains ("Fe80")) { localips.add ( IPAddress); } } } } } } catch (Socketexception ex) { SYSTEM.OUT.PRINTLN ("Get IP Address Exception"); ex.printstacktrace (); }}
Write a script that switches the Linux export IP
#!/bin/shiptables-t nat-i postrouting-o eth0-d 0.0.0.0/0-S 192.168.0.1-j SNAT--to-source $
Public static void setoutip () { for (String ip : Localips) { try { runtime.getruntime (). EXEC (ApplicationConfig.prop.getProperty ("Ipshell") + " "+ip" logger.info ("Set Export IP:" +IP); if (checkipcanused (null, null)) {// See if IP is available return ; } } catch (ioexception e) { logger.error (E.getmessage ()); } }}
Attached: How to view Linux export IP
Curl Icanhazip.comcurl ifconfig.mecurl curlmyip.comcurl ip.appspot.comcurl ipinfo.io/ipcurl ipecho.net/plaincurl www.trackip.net/i
This article by micro-son blog original, blog address: www.weare.net.cn or click Micro-Blog
Java set linux extranet egress IP