千家信息网

Linux系统shell脚本编程的示例分析

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,小编给大家分享一下Linux系统shell脚本编程的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!1、开发脚本前准备一般大家都知道,测试主机是否在线,常用的命令无非就是pin
千家信息网最后更新 2026年11月02日Linux系统shell脚本编程的示例分析

小编给大家分享一下Linux系统shell脚本编程的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!



1、开发脚本前准备


一般大家都知道,测试主机是否在线,常用的命令无非就是ping、nmap,因此,首先找一个地址来测试下ping命令的效果


[root@centos6 scripts]# ping 172.16.1.1

PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.

64 bytes from 172.16.1.1: icmp_seq=1 ttl=255 time=3.43 ms

64 bytes from 172.16.1.1: icmp_seq=2 ttl=255 time=0.699 ms

^C

--- 172.16.1.1 ping statistics ---

9 packets transmitted, 9 received, 0% packet loss, time 8448ms

rtt min/avg/max/mdev = 0.525/1.053/3.436/0.884 ms


好像单纯的这种命令是无法来做批量检查的,必须要带一些参数,否则它们一直ping下去


[root@centos6 scripts]# ping -W 2 -c 2 172.16.1.1

PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.

64 bytes from 172.16.1.1: icmp_seq=1 ttl=255 time=0.704 ms

64 bytes from 172.16.1.1: icmp_seq=2 ttl=255 time=0.481 ms

--- 172.16.1.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 0.481/0.592/0.704/0.114 ms


这种方法可以实现,测试发送2个数据包,然后加上超时时间,自动停止,可以达到效果


[root@centos6 scripts]# echo $?

0

[root@centos6 scripts]# ping -W 2 -c 2 172.16.1.100

PING 172.16.1.100 (172.16.1.100) 56(84) bytes of data.

^C

--- 172.16.1.100 ping statistics ---

2 packets transmitted, 0 received, 100% packet loss, time 2836ms

[root@centos6 scripts]# echo $?

1

因此,我们可以通过返回值来判断是否在线



2、开发简单脚本

既然有实现的方法了,那么接下来就开始开发脚本了

[root@centos6 scripts]# vi checkip.sh

#!/bin/sh

. /etc/init.d/functions

#加载系统函数库

CMD="ping -W 2 -c 2"

#定义命令变量

IP="172.16.1.2 172.16.1.3 172.16.1.100"

#定义IP变量

for n in $IP

#for循环语句

do

$CMD $IP$n >/dev/null 2>&1

#将命令结果不输出

if [ $? -eq 0 ];then

#如果返回值为0就表明在线

action "$IP$n is online" /bin/true

#在线就打印此信息

else

#否则就表示不在线

action "$IP$n is not online" /bin/false

#不在线就打印此信息

fi

done

执行下脚本看看结果如何

[root@centos6 scripts]# sh checkip.sh

172.16.1.2 is online [ OK ]

172.16.1.3 is online [ OK ]

172.16.1.100 is not online [FAILED]


此时肯定有小伙伴问了,你这个脚本测试的只有三个IP,如果内网整个网段IP都手工写上去,岂不是更费时费力,因此,如果是整个网段,那么定义IP变量时可以定义成这样IP="172.16.1." ,因为前三位是相同的,写for 循环时可以修改成如下

for n in `seq 254`

do

$CMD $IP$n(将两段数字拼接成IP地地址)

done

具体这里就不再测试了,有兴趣的可以自行测试下




3、开发nmap脚本检查在线IP与在线IP的开放端口情况


首先得了解下nmap的一些参数,它也是非常实用的命令之一,在日常实际生产环境中,经常用来检查IP、端口、URL地址信息,具体其中的参数这里就不做详细介绍了,后续有时间会分享它的相关参数用法


[root@centos6 scripts]# nmap -sP 172.16.1.1

Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 21:09 CST

Nmap scan report for 172.16.1.1

Host is up (0.0091s latency).

MAC Address: 04:BD:70:FB:A9:B7 (Unknown)

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

[root@centos6 scripts]# nmap -sP 172.16.1.100

Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 21:09 CST

Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn

Nmap done: 1 IP address (0 hosts up) scanned in 0.41 seconds


从上面的结果来看,很容易发现在线与不在线返回的信息不同,但是我们需要取得在线的IP地址信息,那到就只能取 Nmap scan report for 172.16.1.1 ,因为所有在线的IP返回的信息中都会有这一行信息,所以取相同的信息。


