原文: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万+

NETCore执行Shell修改Centos系统IP信息的更多相关文章

  1. 修改CentOS系统的默认启动级别

    ======修改CentOS系统的默认启动级别====== 现在的Linux系统安装完后就运行在第5个级别,即系统启动后直接进入图形界面,而不用在字符模式下登录后用startx或者xinit来起动图形 ...

  2. [转]设置修改CentOS系统时区

    在我们使用CentOS系统的时候,也许时区经常会出现问题,有时候改完之后还是会出错,下面我们就来学习一种方法来改变这个状况.如果没有安装,而你使用的是 CentOS系统 那使用命令 yum insta ...

  3. 修改虚拟机CentOS系统ip地址和主机名

    按照教程安装了虚拟机但是未配置静态IP,所以导致IP地址经常变化,CRT,mysql等连接时经常出现问题. 所以修改虚拟机内CentOS系统的IP为静态IP. 一.查看当前网关 虚拟机-->[编 ...

  4. 设置修改CentOS系统时间和时区

    1.yum install ntp,安装时间服务ntpdate time-a.nist.gov && hwclock -w (跟网络同步时间,并且写入主板BIos) 2.chkconf ...

  5. 设置修改CentOS系统时区

    一.时区 1. 查看当前时区date -R 2. 修改设置时区方法(1)tzselect方法(2) 仅限于RedHat Linux 和 CentOS系统timeconfig方法(3) 适用于Debia ...

  6. 修改CentOS的IP地址

    一.临时修改 命令:ifconfig eth0 192.168.1.147 重启或者关机后,iP地址将会恢复到修改之前的状态. 二.永久修改 命令: vi /etc/sysconfig/network ...

  7. 命令行修改linux系统IP

    修改配置文件/etc/sysconfig/network-scrips/ifcfg-eth0.因为机子启动的时候加载的就是这个文件的配置参数.对这个文件进行修改:   [root@localhost ...

  8. 笔记:修改centos的IP地址相关配置

    最近碰到不少认识的人问相关问题 索性做个笔记 图个方便 修改eth0的网卡配置vi /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0BOOTPR ...

  9. 教你如何修改CentOS系统上的时间

    直接看命令:

随机推荐

  1. scrapy-redis 0.6.8 配置信息

    很多博客的db参数配置都不能用,所以记录一下该版本可用的配置 #启用Redis调度存储请求队列 SCHEDULER = "scrapy_redis.scheduler.Scheduler&q ...

  2. volume create: k8s-volume: failed: Host 172.31.182.142 is not in 'Peer in Cluster' state

    问题描述: 1.gluster peer status查询存在节点 2.创建volume失败提示节点不存在 排查方法: 1.hosts文件是否配置正确 2.检查防火墙是否打开,打开的话放行24007端 ...

  3. CentOS 7 使用Nexus3搭建maven私服

    安装jdk(略) 安装maven wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3. ...

  4. 十一、yield生成器

    1.对比range 和 xrange 的区别 >>> print range() [, , , , , , , , , ] >>> print xrange() x ...

  5. 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)

    目录 题目链接 思路 代码 题目链接 传送门 思路 首先我们将原式化简: \[ \begin{aligned} &\sum\limits_{l_1=1}^{n}\sum\limits_{l_2 ...

  6. 大众点评评论数据抓取 反爬虫措施有css文字映射和字体库反爬虫

    大众点评评论数据抓取  反爬虫措施有css文字映射和字体库反爬虫 大众点评的反爬虫手段有那些: 封ip,封账号,字体库反爬虫,css文字映射,图形滑动验证码 这个图片是滑动验证码,访问频率高的话,会出 ...

  7. 03-C#笔记-数据类型

    --- # 1 数据类型 --- ## 1 和C++不同的数据类型: byte: 8位,0~255 decimal:范围大于double,128位 sbyte: 8位,-128~127 uint 32 ...

  8. openpose开发(1)官方1.5版本源码编译

    环境 WIN10系统,联想Y7000配置,8G内存 VS2019 cuda10 cudnn10 opencv4.11没有扩展库 显卡 1050TI 用到的库(提前下载好的模型,依赖库,user_cod ...

  9. keepalived 配置文件解析

    ! Configuration File for keepalived global_defs { #全局定义部分 notification_email { #设置报警邮件地址,可设置多个 acass ...

  10. SCDM导入点数据

    我们有时候需要把外部的点导入SCDM当中,但是SCDM没有像ICEM或者DM那样直接提供点导入的选项,是不是SCDM就无法导入点的数据了呢?答案当然是否定的.把点导入SCDM中的方法总结如下(示例数据 ...