Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of shell script programming in Linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the Linux system shell script programming example analysis, I hope you will learn something after reading this article, let's discuss it together!

1. Prepare the script before development

As we all know, the most common commands for testing whether the host is online are ping and nmap, so first find an address to test the effect of the ping command.

[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.525max 1.053 ms 3.436 pound 0.884

It seems that this kind of command alone can not do batch inspection, you must take some parameters, otherwise they will continue to 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 ms 0.592 max 0.704 max 0.114

This method can be realized, the test sends 2 data packets, then adds the timeout, stops automatically, and the effect can be achieved.

[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 $?

one

Therefore, we can judge whether it is online by the return value.

2. Develop simple scripts

Now that there is a way to implement it, let's start developing the script.

[root@centos6 scripts] # vi checkip.sh

#! / bin/sh

. / etc/init.d/functions

# load the system function library

CMD= "ping-W 2-c 2"

# define command variables

IP= "172.16.1.2 172.16.1.3 172.16.1.100"

# define IP variables

For n in $IP

# for Loop statement

Do

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

# do not output the command result

If [$?-eq 0]; then

# if the return value is 0, it is online

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

# print this information online

Else

# otherwise it means that it is not online

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

# print this message if you are not online

Fi

Done

Execute the script and see how it turns out.

[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]

At this point, a partner must have asked, your script tests only three IP. If the entire private network segment IP is written by hand, it will be more time-consuming and laborious. Therefore, if it is the entire network segment, then the definition of the IP variable can be defined as this IP= "172.16.1.", because the first three bits are the same, and the for loop can be modified as follows

For n in `seq 254`

Do

$CMD $IP$n (concatenate two numbers into IP addresses)

Done

It is no longer tested here. Those who are interested can test it themselves.

3. Develop a nmap script to check the open ports of online IP and online IP

First of all, we need to understand some parameters of nmap, which is also one of the very practical commands. In the daily actual production environment, it is often used to check IP, port, URL address information. The specific parameters will not be described in detail here. Later, we will share the usage of its related parameters.

[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

From the above results, it is easy to find that the information returned online and not online is different, but we need to get the online IP address information, so we can only take Nmap scan report for 172.16.1.1, because all online IP return information will have this line of information, so take the same information.

[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

# take out IP information

[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

Check the open port, which can be achieved by filtering the keyword open, which can be easily observed by the above information.

[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. Write a script and test the effect

[root@centos6 scripts] # vi checkip_namp01.sh

#! / bin/sh

. / etc/init.d/functions

# load the system function library

FCMD= "nmap-sP"

# define the first command variable

IP= "172.16.1.1 172.16.1.2 172.16.1.100"

# define IP variables

TCMD= "nmap-sS"

# define the first command variable

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

# define variables to get online IP

For ip in ${UPIP}

# for bracelet statement

Do

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

# print information

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

# define the open port variable to get the online IP

For port in ${UPPORT}

# layer 2 Loop check Port

Do

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

# print out the port information opened by the above online IP

Done

Done

Note: when UPPORT= `$TCMD $ip | grep "open" | awk'{print $1}'` defines this variable, the IP address taken must be the IP address taken out in the previous loop, otherwise there will be problems.

Execute the script, how effective is the test?

[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 did not appear because it was not online

Next, test whether the correct port is checked by the script

[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.

Judging from the above results, the script check result is correct. If you need to check the entire network segment, you only need to define the IP variable as "IP=" 172.16.1.0and24 "".

After reading this article, I believe you have some understanding of "sample Analysis of shell script programming in Linux system". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report