Shell Application 4

Source: Internet
Author: User

1, write a small script named getarp.sh, log the MAC address of each host in the LAN:
1. Writing getarp.sh script files
(1) Send ARP request via arping command, record MAC address according to feedback result.
(2) Assign a network segment address (such as 192.168.4.) to the variable nadd as a prefix for the detection address.
(3) using the While loop statement, duplicate detection target and record MAC address, host address from 1-254.

[Email protected] ~]# VI getarp.sh
#!/bin/bash

    1. Define the network segment address, Mac list file
      Nadd= "192.168.4."
      File= "/etc/ethers"
    2. Send ARP requests and log feedback results
      [-F $FILE] &&/bin/cp-f $FILE $FILE. Old//Back up legacy files
      Hadd=1//define Start scan address
      While [$HADD-LT 128]
      Do
      Arping-i ens33-c 2 ${nadd}${hadd} &>/dev/null
      If [$?-eq 0]; Then
      Arp-n | grep ${nadd}${hadd} | awk ' {print $1,$3} ' >> $FILE
      Fi
      Let hadd++
      Done
      [Email protected] ~]# chmod +x getarp.sh
      [[email protected] ~]#./getarp.sh//Execute inspection procedure
      [Email protected] ~]# cat/etc/ethers
      2. Write a scan script named scanhost.sh, check which hosts are open anonymous FTP service, scan the object for all IP addresses in the/etc/ether file, scan port 21.

(1) There are many ways to detect whether a host to open the anonymous FTP service, here to take the wget download tool to access the FTP root directory, if you can successfully list, it is considered anonymous FTP is turned on, otherwise considered off.
(2) Assign a value to the variable target by filtering out all IP addresses in the/etc/ethers file via the awk command.
(3) Use the FOR Loop statement, read the IP address in the target variable, and repeatedly probe the FTP open situation.

[Email protected] ~]# VI scanhost.sh
#!/bin/bash
target=$ (awk ' {print '} '/etc/ethers)
echo "The following hosts are open for anonymous FTP service:"
For IP in $TARGET
Do
wget ftp://$IP/&>/dev/null
If [$?-eq 0]; Then
Echo $IP
RM-RF index.html//delete temporary files generated by the test
Fi
Done

3. Batch create user with For loop

#!/bin/bash
ulist=$ (Cat/root/users.txt)
For UNAME in $ULIST
Do
Useradd $UNAME
echo "123456" | passwd--stdin $UNAME &>/dev/null
Done

4, using while loop to determine the online host

#!/bin/bash
hlist=$ (Cat/root/a.txt)
i=130
While [$i-le 135]
Do
For IP in $HLIST
Do
Ping-c 3-i 0.2 $IP
If [$?-eq 0]
Then
echo "Host 192.168.8 $i is up" >>/1.txt
Else
echo "Host 192.168.8$i is down" >>/2.txt
Fi
Done
Let i++
Done

5, the use of script to determine whether the service started, no boot started
#!/bin/bash
Case "$" in
Start
Echo-n "Qidong"
If Sleep 7200 &
Then
echo "OK"
Fi
;;
Stop
Echo-n "Tingzhi"
Pkill "Sleep" &>/dev/null
echo "OK"
;;
Status
If Pgrep "Sleep" &> dev/null; Then
echo "Sleepqidong"
Else
echo "Sleeptingzhi"
Fi
;;
Restart
$ stop
$ start
;;
*)
echo "YONGFA: $ {Start|stop|status|restart}"
Esac

Shell Application 4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.