NETCore执行Shell修改Centos系统IP信息
原文: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
NETCore执行Shell修改Centos系统IP信息的更多相关文章
- 修改CentOS系统的默认启动级别
======修改CentOS系统的默认启动级别====== 现在的Linux系统安装完后就运行在第5个级别,即系统启动后直接进入图形界面,而不用在字符模式下登录后用startx或者xinit来起动图形 ...
- [转]设置修改CentOS系统时区
在我们使用CentOS系统的时候,也许时区经常会出现问题,有时候改完之后还是会出错,下面我们就来学习一种方法来改变这个状况.如果没有安装,而你使用的是 CentOS系统 那使用命令 yum insta ...
- 修改虚拟机CentOS系统ip地址和主机名
按照教程安装了虚拟机但是未配置静态IP,所以导致IP地址经常变化,CRT,mysql等连接时经常出现问题. 所以修改虚拟机内CentOS系统的IP为静态IP. 一.查看当前网关 虚拟机-->[编 ...
- 设置修改CentOS系统时间和时区
1.yum install ntp,安装时间服务ntpdate time-a.nist.gov && hwclock -w (跟网络同步时间,并且写入主板BIos) 2.chkconf ...
- 设置修改CentOS系统时区
一.时区 1. 查看当前时区date -R 2. 修改设置时区方法(1)tzselect方法(2) 仅限于RedHat Linux 和 CentOS系统timeconfig方法(3) 适用于Debia ...
- 修改CentOS的IP地址
一.临时修改 命令:ifconfig eth0 192.168.1.147 重启或者关机后,iP地址将会恢复到修改之前的状态. 二.永久修改 命令: vi /etc/sysconfig/network ...
- 命令行修改linux系统IP
修改配置文件/etc/sysconfig/network-scrips/ifcfg-eth0.因为机子启动的时候加载的就是这个文件的配置参数.对这个文件进行修改: [root@localhost ...
- 笔记:修改centos的IP地址相关配置
最近碰到不少认识的人问相关问题 索性做个笔记 图个方便 修改eth0的网卡配置vi /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0BOOTPR ...
- 教你如何修改CentOS系统上的时间
直接看命令:
随机推荐
- 在ubuntu更新时,出现错误E: Some index files failed to download, they have been ignored, or old ones used inst
原文:https://blog.csdn.net/tian_ciomp/article/details/51339635 在ubuntu更新时,出现错误E: Some index files fail ...
- Linux shell awk中printf使用
printf 是 awk 的重要格式化输出命令 printf格式化输出内容 格式: printf format,item1,item2... 要点: 1,printf输出时要指定格式f ...
- Linux进程管理之ps
Linux 是一种动态系统,能够适应不断变化的计算需求.下面介绍一些 Linux 所提供的工具来进行进程的查看与控制,掌握这些让我们能在某些进程出现异常的时候及时查看相关的指标,从而解决问题. 进程管 ...
- windows定期删除文件
:: 定时清理客户端上传导入包文件 @echo off title 清理客户端上传导入包文件 :: 导入包文件目录 set log_dir="F:\http\uploadzip\web\ht ...
- Go Programming Language 2
[Go Programming Language 2] 1.In Go, the sign of the remainder is always the same as the sign of the ...
- JDK1.8 LocalDate 使用方式;LocalDate 封装Util,LocalDate工具类(四)
未完待续 ........ 前言: 加班了好几天,终于结束上一个坑的项目了,项目交接人员全部离职,代码一行注释没有,无人问津的情况下,完成了项目,所以好的规范真的很重要. 继续日期改写 一 ...
- reactnative遇到的问题总结
1.View中出现文本报错,View等标签中不能出现字符串文本,字符串文本需要包在Text中,遇到如下错误 下面是问题代码: let rightTitle = this.props.rightTitl ...
- vue-cli3和ts建立vue项目
第一步,如果你之前没有装vuecli,可以直接执行下面命令 npm install --global @vue/cli 注:这里我install 的时候不成,于是我用的是 cnpm install - ...
- 【MyEclipse异常】更换工作空间无法启动的异常
- (HK1-1)海康网络摄像机的使用
https://blog.csdn.net/u014552102/article/details/86700057 一.手机客户端操作: 首先在莹石商城官网https://www.ys7.com/下 ...
