ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

NETCore执行Shell修改Centos系统IP信息

2019-12-19 12:54:04  阅读:266  来源: 互联网

标签:Shell Console Centos NETCore inPar echo ethfile sed ###


原文: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
  • 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执行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
  • 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

注意事项

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
刹影 发布了30 篇原创文章 · 获赞 5 · 访问量 1万+ 私信 已关注

标签:Shell,Console,Centos,NETCore,inPar,echo,ethfile,sed,###
来源: https://www.cnblogs.com/lonelyxmas/p/12067086.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有