shell编程-7

news2024/9/29 23:22:14

shell学习第7天

  • sed的学习
    • 1.sed是什么
    • 2.sed有两个空间pattern hold
    • 3.sed的语法
    • 4. sed里单引号和双引号的区别:
    • 5.sed的查找方式
    • 6.sed的命令
      • sed的标签用法
      • sed的a命令:追加
      • sed的i命令:根据行号插入
      • sed的c命令:整行替换
      • sed的r命令
      • sed的s命令:替换
      • sed的d命令:删除
      • sed中的&符号
    • 7.怎么替换有深度的文件
    • 8. 练习(要是awk更好,就用awk)
    • 9.小知识点
      • 判断进程是否起来 pidof
      • 正则中的 \w 和 \W \s和\b
      • \1
      • 输出文本的第一行和最后一行
      • 编写一个一键更换ip地址,并检测ip地址是否合法的脚本。
      • 一键更换ip升级版

sed的学习

1.sed是什么

sed - stream editor for filtering and transforming text

用于过滤和转换文本的Sed流编辑器 —> 文字流编辑器

Sed is a stream editor. A stream editor is used to perform basic text transformations on
an input stream (a file or input from a pipeline).

Sed是一个流编辑器。流编辑器用于在上执行基本的文本转换
输入流(来自管道的文件或输入)。

sed是脚本中修改文本或者文本替换的最佳工具

简单的例子:修改selinux的配置文件 把enforcing改成disabled

[root@gh-shell 1-22] cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

[root@gh-shell 1-22] sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config 

2.sed有两个空间pattern hold

image-20240122160024598

-i选项直接对源文件进行修改

不接-i就是输出到屏幕 不改源文件

pattern space 输出之后就会清空里边的内容

3.sed的语法

image-20240122161318035

image-20240122163009855

执行多条语句,先打印1-10行,再打印20行,30行

[root@gh-shell 1-22] sed -n '1,10p;20p;30p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sc13:x:1012:1012::/home/sc13:/bin/bash
[root@gh-shell 1-22]# 

cat passwd -n|sed -n ‘1 ~ 3p’:每隔三行输出一下

4. sed里单引号和双引号的区别:

image-20240122164640262

[root@gh-shell 1-22] num1=10
[root@gh-shell 1-22] num2=20
[root@gh-shell 1-22] cat -n /etc/passwd|sed -n "${num1},${num2}p"
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:99:99:Nobody:/:/sbin/nologin
    14	systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15	dbus:x:81:81:System message bus:/:/sbin/nologin
    16	polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17	tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    19	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    20	chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@gh-shell 1-22]# 

5.sed的查找方式

  1. 根据行号

    通过 -p
    [root@gh-shell 1-22] sed -n '1p;2p' passwd 
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    [root@gh-shell 1-22]# 
    
  2. 根据模式—>正则表达式=字符+特殊符号

image-20240122171513259

[root@gh-shell 1-22] sed -n '/bash/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sc1:x:1000:1000::/home/sc1:/bin/bash
sc5:x:1004:1004::/home/sc5:/bin/bash
sc6:x:1005:1005::/home/sc6:/bin/bash
sc7:x:1006:1006::/home/sc7:/bin/bash
sc8:x:1007:1007::/home/sc8:/bin/bash
sc9:x:1008:1008::/home/sc9:/bin/bash
。
。
。
[root@gh-shell 1-22]#  

-n太好用了,只显示匹配处理的行

[root@gh-shell 1-22] sed -rn '/^#|^$/!p' /etc/grub2.cfg 
'不打印出以#开头,还有空行的行'

//$/p 找出以/结尾的行,需要用到转义符号

[root@gh-shell 1-22] df -h|sed -n '/\/$/p'
/dev/mapper/centos-root   50G  2.1G   48G    5% /
[root@gh-shell 1-22]# 

6.sed的命令

sed的标签用法

image-20240123184155341

玩一下:

