原文:NETCore执行Shell修改Centos系统IP信息原文:NETCore执行Shell修改Centos系统IP信息 目录shell代码NETCore执行Shell文件注意事项shell代码首先通过find命令找到/etc/sysconfig/network-scripts/目录下的ifcfg-en*的文件,然后通过sort排序,将第一个文件...
目录
- shell代码
- NETCore执行Shell文件
- 注意事项
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;
/
沃梦达教程
本文标题为:NETCore执行Shell修改Centos系统IP信息
基础教程推荐
猜你喜欢
- C# windows语音识别与朗读实例 2023-04-27
- C#类和结构详解 2023-05-30
- unity实现动态排行榜 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- C#控制台实现飞行棋小游戏 2023-04-22
- C# 调用WebService的方法 2023-03-09
- winform把Office转成PDF文件 2023-06-14
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# List实现行转列的通用方案 2022-11-02
- ZooKeeper的安装及部署教程 2023-01-22