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系统上的时间
直接看命令:
随机推荐
- Cron Expressions——Cron 表达式(QuartZ调度时间配置)
如果你需要像日历那样按日程来触发任务,而不是像SimpleTrigger 那样每隔特定的间隔时间触发,CronTriggers通常比SimpleTrigger更有用. 使用CronTrigger,你可 ...
- django2.2(一)
限制请求method 什么是method 1.通常客户端请求服务器获取资源为GET方式 2.客户端提交数据给服务器端数据为POST方式 method限制请求 如果要限制请求,比如客户端只允许用GET方 ...
- Collaborative Spatioitemporal Feature Learning for Video Action Recognition
Collaborative Spatioitemporal Feature Learning for Video Action Recognition 摘要 时空特征提取在视频动作识别中是一个非常重要 ...
- Fluter基础巩固之Dart语言详解<二>
继续学习枯燥的Dart语言语法,目前的耐得住寂寞是为了将来学得“爽”做准备的!!! 异常: Dart 提供了 Exception 和 Error 类型, 以及一些子类型.还可以定义自己的异常类型.但是 ...
- Nginx对图片进行防盗链
这里需要使用两台Linux主机(一台充当防盗链服务器,一台充当盗链服务器),下表是它们所使用的操作系统以及IP地址. 两台Linux主机所使用的操作系统以及IP地址 主机名称 操作系统 IP地址 防盗 ...
- WeUI教程/第三方扩展及其他UI框架对比
WeUI 是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信内网页和微信小程序量身设计,令用户的使用感知更加统一.包含button.cell.dialog. progress. toa ...
- Python input() 函数
Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型. Python2.x 中 input() 相等于 eval(raw_input(prompt)) ,用来获 ...
- python基础之五:dict 字典
1.数据类型的划分:可变数据类型.不可变数据类型 不可变的有:元组(tuple).字符(str).整型(int).布尔型(bool) 它们都可以哈希 可变的:列表(list).set.字典(dict) ...
- Github api【Restful接口规范】
Overview This describes the resources that make up the official GitHub REST API v3. If you have any ...
- iview form表单数值类型校验「iview自定义form表单校验器」
摘录iview表单验证 Form 组件基于 sync-validator 实现的数据验证,给 Form 设置属性 rules,同时给需要验证的 FormItem 设置属性 prop 指向对应字段即可. ...