[root@gh-shell 1-22] tail -1 passwd 
gaofei12:x:1032:1032::/home/gaofei12:/bin/bash
[root@gh-shell 1-22] sed -i -r 's/(^[0-Z]+)(.*)/\1/' passwd 
[root@gh-shell 1-22] tail -1 passwd 
gaofei12
[root@gh-shell 1-22]# 
{=;p}先执行=号命令,作用输出行号,然后执行打印内容命命

sed -E-n'/\b(\w+)\s+\1\b/{=;p}'two-cities-dup1.txt

sed的a命令:追加

[root@gh-shell shell] sed -i '2a gaodafei' user_pwd.txt     #第二行的后面加个gaodafei
[root@gh-shell shell] head -5 user_pwd.txt 
gf1  01a8589953
gf2  76088a94e6
gaodafei
gf3  e09f493732
gf4  d07af864bb
[root@gh-shell shell]# 
[root@gh-shell shell] sed -i '/gf3/a gaodafei123' user_pwd.txt 
[root@gh-shell shell] head -5 user_pwd.txt 
gf1  01a8589953
gf2  76088a94e6
gaodafei
gf3  e09f493732
gaodafei123
[root@gh-shell shell]# 

sed的i命令:根据行号插入

[root@gh-shell shell] sed -i '$i gaohui' user_pwd.txt  #在匹配的那个前一行加入
[root@gh-shell shell] tail user_pwd.txt 
gaodafei123
gf4  d07af864bb
gf5  57196a836a
gf6  2fce65efeb
gf7  2f41fdfa48
gf8  d610f5fcf3
gf9  e5003d62d1
gf10  7e8c0ffca3
gaohui
gaohui
[root@gh-shell shell]# 
[root@gh-shell shell] sed -i '/li/i shenmj' user_pwd.txt  匹配的前一行加
[root@gh-shell shell] tail user_pwd.txt 
gf5  57196a836a
gf6  2fce65efeb
gf7  2f41fdfa48
gf8  d610f5fcf3
gf9  e5003d62d1
gf10  7e8c0ffca3
gaohui
gaohui
shenmj
lidaliu
[root@gh-shell shell]# 

sed的c命令:整行替换

image-20240124105948045

sed的r命令

'读入操作'
sed '$r /etc/hosts' /etc/fstab
在fstab文件的末尾后面读入hosts文件的内容

sed的s命令:替换

image-20240124111054105

image-20240124113243808

image-20240124114758242

[root@gh-shell 1-13] cat grade.txt 
name	chinese	math	english
cali	80	91	82 cali	feng cali feng
tom	90	80	99
lucy	99	70	75
jack	60	89	99
[root@gh-shell 1-13] sed -i 's/cali/fengdeyong/' grade.txt #默认替换第一个
[root@gh-shell 1-13] cat grade.txt 
name	chinese	math	english
fengdeyong	80	91	82 cali	feng cali feng
tom	90	80	99
lucy	99	70	75
jack	60	89	99
[root@gh-shell 1-13] sed -i 's/cali/fengdeyong/2' grade.txt #替换第二个
[root@gh-shell 1-13] cat grade.txt 
name	chinese	math	english
fengdeyong	80	91	82 cali	feng fengdeyong feng
tom	90	80	99
lucy	99	70	75
jack	60	89	99
[root@gh-shell 1-13] sed -i 's/cali/fengdeyong/g' grade.txt  #全局替换
[root@gh-shell 1-13] cat grade.txt 
name	chinese	math	english
fengdeyong	80	91	82 fengdeyong	feng fengdeyong feng
tom	90	80	99
lucy	99	70	75
jack	60	89	99
[root@gh-shell 1-13]# 

换分隔符

