- 论坛徽章:
- 0
|
决定采用绑定IP的办法来做,代码如下:
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;
public class VIPTestMain {
public static void main(String args[])throws Exception{
addVirtualIP("10.80.1.218");
Thread.sleep(5000);
delVirtualIP("10.80.1.218");
}
/**
* TODO: Only linux and SunOS are supported.
*
* @param vip
* @throws Exception
*/
public static void addVirtualIP(String vip)throws Exception{
String ethernetCardDeviceName = getEthernetCardDeviceName();
System.out.println("ethernetCardDeviceName="+ethernetCardDeviceName);
//
if(ethernetCardDeviceName==null){
return;
}
String os_name = System.getProperty("os.name").toLowerCase();
if(os_name.indexOf("sun")>=0 || os_name.indexOf("solaris")>=0){
String command1 = "ifconfig "+ethernetCardDeviceName.trim()+":1 plumb";
String command2 = "ifconfig "+ethernetCardDeviceName.trim()+":1 "+vip+" up";
System.out.println("command1="+command1);
System.out.println("command2="+command2);
Runtime.getRuntime().exec(command1);
Runtime.getRuntime().exec(command2);
}else if(os_name.indexOf("linux")>=0){
String command = "ip addr add "+vip+"/32 dev "+ethernetCardDeviceName;
System.out.println("command="+command);
Runtime.getRuntime().exec(command);
}
}
/**
* TODO: Only linux and SunOS are supported.
* @param vip
* @throws Exception
*/
public static void delVirtualIP(String vip)throws Exception{
String ethernetCardDeviceName = getEthernetCardDeviceName();
System.out.println("ethernetCardDeviceName="+ethernetCardDeviceName);
//
if(ethernetCardDeviceName==null){
return;
}
String os_name = System.getProperty("os.name").toLowerCase();
if(os_name.indexOf("sun")>=0 || os_name.indexOf("solaris")>=0){
String command = "ifconfig "+ethernetCardDeviceName.trim()+":1 unplumb";
System.out.println("command="+command);
Runtime.getRuntime().exec(command);
}else if(os_name.indexOf("linux")>=0){
String command = "ip addr del "+vip+"/32 dev "+ethernetCardDeviceName;
System.out.println("command="+command);
Runtime.getRuntime().exec(command);
}
}
/**
* TODO: Only linux and SunOS are supported.
* @return
* @throws Exception
*/
public static String getEthernetCardDeviceName()throws Exception{
String os_name = System.getProperty("os.name").toLowerCase();
String deviceName = null;
//
if(os_name.indexOf("sun")>=0 || os_name.indexOf("solaris")>=0){
String command = "ifconfig -a";
System.out.println("command="+command);
Vector v = executeCommand(command);
//
Iterator it = v.iterator();
while(it.hasNext()){
String item = it.next().toString();
if(item!=null){
item = item.trim().toLowerCase();
if(item.indexOf("loopback")<0 && item.indexOf("broadcast")>=0){
System.out.println("item="+item);
if(item.indexOf(":")>=0){
deviceName = item.substring(0,item.indexOf(":")).trim();
}
break;
}
}
}
}else if(os_name.indexOf("linux")>=0){
String command = "ip addr show";
Vector v = executeCommand(command);
//
/*String firstLine = v.get(0).toString().trim();
deviceName = firstLine.substring(0,firstLine.indexOf(" "));*/
Iterator it = v.iterator();
while(it.hasNext()){
String item = it.next().toString();
if(item!=null){
item = item.trim().toLowerCase();
if(item.indexOf("loopback")<0 && item.indexOf("broadcast")>=0){
System.out.println("item="+item);
String ss[] = item.split(":");
if(ss.length>=3){
deviceName = ss[1].trim();
}
break;
}
}
}
}
return deviceName;
}
public static Vector executeCommand(String command)throws Exception{
Process pro =Runtime.getRuntime().exec(command);
Vector v = new Vector();
//
InputStream in = pro.getInputStream();
InputStreamReader isr = new InputStreamReader(pro.getInputStream());
LineNumberReader reader = new LineNumberReader (isr);
String line = null;
while( (line=reader.readLine())!=null){
if(line!=null){
System.out.println("line="+line);
v.add(line);
}
}
System.out.println("=======================================");
reader.close();
isr.close();
in.close();
return v;
}
} |
|