Java set linux extranet egress IP

Source: Internet
Author: User
Tags get ip

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

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.