[root@gh-shell 1-13] sed -i 's#sbin/nologin#fengdeyong#' passwd 
[root@gh-shell 1-13] tail passwd 
liu1:x:1023:1023::/home/liu1:fengdeyong
liu2:x:1024:1024::/home/liu2:fengdeyong
liu3:x:1025:1025::/home/liu3:fengdeyong
gaodingjiang:x:1026:1026::/home/gaodingjiang:fengdeyong
gaodingjiang1:x:1027:1027::/home/gaodingjiang1:fengdeyong
gaodingjiang12:x:1028:1028::/home/gaodingjiang12:fengdeyong
gaodingjiang123:x:1029:1029::/home/gaodingjiang123:fengdeyong
gh123:x:1030:1030::/home/gh123:fengdeyong
gaofei123:x:1031:1031::/home/gaofei123:fengdeyong
gaofei12:x:1032:1032::/home/gaofei12:fengdeyong
[root@gh-shell 1-13]# 

练习:

[root@gh-shell 1-13] sed '/^SELINUX=/ s/disabled/enforcing/' /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@gh-shell 1-13]# 

尝试删除grade.txt中lucy的行:

[root@gh-shell 1-13] sed -i  's/lucy//g' grade.txt
[root@gh-shell 1-13] cat grade.txt 
name	chinese	math	english
fengdeyong	80	91	82 fengdeyong	feng fengdeyong feng
tom	90	80	99
	99	70	75
jack	60	89	99
[root@gh-shell 1-13]# 

sed的d命令:删除

image-20240124112508212

'一般不建议删,而是加个注释'
[root@gh-shell 1-13] sed -i  '/^jack/ s/^/#/' grade.txt
[root@gh-shell 1-13] cat grade.txt 
name	chinese	math	english
fengdeyong	80	91	82 fengdeyong	feng fengdeyong feng
tom	90	80	99
	99	70	75
#jack	60	89	99
[root@gh-shell 1-13]# 

sed中的&符号

$代替sed在模式匹配里找到的内容

[root@gh-shell 1-24] vim cat.txt
[root@gh-shell 1-24] sed -i 's/.at/"&"/g' cat.txt 
[root@gh-shell 1-24] cat cat.txt 
i have a "fat" "cat"
i have a "fat" "cat"
i have a "fat" "cat"
i have a "fat" "cat"
i have a "fat" "cat"
[root@gh-shell 1-24]# 

7.怎么替换有深度的文件

'先用find找,再用sed替换'
[root@gh-shell 1-22] mkdir aa/bb/cc -p
[root@gh-shell 1-22] cp ip.txt aa/
[root@gh-shell 1-22] cp ip.txt aa/bb/
[root@gh-shell 1-22] cp ip.txt aa/bb/cc/
[root@gh-shell 1-22] find . -name ip.txt
./ip.txt
./aa/bb/cc/ip.txt
./aa/bb/ip.txt
./aa/ip.txt
[root@gh-shell 1-22]# 

写个脚本去改:

[root@gh-shell 1-22] cat sc_sed.sh
#!/bin/bash

for i in $(find /shell -name ip.txt)
do
	echo $i
	sed -i 's/192.168.1.8/8.8.8.8/g' $i
	echo "#####################"
	cat $i
done
[root@gh-shell 1-22] bash sc_sed.sh 
/shell/1-22/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
/shell/1-22/aa/bb/cc/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
/shell/1-22/aa/bb/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
/shell/1-22/aa/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
[root@gh-shell 1-22]# 

8. 练习(要是awk更好,就用awk)

19/Jan/2024:09:51:12 到 19/Jan/2024:10:41:48 日志,显示出来–>sed或者awk

'sed:'
sed -n '/19\/Jan\/2024:09:51:12/,/19\/Jan\/2024:10:41:48/p' access.log

'awk:'
awk '/19\/Jan\/2024:09:51:12/,/19\/Jan\/2024:10:41:48/' access.log

例子:

image-20240127101352808

复制/etc/hosts文件到当前目录下,然后进行操作在每行前面加一个字符串sanchuang

[root@gh-shell 1-22] sed -i 's/^/sanchuang/' hosts
[root@gh-shell 1-22] cat hosts 
sanchuang127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
sanchuang::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@gh-shell 1-22]# 

自己编辑一个文件test.txt,内容如下:
0.0.0.0
1.1.1.1
2.2.2.2
使用sed或者awk或者编写脚本(shell,python,go等)实现输出以下形式:
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80

