All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.afu.utils.os.linux.FirewallCmd Maven / Gradle / Ivy

There is a newer version: 0.0.55-RELEASE
Show newest version
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);
    }




}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy