当前位置:网站首页>Shell script case branch statement, pick the max address of anonymous login FTP

Shell script case branch statement, pick the max address of anonymous login FTP

2022-07-19 02:47:00 For whom do the stars change

1、 Sentence structure

 case  A variable's value   in
Model a )  Command sequence 1
;;
Model 2 ) Command sequence 2
;;

......
*)
esac

2、case Statement application example  

        (1) Check the character type entered by the user
Prompt the user to enter a character from the keyboard , adopt case Statement to determine whether the character is a letter 、 Numbers or other control characters

vim a.sh

#!/bin/bash
read -p " Please enter a character , And press Enter Key confirmation :" KEY
case "$KEY" in
[a-z] |  [A-Z])                            ( Match any letter )
        echo  " You entered the letters ."
        ;;
[0-9])                                    ( Match any number )
        echo " You have entered a number ."
        ;;
*)                                        ( The default mode , Match any character )

        echo " You have entered a space 、 Function keys or other control characters ."
esac


chmod +x a.sh
./a.sh

        (2)  System scripting service
By position variable $1 designated start, stop, restart, status Control parameter , Respectively used to start 、 stop it 、 restart sleep process

vim fuwu.sh

#!/bin/bash
case "$1" in
start)
echo -n " Starting sleep service ..."
        if sleep 9000  &
then
        echo "OK"
fi
;;

stop)
echo  -n " Stopping sleep service ..."
pkill "sleep" &> /dev/null
echo "OK"
;;
status)
if pgrep "sleep" &> /dev/null
then
        echo "sleep Service started ."
else
        echo "sleep The service has stopped ."
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo " usage :$0 {start|stop|status|restart}"
esac


chmod +x fuwu.sh
./fuwu.sh start
./fuwu.sh stop
./fuwu.sh status
./fuwu.sh restart

         (3) adopt arping Command send ARP request , Record according to the feedback results MAC Address .( Need one vsftp The server )

Add network segment address ( Such as 192.168.1.) Assign a value to a variable NADD, As the prefix of the detection address .
Use while Loop statement , Repeatedly detect the target and record MAC Address , Host address from 1-254.
 

vim getarp.sh

#!/bin/bash
# 1.  Define the network segment address 、MAC List file 
NADD="192.168.1."
FILE="/etc/ethers"
# 2.  send out ARP request , And record the feedback results 
[ -f $FILE ] && /bin/cp -f $FILE $FILE.old      		( Back up the original file )
HADD=1                                          		( Define the starting scanning address )
while [ $HADD -lt 30 ]
do
    ping -c 2  -i 0.2 -W  3   ${NADD}${HADD} &> /dev/null
    if [ $? -eq 0 ] 
then
       arp -n | grep ${NADD}${HADD} | awk '{print $1,$3}' >> $FILE
    fi
    let HADD++
done


chmod +x getarp.sh
./getarp.sh  
cat /etc/ethers


        (4)  Check whether a host is anonymous FTP service

wget Download tools to visit FTP The way of root directory , If you can successfully list , Is considered anonymous FTP Enabled , Otherwise, it is regarded as closed
 

vim scanhost.sh

#!/bin/bash
TARGET=$(awk '{print $1}' /etc/ethers)
echo " The following hosts have been opened for anonymity FTP service :"
for IP in $TARGET
do
    wget ftp://$IP/ &> /dev/null
        if [ $? -eq 0 ] 
then
            echo $IP
            rm -rf index.html               		( Delete the temporary file generated by the test )
        fi
done

chmod +x scanhost.sh
./scanhost.sh

原网站

版权声明
本文为[For whom do the stars change]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170009270144.html