[root@gh-shell 1-22] sed -i.bak 'N;N;s/\n/:80,/g;s/$/:80/'  test.txt 
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80

[root@gh-shell 1-22] cat test.txt.bak|sed -n 's/$/:80/;H;${x;s/\n/,/2g;p}'
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
[root@gh-shell 1-22]# 

'awk最简单'
[root@gh-shell 1-22] cat test.txt.bak.a |xargs |awk '{print $1":80,"$2":80,"$3":90"}'
0.0.0.0:80,1.1.1.1:80,2.2.2.2:90
[root@gh-shell 1-22]# 

x Exchange the contents of the hold and pattern spaces.
h H Copy/append pattern space to hold space.
g G Copy/append hold space to pattern space.
n N Read/append the next line of input into the pattern space.

image-20240127105904709

**sed -i.bak ‘N;N;s/\n/:80,/g;s/$/:80/’ test.txt **

这是一个使用 sed 命令在文本文件中进行替换的命令。让我逐步解释这个命令:

  • sed: 流编辑器,用于处理和转换文本流。

  • -i.bak: -i 表示直接在文件中进行修改(in-place),后面的 .bak 是一个备份文件的扩展名。这样在修改文件的同时会在同级目录下生成一个备份文件,以便于回滚。

  • 'N;N;s/\n/:80,/g;s/$/:80/': 这是 sed 的编辑脚本,它包含了多个命令。

    • N;N: 连续两次使用 N 命令,将下一行添加到当前行的末尾,形成两行的模式空间。

    • s/\n/:80,/g: 将模式空间中的所有换行符 \n 替换为 :80,。这样,每两行之间的换行符都被替换为 :80,,形成了IP地址和端口号的组合。

    • s/$/:80/: 将模式空间中的行尾 $ 替换为 :80,给每一行的最后添加 :80

总体来说,这个命令的作用是将每两行之间的换行符替换为 :80,,并在每行的末尾添加 :80。最终结果是将 test.txt 文件中每两行IP地址间的换行符替换为 :80,,并在每行的末尾添加 :80

cat test.txt.bak|sed -n 's/$/:80/;H;${x;s/\n/,/2g;p}'

这是一个用于处理文本的 LInix 命令行管道,涉及到 sedcat 命令。让我逐步解释这个命令:

  1. cat test.txt.bakcat 命令用于将文件的内容输出到标准输出。这里是将 test.txt.bak 文件的内容输出。

  2. |:管道符号,将前一个命令的输出传递给下一个命令作为输入。

  3. sed -n 's/$/:80/;H;${x;s/\n/,/2g;p}'sed 是一种流编辑器,用于文本流的处理。

    • -n 参数表示关闭默认输出,只输出经过编辑的行。
    • s/$/:80/:正则表达式替换,将每一行的行尾 $ 替换为 :80。这样,每个IP地址后都会添加 :80
    • H:将模式空间中的行追加到保持空间(hold space)的末尾。
    • ${x;s/\n/,/2g;p}
      • ${}:表示在最后一行执行的命令。 花括号里边是只对最后一行操作!
      • x:交换模式空间和保持空间的内容。
      • s/\n/,/2g:将保持空间中的所有换行符替换为逗号。这样,每个IP地址与端口号的组合之间的换行符都会被替换为逗号。
      • p:打印最终结果。

综合起来,这个命令的作用是将 test.txt.bak 文件中的每个IP地址后面添加 :80,然后将每个IP地址与端口号的组合之间的换行符替换为逗号,最终输出一行带有IP地址和端口号的字符串。

先复制/etc/passwd文件到当前目录下,然后对当前目录下的passwd进行操作

1.取出passwd文件的第一列

[root@gh-shell 1-22] cat passwd|awk -F: '{print $1}'

2.sed将PATH环境变量中的冒号换成 ->可以将PATH变量的内容重定向到一个文件里,例如path.txt

echo $PATH|sed

