Writing script rules
1. Declaration interpreter
#!/bin/bash
bash benefits: history, tab, pipeline, redirection, alias
2. Notes
3. Code
1. Write a script to create a user, enter the user name and password, enter the user name as empty, and exit the script
#! /bin/bash read -p 'Please enter the user name you want to create' a [ -z $a ] && echo "The entered user name is empty" && exit //Enter null exit script useradd $a &> /dev/null echo user $a Created successfully stty -echo #Turn off echo read -p 'Please enter the password you want to set' b echo $b | passwd --stdin $a &> /dev/null # /dev/null system black hole stty echo #Turn on echo echo user $a Password set successfully
2. Write a script to realize the whole process of vsftpd service package configuration and service startup, and enable the upload function
#!/bin/bash yum list installed | grep vsftp &> /dev/null #Test whether vsftpd service is installed, and exit after installation [ $? == 0 ] && echo "already installed" && exit yum -y install vsftpd &>/dev/null #Install vsftpd service sed -i 's/^#anon_upload/anon_upload/' /etc/vsftpd/vsftpd.conf #Modify profile systemctl restart vsftpd #Restart service systemctl enable vsftpd #Set startup and self startup chmod o+w /var/ftp/pub #Give ftp directory permission systemctl stop firewalld #Pause firewall setenforce 0 #Pause selinux
3. Write a script and check the number of system login users every 2 minutes. If there are more than 3 users, send an email to the administrator
#! /bin/bash a=`who | wc -l` #Check the current login number [ $a -gt 3 ] && echo "More than three users." | mail -s test root
[root@ha131 ~]# chmod 755 ceshi.sh
[root@ha131 ~]# crontab -e
*/2 * * * * /root/cehsi.sh #Every two minutes
4. Test whether the network is connected
Tips:
ping -c 3 ip # ping ends three times
ping -i 0.2 # sets the ping speed at an interval of 0.2
ping -W 1 # sets the time when results are returned
#! /bin/bash ping -c 3 -i 0.2 -W 1 www.baidu.com &>/dev/null if [ $? -eq 0 ];then echo "No problem" else echo "something the matter" fi
5. Test whether it is connected with the target ip
#! /bin/bash read -p "input ip" a ping -c 3 -i 0.2 -W 1 $a &>/dev/null if [ $? -eq 0 ];then echo "$a To connect" else echo "$a Unconnected" fi
6.for loop test whether it is connected with the target ip
#! /bin/bash for i in `seq 10` #Prepare ten values [1 ~ 10] do ping -c 3 -i 0.2 -W 1 192.168.107.$i &>/dev/null if [ $? -eq 0 ];then echo "192.168.107.$i connected" else echo "192.168.107.$i Unconnected" fi done
7. Guess random numbers
Result: guess big
Guess it's small
You guessed right
#! /bin/bash a=$[RANDOM%11] read -p "Please enter a number" n if [ $a -eq $n ];then echo "You guessed right" elif [ $a -gt $n ];then echo "Guess big" else echo "Guess it's small" fi
8.while loop optimization guessing random numbers
#! /bin/bash a=$[RANDOM%101] b=0 echo $a while : do let b++ read -p "Please enter a number(0~100)" n if [ $a -eq $n ];then echo "You guessed right,I guess $b second" exit elif [ $a -gt $n ];then echo "Guess it's small" else echo "Guess big" fi done
9. Write a one click Install nginx script
Make sure that the tar package is installed first by nginx
This case uses nginx1 Version 20.1
#!/bin/bash yum -y install gcc openssl-devel pcre-devel #Install the appropriate environment tar -xf /nginx/nginx-1.20.1.tar.gz -C /nginx/nginx-1.20.1 #Destination path for decompression cd nginx-1.20.1/ #Enter directory ./configure #Perform installation make make install
Test nginx service. If there is httpd service in the computer, please close it.
systemctl stop firewalld #Pause the firewall first to prevent affecting the results
Start test
Test successful
10. Write a one click Start nginx or close script
#! /bin/bash case $1 in st|start) netstat -ntulp | grep -q nginx if [ $? -eq 0 ];then echo "Service started" else /usr/local/nginx/sbin/nginx fi;; stop) /usr/local/nginx/sbin/nginx -s stop;; rest) /usr/local/nginx/sbin/nginx -s stop /usr/local/nginx/sbin/nginx;; *) echo "st||stop||rest||start" esac
11. Test the ip connectivity of the current network segment
#! /bin/bash myping(){ #Define shell functions for easy calling ping -c 1 -W1 $1 &> /dev/null if [ $? -eq 0 ];then echo "$1 up" else echo "$1 down" fi } for i in {1..254} do myping 192.168.108.$i & done wait #The wait command is used to wait for all background processes to finish before ending the script
12. Enter numbers on the keyboard, enter 0 to end, and calculate the cumulative sum
#! /bin/bash x=0 while : do read -p "Enter a number (end with 0)" n if [ $n -eq 0 ];then break fi if [ -z $n ];then echo "Please re-enter" fi let x+=n done echo "The cumulative sum is $x"
13. The output is a multiple of 6 within 20, plus ten output
#! /bin/bash for i in `seq 20` do a=$[i%6] [ $a -ne 0 ] && continue #If the remainder is not 0, end this cycle echo $[i+10] done
14. How to get eight random characters (random password)
#! /bin/bash a=qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789 for i in {1..8} do b=$[RANDOM%62] c=${a:$b:1} pass=$pass$c done echo $pass
15. Optimize user script creation (no password is entered, default password 123 is used)
#! /bin/bash read -p "Please enter the user name you want to create: " a [ -z $a ] && echo "The user name is blank" && exit # Enter null exit script useradd $a &> /dev/null echo user $a Created successfully stty -echo #Turn off echo read -p 'Please enter the password you want to set' b echo ${b:-123} | passwd --stdin $a &> /dev/null stty echo #Turn on echo echo user $a Password set successfully
16. Use sed and string processing to determine whether the yum warehouse is available (centos7)
#!/bin/bash a=`yum repolist | sed -n '$p'` x=`echo ${a#* } | sed 's/,//'` [ $x -eq 0 ] && echo "yum Not available" && exit || echo "yum available"
17. Comprehensive application of SED script
Write a script getpass SH, implement the requirements
1. Find the user who uses bash as the login
2. List the shadow Password records used
3. Save "user name - > password record" in each line to getpass Log
#!/bin/bash A=$(sed -n '/bash$/s/:.*//P '/ etc / passwd) # find the user whose login is bash for i in $A do pass1=$(grep $i /etc/shadow) #Find a line for the user password pass2=${pass1#*:} #Delete from left to right to the first: pass=${pass2%%:*} #Delete from right to left to the last: echo "$i --> $pass" done
18. Comprehensive application of awk script
Write a script getpass SH, realize the requirements
1. Find the user who uses bash as the login
2. List the shadow Password records used
3. Save "user name - > password record" in each line to getpass Log
#!/bin/bash A=`awk -F: '/bash$/{print $1}' /etc/passwd` #First find out which users use the explanation Device IV bash,Find these users and assign their names to the variables A for i in $A #These found user names are handed over to the for loop in turn do grep $i /etc/shadow | awk -F: '{print $1 "-->" $2}' #If the account in the first cycle is root, grep root can find the corresponding line in the shadow document, including the user and password, and use awk to filter out the user name in the first column -- > the password in the second column done
19. awk extended application
Edit a script to display the average cpu load, hard disk space, network card traffic, number of installed software packages, number of accounts, and number of currently logged in accounts
uptime | awk '{print"cpu Average load" $9,$10,$11}' #Free memory information free -m view in mb #!/bin/bash while : do echo "###########################" uptime | awk '{print"cpu Average load" $9,$10,$11}' free -m | awk '/^Mem/{print"Host remaining memory"$4"M"}' df -h | awk '/sda3/{print "Main hard disk remaining space"$4}' ifconfig ens192 | awk '/RX p/{print "Primary network card ens192 The amount of data received is"$5}' ifconfig ens192 | awk '/TX p/{print "Main network card ens192 The amount of data sent is"$5}' A=`rpm -qa |wc -l` echo "There are software packages installed on the current host $A individual " awk '{a++} END{print"Host owned account" a"individual"}' /etc/passwd x=`who |wc -l` echo "Current total login $x Users" a=`ps aux |wc -l` #ps aux view the detailed process of this machine echo "The processes running on the host share $a individual" echo "################################" sleep 3 clear done
21. Write the monitoring script. When someone enters the local password incorrectly for more than three times, send an email to root
#!/bin/bash x=`awk '/Failed/{ip[$11]++} END{for(i in ip){print i","ip[i]}}' /var/log/secure` for i in $x do p=${i%,*} #ip b=${i#*,} # Number of incorrect password entries [ $b -gt 3 ] && echo "$p Failed to access this computer $b second" | mail -s test1 root done