linux 修改网卡配置(修改本地文件)

首先:

查看服务器版本,如果是centos,查看命令是:

cat /etc/centos-release

返回结果,如:

CentOS Linux release 7.5.1804 (Core) 

1.找到网卡

2.代码读取:

(1)工具类

public class PropertyUtil {

    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
    private static Properties props=null;

    synchronized static public HashMap<String, String> loadProps(String prop){
        HashMap<String, String> map = new HashMap<>();
        props = new Properties();

        try {
            props.load(loadConfig(prop));//读取文件
        } catch (Exception e) {
            e.printStackTrace();
        }

        for(Object i:props.keySet()){
            map.put((String)i,props.getProperty((String)i));
        }
        return map;
    }

    //读取文件
    public  static InputStream loadConfig(String  filename){
        try {
            InputStream ins;
            String filePath =filename;
            if (new File(filePath).exists()) {
                System.err.println("Load Config file"+filePath);
                ins = new BufferedInputStream(new FileInputStream(filePath));
            } else {
                System.err.println("Load Config file"+filename);
                // System.out.println(PropertyUtil.class.get);
                ins = PropertyUtil.class.getResourceAsStream(filename);
            }
            return  ins;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //修改文件
    public static void writeNetWorkCard(Map<String, String> netMap) {
        String profilepath ="/etc/sysconfig/network-scripts/ifcfg-eno1";
        logger.info("修改properties文件内容.......");
        try {

            for(String keyname : netMap.keySet()){
                String keyvalue = netMap.get(keyname);
                OutputStream fos = new FileOutputStream(profilepath);
                props.setProperty(keyname, keyvalue);
                props.store(fos, null);
                fos.close();
            }
        } catch (IOException e) {
            System.err.println("属性文件更新错误");
        }
    }
}

获取和修改网卡-配置调用代码:

(1)controller:

HashMap<String, String> ifcfgMap=sysService.getNetWorkCard();

把这个ifcfgmap return给前端。

(3)service:

    /**
     * 获取网卡信息
     */
    public HashMap<String, String> getNetWorkCard(){
        String profilepath="/etc/sysconfig/network-scripts/ifcfg-eno1";
        HashMap<String, String> map= PropertyUtil.loadProps(profilepath);
        return  map;
    }

    /**
     * 修改网卡信息
     */

    public int updateNetWorkCard(Map<String, String> maps){

        //1.修改
        PropertyUtil.writeNetWorkCard(maps);
        //2.执行重启网卡命令
        String order="service network restart";
        execCmd(order);
        return  0;
    }


    /**
     * cmd执行命令
     */
    private String  execCmd(String cmd){
        StringBuilder sb=new StringBuilder();
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line="";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        }
        catch(Exception e1) {}
        return  sb.toString();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我先来一碗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值