2

我需要在 ubuntu 上配置网络接口。这是我的代码

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package epsetupserv.network;

    import epsetupapi.entity.NetworkEntity;
    import epsetupserv.config.GeneralConfigurator;
    import epsetupserv.lib.FileOperation;
    import epsetupserv.lib.ShellAccess;
    import java.util.ArrayList;
    import java.util.List;

    /**
     *
     * @author nununeh
     */
    public class NetworkConfigurator {

        private static String realPath = "/etc/network/";
        private static String realFile = "interface";

        public static List<NetworkEntity> getRealNetworkInterface() {
            List<NetworkEntity> list = new ArrayList<NetworkEntity>();

            int eth0_loc = FileOperation.findStringLine(realPath, realFile, "auto eth0");
            int eth1_loc = FileOperation.findStringLine(realPath, realFile, "auto eth1");
            int tline = FileOperation.countLine(realPath, realPath);

            GeneralConfigurator gn = new GeneralConfigurator(realPath, realFile);
            gn.setDelimiter(" ");

            NetworkEntity n1 = new NetworkEntity();
            n1.setAddress(gn.getProperty("address", eth0_loc, eth1_loc));
            n1.setNetmask(gn.getProperty("netmask", eth0_loc, eth1_loc));
            n1.setNetwork(gn.getProperty("network", eth0_loc, eth1_loc));
            n1.setBroadcast(gn.getProperty("broadcast", eth0_loc, eth1_loc));
            list.add(n1);

            NetworkEntity n2 = new NetworkEntity();
            n2.setAddress(gn.getProperty("address", eth1_loc, tline));
            n2.setNetmask(gn.getProperty("netmask", eth1_loc, tline));
            n2.setNetwork(gn.getProperty("network", eth1_loc, tline));
            n2.setBroadcast(gn.getProperty("broadcast", eth1_loc, tline));
            list.add(n2);

            return list;
        }


        public static void setRealNetworkInterface(NetworkEntity n1, NetworkEntity n2) {
            int tline = FileOperation.countLine(realPath, realFile);
            FileOperation.clearByLine(realPath, realFile, 1, tline);
            FileOperation.insertNewLine(realPath, realFile, ""
                    + "# Generated By ePanel\\nn"
                    + "auto lo\n"
                    + "iface lo inet loopback\n"
                    + "\n"
                    + "auto eth0\n"
                    + "iface eth0 inet static\n"
                    + " address " + n1.getAddress() + "\n"
                    + " netmask " + n1.getNetmask() + "\n"
                    + " network " + n1.getNetwork() + "\n"
                    + " broadcast " + n1.getBroadcast() + "\n"
                    + "\n"
                    + "auto eth1\n"
                    + "iface eth1 inet static\n"
                    + " address " + n2.getAddress() + "\n"
                    + " netmask " + n2.getNetmask() + "\n"
                    + " network " + n2.getNetwork() + "\n"
                    + " broadcast " + n2.getBroadcast() + "\n");
        }
    }

我使用班级时的问题,我无法一一更改属性。因此,如果我只想从 eth1 更改地址,我必须通过一个进程运行来更改所有地址(所有属性低于 eth0 和 eth1)。你有任何替代代码吗?

4

0 回答 0