NETCore execution Centos Shell to modify the IP information system

Original: NETCORE perform Shell to modify the IP information system Centos

shell code

First find command to find the / etc / sysconfig / ifcfg-en * at the network-scripts / directory file, then sort ordering, the first file as the file you want to modify.
setting parameter indicates the type of dynamic IP or static IP
code is as follows

#!/bin/bash
ip=$1
gateway=$2
netmask=$3
dns1=$4
dns2=$5
type=$6
echo "###ip:" $ip "###"
echo "###gateway:" $gateway "###"
echo "###subnetmask:" $netmask "###"
echo "###dns1:" $dns1 "###"
echo "###dns2:" $dns2 "###"
echo "###type:" $type "###"
eth=`find /etc/sysconfig/network-scripts/ -name "ifcfg-e*" |sort |head -1 |awk -F/ '{print $5}'`
ethfile="/etc/sysconfig/network-scripts/$eth"
echo "${ethfile}"
sed 's/^ONBOOT=no/ONBOOT=yes/g' "${ethfile}"
if [ $6 == "1" ]
then
	echo "###dhcp###"
	sed -i 's/BOOTPROTO=static/BOOTPROTO=dhcp/g' "${ethfile}"
	sed -i '/IPADDR=/d' "${ethfile}"
	sed -i '/GATEWAY=/d' "${ethfile}"
	sed -i '/NETMASK=/d' "${ethfile}"
	sed -i '/DNS1=/d' "${ethfile}"
	sed -i '/DNS2=/d' "${ethfile}"

else
	echo "###static###"
	sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/g' "${ethfile}"
	sed -i '/IPADDR=/d' "${ethfile}"
	sed -i '/GATEWAY=/d' "${ethfile}"
	sed -i '/NETMASK=/d' "${ethfile}"
	sed -i '/DNS1=/d' "${ethfile}"
	sed -i '/DNS2=/d' "${ethfile}"
	echo "IPADDR=${ip}" >>${ethfile}
	echo "GATEWAY=${gateway}" >>${ethfile}
	echo "NETMASK=${netmask}" >>${ethfile}
	echo "DNS1=${dns1}" >>${ethfile}
	echo "DNS2=${dns2}" >>${ethfile}
fi
service network restart

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

NETCore execute Shell files

class Program
{
    static void Main(string[] args)
    {
        #region NETCore调用Shell
        string fileName = AppDomain.CurrentDomain.BaseDirectory + "test.sh";
        Console.WriteLine("path:" + fileName);
        Console.WriteLine("Input your arguments");
        string arguments = Console.ReadLine();// "10.10.20.20 10.10.20.1 255.255.255.0 114.114.114.114 8.8.8.8 0";每个参数用空格隔开
        Console.WriteLine("your arguments is:" + arguments);
        //创建一个ProcessStartInfo对象 使用系统shell 指定命令和参数 设置标准输出
        var psi = new ProcessStartInfo(fileName, arguments) { RedirectStandardOutput = true };
        //启动
        var proc = Process.Start(psi);
        if (proc == null)
        {
            Console.WriteLine("Can not exec.");
        }
        else
        {
            Console.WriteLine("-------------Start read standard output--------------");
            //开始读取
            using (var sr = proc.StandardOutput)
            {
                while (!sr.EndOfStream)
                {
                    Console.WriteLine(sr.ReadLine());
                }

                if (!proc.HasExited)
                {
                    proc.Kill();
                }
            }
            Console.WriteLine("---------------Read end------------------");
            Console.WriteLine($"Exited Code : {proc.ExitCode}");
        }
        #endregion

        Console.ReadKey();

        #region Win系统设置IP
        //ManagementBaseObject inPar = null;
        //ManagementBaseObject outPar = null;
        //ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        //ManagementObjectCollection moc = mc.GetInstances();
        //foreach (ManagementObject mo in moc)
        //{
        //    if (!(bool)mo["IPEnabled"])
        //        continue;

        //    //设置ip地址和子网掩码 
        //    inPar = mo.GetMethodParameters("EnableStatic");
        //    inPar["IPAddress"] = new string[] { "192.168.16.248", "192.168.16.249" };// 1.备用 2.IP
        //    inPar["SubnetMask"] = new string[] { "255.255.255.0", "255.255.255.0" };
        //    outPar = mo.InvokeMethod("EnableStatic", inPar, null);

        //    //设置网关地址 
        //    inPar = mo.GetMethodParameters("SetGateways");
        //    inPar["DefaultIPGateway"] = new string[] { "192.168.16.2", "192.168.16.254" }; // 1.网关;2.备用网关
        //    outPar = mo.InvokeMethod("SetGateways", inPar, null);

        //    //设置DNS 
        //    inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
        //    inPar["DNSServerSearchOrder"] = new string[] { "211.97.168.129", "202.102.152.3" }; // 1.DNS 2.备用DNS
        //    outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
        //    break;
        //}
        #endregion
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

Precautions

1. At the beginning of shell scripts often use a word to define what kind of sh interpreter to interpret the script, in this case using a #! / Bin / bash
2. Note shell script file encoding, if the encoding is not recognized by Linux, C # implementation of Linux script error occurs No Such file or directory abnormal. It is proposed to create a shell file, vi edit files in Linux use touch commands.
3.shell modified profile IP information is provided as follows. This is the IP configuration Centos7, it must be right, if abnormal after the network settings first consideration gateway subnet mask is not set up wrong.

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=30e3eefd-6ca5-45f2-90d8-4ad2c69f3b40
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.43.132
GATEWAY=192.168.43.2
NETMASK=255.255.255.0
DNS1=192.168.43.1
DNS2=192.168.43.2

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
Published 30 original articles · won praise 5 · views 10000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12067086.html