[root@gh-shell 1-22]# echo $PATH
/shell/1-13/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@gh-shell 1-22]# echo $PATH >>gh.txt
[root@gh-shell 1-22]# cat gh.txt 
/shell/1-13/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@gh-shell 1-22]# sed -i 's/:/->/g' gh.txt 
[root@gh-shell 1-22]# cat gh.txt 
/shell/1-13/->/usr/local/sbin->/usr/local/bin->/usr/sbin->/usr/bin->/root/bin
[root@gh-shell 1-22]# 

3.sed将PATH环境变量斜杠/换成斜杠\

[root@gh-shell 1-22] sed -i 's/\//\\/g' gh.txt
[root@gh-shell 1-22] cat gh.txt 
\shell\1-13\:\usr\local\sbin:\usr\local\bin:\usr\sbin:\usr\bin:\root\bin

4.sed修改SELINUX配置文件从开启(enforcing)变成禁用(disabled)
/etc/sysconfig/selinux

sed '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config 

5.去掉passwd文件中第二个字段的x

[root@gh-shell 1-22] cat passwd |awk -F: '{print $1":"$3":"$4":"$5":"$6":"$7}' 
[root@gh-shell 1-22] sed -i -r '/([0-Z]+)(:X:)(.*)/\1:\3' passwd

6.只显示ip add的ip地址

[root@gh-shell 1-22] ip add|awk -F"[/ ]+" '/ens33$/{print $3}'
192.168.153.166
[root@gh-shell 1-22]# 

7.复制/etc/ssh/sshd config到当前目录下,修改里面的端口号修改为8899

将#Port 22 配置修改为Port 8899 要求去掉前面的#号,将22修改为8899

[root@gh-shell 1-22] sed -i '/^#Port/c Port 8899' ssh_config

9.小知识点

判断进程是否起来 pidof

[root@gh-shell 1-22] pidof nginx
[root@gh-shell 1-22] echo $?
1
[root@gh-shell 1-22] pidof sshd
2740 2719 2133 2109 1109   #获得正在运行的进程号
[root@gh-shell 1-22]# 

或者直接统计有多少行,一行就是没有启动

[root@gh-shell 1-22] ps -ef|grep nginx
root       3412   2744  0 15:52 pts/3    00:00:00 grep --color=auto nginx
[root@gh-shell 1-22] ps -ef|grep nginx|wc -l
1
[root@gh-shell 1-22] ps -ef|grep sshd
root       1109      1  0 12:28 ?        00:00:00 /usr/sbin/sshd -D
root       2109   1109  0 12:31 ?        00:00:00 sshd: root@pts/0
root       2133   1109  0 12:31 ?        00:00:00 sshd: root@pts/1
root       2719   1109  0 14:52 ?        00:00:00 sshd: root@pts/2
root       2740   1109  0 14:52 ?        00:00:00 sshd: root@pts/3
root       3426   2744  0 15:52 pts/3    00:00:00 grep --color=auto sshd
[root@gh-shell 1-22] ps -ef|grep sshd|wc -l
6
[root@gh-shell 1-22]# 

正则中的 \w 和 \W \s和\b

  • \w表示字母,数组,下划线
  • \W表示特殊符号
  • \s表示匹配的空白
  • \b。。。\b表示单词界定

\1

[root@halou-gf lianxi]echo aaafdfd bbb ccc |sed -nr 's/([a-z]+) ([a+z]+) ([a-z]+)/\3 \2 \1/p'

在 sed 中,\3, \2, \1 属于后向引用,用于引用正则表达式中括号分组的匹配结果。

在你提供的示例中,正则表达式 ([a-z]+) ([a+z]+) ([a-z]+) 匹配了三个部分,每个部分都用括号分组起来了。其中:

  • ([a-z]+): 第一个括号分组,匹配一个或多个小写字母。
  • ([a+z]+): 第二个括号分组,匹配一个或多个小写字母和加号 +
  • ([a-z]+): 第三个括号分组,匹配一个或多个小写字母。

