zoukankan      html  css  js  c++  java
  • NETCore执行Shell修改Centos系统IP信息

    shell代码

    首先通过find命令找到/etc/sysconfig/network-scripts/目录下的ifcfg-en*的文件,然后通过sort排序,将第一个文件作为要修改的文件。
    type参数表示设置的是静态IP还是动态IP
    代码如下

    #!/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
    

    NETCore执行Shell文件

    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.在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本,本例中使用#!/bin/bash
    2.注意shell脚本的文件编码,如果编码不是Linux能识别的,C#执行Linux脚本出错,出现No Such file or directory异常。所以建议在Linux下用touch指令创建shell文件,vi编辑文件。
    3.shell修改IP信息设置后的配置文件如下。这是Centos7的IP配置,一定是正确的,如果设置完后网络异常首先考虑网关子网掩码是不是设置错了。

    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
    
  • 相关阅读:
    git 查看远程分支、本地分支、删除本地分支
    iOS edgesForExtendedLayout、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets属性详解
    【iOS开发】UIWebView与JavaScript(JS) 回调交互
    iOS打印Debug日志的方式
    iOS项目上传到AppStore步骤流程
    IOS开发之实现App消息推送(最新)
    DKNightVersion 的实现 --- 如何为 iOS 应用添加夜间模式
    用Session实现验证码
    HTTP中Get与Post、ViewState 原理
    ASP.NET获取服务器文件的物理路径
  • 原文地址:https://www.cnblogs.com/zt102545/p/13940225.html
Copyright © 2011-2022 走看看