import java.io.BufferedReader;
import java.io.InputStreamReader;
public class IP_Modifies {
public static void main(String[] args) throws Exception {
//新IP地址
String IPADDR="192.168.161.133";
//新子网掩码
String NETMASK="255.255.255.0";
//新网管地址
String GATEWAY="192.168.161.2";
//网络配置文件路径 注:需要给ifcfg-ens160 修改权限 chmod +x ifcfg-*
String netPath="/etc/sysconfig/network-scripts/ifcfg-ens160";
String stopNetwork ="systemctl stop NetworkManager.service && systemctl stop network.service";
String BOOTPROTO="sudo sed -i \'s/BOOTPROTO=dhcp/BOOTPROTO=static/\' "+netPath;
String addIPADDR="sudo sed -i \'/IPADDR/d\' "+netPath;
String addNETMASK="sudo sed -i \'/NETMASK/d\' "+netPath;
String addGATEWAY="sudo sed -i \'/GATEWAY/d\' "+netPath;
String IPADDRAdd="sudo echo "+"IPADDR="+IPADDR+" >> "+netPath;
String NETMASKAdd="sudo echo "+"NETMASK="+NETMASK+" >> "+netPath;
String GATEWAYAdd = "sudo echo "+"GATEWAY="+GATEWAY+" >> "+netPath;
String startNetwork="systemctl start network.service && systemctl start NetworkManager.service";
Process process = Runtime.getRuntime().exec(
new String[]{"/bin/sh","-c",stopNetwork+" && "
+BOOTPROTO+" && "
+addIPADDR+" && "
+addNETMASK+" && "
+addGATEWAY+" && "
+IPADDRAdd+" && "
+NETMASKAdd+" && "
+GATEWAYAdd+" && "
+startNetwork
},null,null);
process.waitFor();
process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ping -c 3 " + IPADDR},null,null);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
修改Centos7静态IP方法的Java工具类
最新推荐文章于 2024-08-24 21:35:37 发布