\3, \2, \1 分别对应这三个括号分组的匹配结果。

通过将 \3 \2 \1 作为替换字符串,sed 命令会将匹配到的文本进行替换。具体来说,\3 将会被匹配到的第三个括号分组的内容替换,\2 将会被匹配到的第二个括号分组的内容替换,\1 将会被匹配到的第一个括号分组的内容替换。

所以,在你的示例中,sed 命令会将匹配到的字符串 aaafdfd bbb ccc 进行替换,将括号分组的内容按照 \3 \2 \1 的顺序重新排列。最终输出结果为 ccc bbb aaafdfd

输出文本的第一行和最后一行

'方式1:'
[root@gh-shell 1-22] head -1 passwd;tail -1 passwd 
root:x:0:0:root:/root:/bin/bash
gaofei12:x:1032:1032::/home/gaofei12:/bin/bash

'方式2:'
[root@gh-shell 1-22] sed -rn '1p;$p' passwd 
root:x:0:0:root:/root:/bin/bash
gaofei12:x:1032:1032::/home/gaofei12:/bin/bash

编写一个一键更换ip地址,并检测ip地址是否合法的脚本。

[root@gh-shell 1-22] cat modify_ip.sh 
#!/bin/bash

#接受第一个位置变量
new_ip=$1
# 检测IP地址是否合法
ip_regex='^((1[0-9][0-9]|2[0-4][0-9]|25[0-5]\.)([01][0-9]|2[0-4]|25[0-5]\.){2}([01][0-9]|2[0-4]|25[0-5]))$'
if ! [[ $new_ip =~ $ip_regex ]]; then
  echo "无效的IP地址"
  exit 1