[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "Nmap scan report for"

Nmap scan report for 172.16.1.1

[root@centos6 scripts]#

nmap -sS 172.16.1.1|grep "Nmap scan report for"|awk '{print $5}'

172.16.1.1

#取出IP信息


[root@centos6 scripts]# nmap -sS 172.16.1.1

Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-03 20:56 CST

Nmap scan report for 172.16.1.1

Host is up (0.041s latency).

Not shown: 994 closed ports

PORT STATE SERVICE

21/tcp open ftp

22/tcp filtered ssh

23/tcp open telnet

80/tcp open http

179/tcp filtered bgp

443/tcp open https

MAC Address: 04:BD:70:FB:A9:B7 (Unknown)

Nmap done: 1 IP address (1 host up) scanned in 8.74 seconds


检查开启端口,我们可以通过过滤关键字 open 来实现,通过上面的信息很容易观察出来


[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "open"

21/tcp open ftp

23/tcp open telnet

80/tcp open http

443/tcp open https

[root@centos6 scripts]# nmap -sS 172.16.1.1|grep "open"|awk '{print $1}'

21/tcp

23/tcp

80/tcp

443/tcp



4、编写脚本并测试效果

[root@centos6 scripts]# vi checkip_namp01.sh

#!/bin/sh

. /etc/init.d/functions

#加载系统函数库

FCMD="nmap -sP "

#定义第一个命令变量

IP="172.16.1.1 172.16.1.2 172.16.1.100"

#定义IP变量

TCMD="nmap -sS"

#定义第一个命令变量

UPIP=`$FCMD $IP|grep "Nmap scan report for"|awk '{print $5}'`

#定义获取在线IP的变量

for ip in ${UPIP}

#for手环语句

do

action "$ip is on line" /bin/true

#打印信息

UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'`

#定义获取在线IP的开放端口变量

for port in ${UPPORT}

#二层循环检查端口

do

action "$ip $port is open" /bin/true

#将上面在线IP开放的端口信息打印输出

done

done

注:UPPORT=`$TCMD $ip|grep "open"|awk '{print $1}'` 定义这个变量时,取的IP地址一定要是上一个循环取出的IP地址,否则会有问题


执行脚本,测试效果如何?


[root@centos6 scripts]# sh checkip_namp01.sh

172.16.1.1 is on line [ OK ]

172.16.1.1 21/tcp is open [ OK ]

172.16.1.1 23/tcp is open [ OK ]

172.16.1.1 80/tcp is open [ OK ]

172.16.1.1 443/tcp is open [ OK ]

172.16.1.2 is on line [ OK ]

172.16.1.2 23/tcp is open [ OK ]

172.16.1.100没有出现的原因是它不在线


接下来测试下脚本检查的端口是否正确


[root@centos6 scripts]# telnet 172.16.1.1 443

Trying 172.16.1.1...

Connected to 172.16.1.1.

Escape character is '^]'.

^]

telnet> quit

Connection closed.

[root@centos6 scripts]# telnet 172.16.1.1 21

Trying 172.16.1.1...

Connected to 172.16.1.1.

Escape character is '^]'.

220 FTP service ready.

^]

telnet> quit

Connection closed.

[root@centos6 scripts]# telnet 172.16.1.2 23

Trying 172.16.1.2...

Connected to 172.16.1.2.

Escape character is '^]'.

TL-AP301C login:

telnet> quit

Connection closed.


从上面的结果来看,脚本检查的结果是正确,如果需要检查整个网段只需要将定义IP变量时定义成"IP="172.16.1.0/24""即可


看完了这篇文章,相信你对"Linux系统shell脚本编程的示例分析"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

在线 脚本 信息 变量 测试 命令 检查 端口 地址 结果 系统 参数 效果 开发 循环 网段 面的 开放 示例 脚本编程 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网高科技 华服互联网科技靠谱吗 互联网科技有限公司经营范围 互联网科技公司税收优惠 互联网科技公司怎么样 南京欣网互联网科技 菲比寻常互联网科技 四个字互联网科技公司取名 互联网科技股午盘股票涨跌 科技赋能互联网健康 须眉科技是互联网公司吗 北京华百购互联网科技有限公司 中和天地互联网科技有限公司 区块链结合互联网科技 互联网第三次科技革命特征 富国互联网科技002692 中国新注册互联网科技 银川新华互联网科技学校是国营吗 美国五大互联网科技公司市值多少 互联网对科技影响 广东万易互联网科技有限公司惠州分公司 互联网科技资迅 金融科技与互联网金融 万达互联网科技 融贸通互联网科技 中国科技互联网公司收入 南通创西澳互联网科技有限公司 富国互联网金融科技混合基金 容联科技互联网电商 易租星互联网科技有限公司
0