io.afu.utils.os.linux.FirewallCmd Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
RffanLAB Utils For Many Way use
package io.afu.utils.os.linux;
import io.afu.common.exception.BaseException;
import io.afu.utils.os.RunComand;
/**
* @author RffanLAB.方露宇
*/
public class FirewallCmd {
/**
* 开启某个IP的端口访问
* @param port 端口
* @param accessIP 访问IP
* @throws BaseException 操作时的抛错
*/
public static void openPortFor(String port,String accessIP) throws BaseException{
String[] cmd = {
"firewall-cmd", "--permanent","--add-rich-rule=\"rule family=\"ipv4\"","source", "address=\""+accessIP+"\" port protocol=\"tcp\" port=\""+port+"\" accept\""
};
String out = RunComand.runCMD(cmd);
reloadFirewalld();
}
/**
* 关闭对某个IP的端口访问
* @param port 端口
* @param accessIP IP
* @throws BaseException 操作时的抛错
*/
public static void closeAccessForPort(String port,String accessIP) throws BaseException {
String[] cmd = {
"firewall-cmd", "--permanent","--remove-rich-rule=\"rule family=\"ipv4\"","source", "address=\""+accessIP+"\" port protocol=\"tcp\" port=\""+port+"\" accept\""
};
String out = RunComand.runCMD(cmd);
reloadFirewalld();
}
/**
* 重新加载一下防火墙配置
* @throws BaseException 运行防火墙抛错
*/
public static void reloadFirewalld() throws BaseException {
String[] cmd = {
"firewall-cmd",
"--reload"
};
String out = RunComand.runCMD(cmd);
}
}