fi
#备份
mkdir /backup_network -p
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /backup_network
#修改ip地址为第一个位置变量的内容
sed -i "/IPADDR/c IPADDR=$new_ip/" /etc/sysconfig/network-scripts/ifcfg-ens33
#重启网络服务
service network restart
#获取ip地址
sc_ip=$(ip add|awk '/inet.*ens33$/{print $2}')
#通过ip add命令去查看本机的ip地址,然后截取出来统计ip内容的长度,如果长度大于1就表示有ip,如果长度为小于1就表示没有ip
if ((${#sc_ip}>1));then
	echo "modify ip successfully"
else
	#回滚
	cp /backup_network/ifcfg-ens33 /etc/sysconfig/network-scripts/ -f
	service network restart 
fi

[root@gh-shell 1-22]# 

传个错误的ip会失败

正确的会成功

一键更换ip升级版

编写一个修改ip地址的脚本

提醒用户输入ip、子网掩码、网关、dns服务器地址

直接修改网卡配置文件,然后刷新服务让输入的ip地址信息生效

需要检查输入ip地址是否合法,不能输入空的内容或者字母,如果ip地址不合法或者输入空内容、字母都给予提醒

提醒:空的内容包括回车和空格,可以是多个空格 在我刚才脚本的基础上改

#!/bin/bash

# 提醒用户输入IP地址
read -p "请输入新的IP地址: " new_ip

# 检查IP地址是否为空或包含非数字字符
if [[ -z "$new_ip" || ! "$new_ip" =~ ^[0-9.]+$ ]]; then
  echo "无效的IP地址"
  exit 1
fi

# 提醒用户输入子网掩码
read -p "请输入子网掩码: " subnet_mask

# 检查子网掩码是否为空或包含非数字字符
if [[ -z "$subnet_mask" || ! "$subnet_mask" =~ ^[0-9.]+$ ]]; then
  echo "无效的子网掩码"
  exit 1
fi

# 提醒用户输入网关
read -p "请输入网关地址: " gateway

# 检查网关地址是否为空或包含非数字字符
if [[ -z "$gateway" || ! "$gateway" =~ ^[0-9.]+$ ]]; then
  echo "无效的网关地址"
  exit 1
fi

# 提醒用户输入DNS服务器地址
read -p "请输入DNS服务器地址: " dns_server

# 检查DNS服务器地址是否为空或包含非数字字符
if [[ -z "$dns_server" || ! "$dns_server" =~ ^[0-9.]+$ ]]; then
  echo "无效的DNS服务器地址"
  exit 1
fi

# 备份网络配置
mkdir -p /backup_network
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /backup_network

# 修改网卡配置文件
sed -i "/IPADDR/c IPADDR=$new_ip/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "/NETMASK/c NETMASK=$subnet_mask/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "/GATEWAY/c GATEWAY=$gateway/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "/DNS1/c DNS1=$dns_server/" /etc/sysconfig/network-scripts/ifcfg-ens33

# 重启网络服务
service network restart

# 获取修改后的IP地址
sc_ip=$(ip add | awk '/inet.*ens33$/{print $2}')

# 检查是否成功修改IP地址
if [ ${#sc_ip} -gt 1 ]; then
  echo "成功修改IP地址"
else
  # 回滚
  cp -f /backup_network/ifcfg-ens33 /etc/sysconfig/network-scripts/
  service network restart
  echo "无法修改IP地址。回滚操作。"
fi

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1413815.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Ubuntu20.04添加桌面启动、侧边栏启动和终端启动

桌面启动 新建XX.desktop文件 在桌面新建一个XX.desktop文件,以QtCreator为例。 (注意这里不能使用sudo,因为这样会把文件的权限归为root,导致后续设置可执行程序不方便) gedit qtcreator.desktop在XX.desktop文件中…

算子:详细篇

目录 一、执行环境 1.1 创建执行环境 1.2 执行模式 二、源算子 2.1 从集合中读取数据 2.2 从文件读取数据 2.3 从socket读取数据 2.4 从kafka读取数据 三、转换算子 3.1 基本转换算子 (1)映射(map) (2)过滤(filter) &#xff08…

数据结构与算法教程,数据结构C语言版教程!(第六部分、数据结构树,树存储结构详解)二

第六部分、数据结构树,树存储结构详解 数据结构的树存储结构,常用于存储逻辑关系为 "一对多" 的数据。 树存储结构中,最常用的还是二叉树,本章就二叉树的存储结构、二叉树的前序、中序、后序以及层次遍历、线索二叉树、…

ASP.NET 7 Core Web 读取appsetting.json

把一些配置信息保存在json文件可以避免更改时要重新发布程序的烦恼。 我这里使用的是写一个类文件,然后通过program.cs启动的方式(.net 6 开始没有startup了)。 项目类型:ASP.NET Core Web MVC / .NET 7.0 / VS2022 第一步…

架构学习(一):scrapy实现按脚本name与日期生成日志文件

原生scrapy日志机制 一般情况下,我们可以直接在setting文件中定义日志文件,这种会把所有脚本的日志都写在同一个文件 LOG_LEVEL INFO # 日志级别 LOG_STDOUT True # 日志标准输出 LOG_FILE rD:\python\crawler\logs\1163.log # 日志文件路径现在…

少儿编程 2023年12月电子学会图形化编程等级考试Scratch一级真题解析(选择题)

2023年12月scratch编程等级考试一级真题 选择题(共25题,每题2分,共50分) 1、观察下列每个圆形中的四个数,找出规律,在括号里填上适当的数 A、9 B、17 C、21 D、5 答案:C 考点分析&#xf…

niushop靶场漏洞查找-文件上传漏洞等(超详细)

实战漏洞-niushop 一.端口扫描 http://www.xxx.com/index.php?s/admin/login 这里查询到后面的url有且仅有一个,目测估计是后台 访问url 发现确实是后台 二、找漏洞 Sql注入漏洞1: 点击进去 修改id www.xxx.com/index.php?s/goods/goodslist&…

automa插件使用的一些实战经验3

1 子流程的变量怎么传回父流程 主流程向子流程传参很容易 在子流程可以看到,父流程定义的表格,在子流程中是看不到的,那么子流程定义的变量如何传回父流程呢?另外在子流程再添加执行工作流,是无法选择父流程本身&…

惊了!用vue开发官网,以前我觉得胡闹,现在觉得未尝不可。

以前,有人做好官网UI(展示性,没啥功能),找我开发前端,说要vue来做,我都劝了。 基于以下四个原因: 1、官网毕竟还是考虑seo的,流量多少算多少,总比没有强&am…

如何使用 dotnet pack 打包 .NET 跨平台程序集?

如何使用 dotnet pack 打包 .NET 跨平台程序集? dotnet pack 介绍官方描述 dotnet pack 命令说明使用示例打包 .net 类库项目生成带注释的 nuget 包构建特定平台的 nuget 包关于 .NET RID 目录 dotnet pack 介绍 dotnet pack 是一个 .NET Core NuGet 包打包程序。 …

鸿蒙开发初体验

文章目录 前言一、环境配置1.1 安装DevEco Studio1.2 安装相关环境 二、工程创建三、工程结构介绍四、代码实现4.1 初识ArkTs4.2 具体实现 参考资料 前言 HarmonyOS是华为公司推出的一种操作系统,旨在为不同设备提供统一的操作系统和开发平台。鸿蒙开发的出现为用户…

3d gaussian splatting笔记(paper部分翻译)

本文为3DGS paper的部分翻译。 基于点的𝛼混合和 NeRF 风格的体积渲染本质上共享相同的图像形成模型。 具体来说,颜色 𝐶 由沿射线的体积渲染给出: 其中密度 𝜎、透射率 𝑇 和颜色 c 的样本是沿着射线以…

排序(插入排序)

现在,我们学习了之前数据结构的部分内容,即将进入一个重要的领域:排序,这是一个看起来简单,但是想要理清其中逻辑并不简单的内容,让我们一起加油把! 排序的概念及其运用 排序的概念 排序&…

贾玲新片刚刚发出紧急声明,是什么情况。

♥ 为方便您进行讨论和分享,同时也为能带给您不一样的参与感。请您在阅读本文之前,点击一下“关注”,非常感谢您的支持! 文 |猴哥聊娱乐 编 辑|徐 婷 校 对|侯欢庭 1月22日,一则“多位明星参演的电影涉影视投资诈骗…

浅谈WPF之样式与资源

WPF通过样式,不仅可以方便的设置控件元素的展示方式,给用户呈现多样化的体验,还简化配置,避免重复设置元素的属性,以达到节约成本,提高工作效率的目的,样式也是资源的一种表现形式。本文以一个简…

ARKit 3D 物体检测跟踪

3D 物体检测跟踪 3D物体检测跟踪技术,是指通过计算机图像处理和人工智能技术对摄像机拍摄到的3D物体识别定位并对其姿态进行跟踪的技术。3D物体跟踪技术的基础也是图像识别,但比前述2D 图像检测、识别、跟踪要复杂得多,原因在于现实世界中的物…

【网络协议测试】畸形数据包——圣诞树攻击(DOS攻击)

简介 TCP所有标志位被设置为1的数据包被称为圣诞树数据包(XMas Tree packet),之所以叫这个名是因为这些标志位就像圣诞树上灯一样全部被点亮。 标志位介绍 TCP报文格式: 控制标志(Control Bits)共6个bi…

单调栈第二天(还没写完)

503.下一个更大元素II 力扣题目链接(opens new window) 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更…

可以运行在浏览器的Windows 2000

Windows 2000 可以在浏览器里跑了,缺点就是速度慢。 点击这里在浏览器中运行 Windows 2000​​​​​​- --------------------------------------------------------------------------------------------------------------------------------- --------------…

2024年【浙江省安全员-C证】考试题库及浙江省安全员-C证模拟考试

题库来源:安全生产模拟考试一点通公众号小程序 2024年【浙江省安全员-C证】考试题库及浙江省安全员-C证模拟考试,包含浙江省安全员-C证考试题库答案和解析及浙江省安全员-C证模拟考试练习。安全生产模拟考试一点通结合国家浙江省安全员-C证考试最新大纲…