Author:JackTian
Source: Public account “Jackie’s IT Journey”
ID:Jake_Internet
Link: 10 Shell Script Examples You Can Use Now!
Script 1: This script is used to read all /bin/bash
in the /etc/passwd
file of the Linux
system >A list of users that log in to the default Shell
and extract the username of each user. Check whether these user names are not equal to root
and tidb
. If the user name is not equal to root
and tidb
, use the usermod
command to modify the user’s default Shell
to: /sbin/nologin
, which is usually used to prohibit users from logging into the system remotely.
#!/bin/bash for user in $(cat /etc/passwd | grep /bin/bash | cut -d ":" -f 1) do if [ $user != "root" ] & amp; & amp; [ $user != "tidb" ]; then usermod -s /sbin/nologin $user fi done
Script 2: This script first customizes the two files md5_list
and md5_no_hash.txt
, and then reads md5_list
The hash value of each line in the file is used to determine whether it exists in the md5_no_hash.txt
file.
If it is checked that the hash value exists in md5_no_hash.txt
, then print information: MD5 value xxx exists in md5_no_hash.txt
.
If it is checked that the hash value does not exist in md5_no_hash.txt
, the message will be printed: MD5 value xxx does not exist in md5_no_hash.txt
.
#!/bin/bash #Specify the paths to the md5_list file and md5_no_hash.txt file md5_list_file="md5_list" md5_no_hash_file="md5_no_hash.txt" # Loop through each line in the md5_list file while IFS= read -r md5; do # Check whether md5 is in md5_no_hash.txt if grep -q "$md5" "$md5_no_hash_file"; then echo "MD5 value $md5 exists in $md5_no_hash_file" else echo "MD5 value $md5 does not exist in $md5_no_hash_file" fi done < "$md5_list_file"
Script 3: The main function of this script is to traverse and read a file containing the IP
address. For example, the file name is: mmyd
, and then Perform a ping
operation on each line of the IP
address in the file and check the packet loss rate. If the packet loss rate of ping
is: 100%, then output the unreachable log of the IP
; if there is no packet loss rate of ping
, then Output the reachable log of the IP
.
#!/bin/bash # author:JackTian # Get the current system time current_time=$(date + "%Y-%m-%d-%H:%M:%S") # Get the start timestamp start_time=$(date + %s) for i in `cat mmyd` # This is to read all the IP addresses listed in the file one by one do ping=`ping -c 10 $i | grep loss | awk '{print $6}' | awk -F "%" '{print $1}'` # The above line is to ping the read IP once to see if the ping succeeds. And take out the value of the percentage of loss, which is the value of packet loss, to see if there is packet loss. Packet_Loss_Rate=`ping -c 10 $i | grep loss | awk '{print $6}'` if [ $ping -eq 100 ];then # Compare and see if the value of packet loss is 100. If it is 100, all packets are lost, that is, the ping failed, and the IP ping failed. If it is not equal to 100, the ping was successful, and the IP ping was displayed. It worked. echo "$current_time XX mobile-active IP address: $i ping failed, packet loss rate: $Packet_Loss_Rate" >>/opt/jacktian/mmyd_ping.log else echo "$current_time XX mobile-active IP address: $i ping successful!" >>/opt/jacktian/mmyd_ping.log fi done # Get the end timestamp end_time=$(date + %s) # Calculate execution time (seconds) execution_time_seconds=$((end_time - start_time)) # Convert execution time to minutes and seconds minutes=$((execution_time_seconds / 60)) seconds=$((execution_time_seconds % 60)) echo "Execution completed! Total execution time of this script: $minutes minutes $seconds seconds"
Script 4: This script is used to create a new user in the Linux
system and set the password, province code, directory permissions and vsftpd
for the user Configuration of the service.
#!/bin/sh read -p "user:" user read -p "passd:" pass read -p "province:" province useradd $user -d /bigdata/sftp/province/$province/ #passwd $user echo $pass | passwd --stdin $user chown $user /bigdata/sftp/province/$province/ -R chmod 750 /bigdata/sftp/province/$province/ -R echo $user>>/etc/vsftpd/chroot_list echo $user>>/etc/vsftpd/user_list systemctl restart vsftpd.service
read -p "user:" user
: Prompts the user to enter a user name and stores the entered value in the variableuser
read -p "passd:" pass
: Prompts the user to enter a password and stores the entered value in the variablepass
. NOTE: When entering your password, no characters will be displayedread -p "province:" province
: prompts the user to enter a province code and stores the entered value in the variableprovince
useradd $user -d /bigdata/sftp/province/$province/
: This command will create a new user whose user name is the previously entereduser
and whose home directory For:/bigdata/sftp/province/$province/
echo $pass | passwd --stdin $user
: This command will pass the previously entered password to thepasswd
command through standard input to set the password for the newly created user.chown $user /bigdata/sftp/province/$province/ -R
: This command will change the newly created user to the/bigdata/sftp/province/$province/
directory and the owner of its subdirectorychmod 750 /bigdata/sftp/province/$province/ -R
: This command will set the permissions of the/bigdata/sftp/province/$province/
directory and its subdirectories It is:750
, which means: the user has read, write, and execute permissions, while the user group only has read and execute permissions.echo $user>>/etc/vsftpd/chroot_list
: This command will add the newly created user to thechroot
list of thevsftpd
service. This means that when thevsftpd
service is running, the user will be restricted to his own directory and cannot access other directories on the system.echo $user>>/etc/vsftpd/user_list
: This command will add the newly created user to the user list of thevsftpd
service. This means that this user can log in and access the system while thevsftpd
service is running.systemctl restart vsftpd.service
: Restart thevsftpd
service to make all previous configurations effective
Script 5: This script is used to create a new user in the Linux
system and set the password, province code, idcid, directory permissions and vsftpd for the user. code>Configuration of the service. Slightly different from Script 4 above.
#!/bin/sh read -p "user:" user read -p "passd:" pass read -p "province:" province read -p "idcid:" idcid mkdir -p /bigdata/sftp/province/$province/$idcid useradd $user -d /bigdata/sftp/province/$province/$idcid #passwd $user echo $pass | passwd --stdin $user chown $user /bigdata/sftp/province/$province/$idcid/ -R chmod 750 /bigdata/sftp/province/$province/$idcid -R echo $user>>/etc/vsftpd/chroot_list echo $user>>/etc/vsftpd/user_list systemctl restart vsftpd.service
Script 6: This script first customizes the province code list into multiple directory paths and loops through the customized province code list. For each province code, the script divides it into three parts: province code, operator and data reporting type. Then enter the corresponding province directory.
Execute the du -sh 2023-10-*
command to query the sizes of all directories starting with 2023-10-
, and output the results to a file named $province_code_file_size .txt
file.
Execute a for
loop, loop through all subdirectories and query the number of files in each subdirectory, and then output the results to a file named $province_code_file_count.txt
.
Finally, the script will return to the upper-level directory to query the file size and number of files under the next province code. All query results will be saved in files named with province codes in the /opt/
directory.
#!/bin/bash # @Time : 2023/10/30 # @Author : jacktian # @Desc: This is a script suitable for a server to circularly query the file size and number of files in a specific province, a specific operator, and a specific data reporting type. # Define province code list provinces=("110000/dianxin/1024" "120000/liantong/1024" "130000/yidong/1024") # Traverse the province code list for province in "${provinces[@]}"; do # Split province codes, operators and data reporting types province_code=$(echo $province | cut -d'/' -f1) operator=$(echo $province | cut -d'/' -f2) category=$(echo $province | cut -d'/' -f3) # Enter the province directory cd /bigdata/sftp/province/$province_code/$operator/$category # Execute the du command to query the file size and output the result to the corresponding province coded txt file du -sh 2023-10-* >> /opt/"$province_code"_file_size.txt # Execute the for command to query the number of date files and output the results to the corresponding province code txt file for date in $(ls -d */ | cut -d'/' -f1); do echo $date $(ls -1 $date | wc -l) >> /opt/"$province_code"_file_count.txt done # Return to the upper directory cd.. done
Script 7: This script is used to query the file size and number of files in a specific province and specific data reporting type in a loop. Slightly different from script 6 above.
#!/bin/bash # @Time : 2023/10/30 # @Author : jacktian # @Desc: This is a script suitable for a server to query the file size and number of files in a specific province and specific data reporting type in a loop. # Define province code list provinces=("110000" "120000" "130000") # Traverse the province code list for province in "${provinces[@]}"; do # Enter the province directory cd /bigdata/sftp/province/$province/1024 # Execute the du command to query the file size and output the result to the corresponding province coded txt file du -sh 2023-10-* >> /opt/"$province"_file_size.txt # Execute the for command to query the number of date files and output the results to the corresponding province code txt file for date in $(ls -d */ | cut -d'/' -f1); do echo $date $(ls -1 $date | wc -l) >> /opt/"$province"_file_count.txt done # Return to the upper directory cd.. done
Script 8: This script is mainly used to record the modifications of certain XML files within a specific time period and save the results in the log.
#!/bin/bash # Get the current date current_date=$(date + %Y-%m-%d) # First command directory_path="/bigdata/sftp/province/110000/yidong/1024/$current_date" output_file="/opt/log_110000_yidong_$current_date.txt" cd "$directory_path" & amp; & amp; ls -l *.xml | awk '{print $8,$9}' | grep -v '^$' | awk -F '[/: ]' '{hour=substr($1,1,2); if ((hour >= "00" & amp; & amp; hour < "08") || (hour >= " 10" & amp; & amp; hour < "12") || (hour >= "14" & amp; & amp; hour <= "24")) print}' >> "$output_file" # The second command directory_path="/bigdata/sftp/province/120000/dianxin/1024/$current_date" output_file="/opt/log_120000_dianxin_$current_date.txt" cd "$directory_path" & amp; & amp; ls -l *.xml | awk '{print $8,$9}' | grep -v '^$' | awk -F '[/: ]' '{hour=substr($1,1,2); if ((hour >= "00" & amp; & amp; hour < "08") || (hour >= " 10" & amp; & amp; hour < "12") || (hour >= "14" & amp; & amp; hour <= "24")) print}' >> "$output_file" # The third command directory_path="/bigdata/sftp/province/130000/liantong/1024/$current_date" output_file="/opt/log_130000_liantong_$current_date.txt" cd "$directory_path" & amp; & amp; ls -l *.xml | awk '{print $8,$9}' | grep -v '^$' | awk -F '[/: ]' '{hour=substr($1,1,2); if ((hour >= "00" & amp; & amp; hour < "08") || (hour >= " 10" & amp; & amp; hour < "12") || (hour >= "14" & amp; & amp; hour <= "24")) print}' >> "$output_file" #Exit process exit
First, use the date
command to get the current date in the format: YYYY-MM-DD
, and customize the current_date
variable.
Then, a directory path directory_path
and output file output_file
were customized.
Use ls -l *.xml
to list all files ending with .xml
, and use awk '{print $8,$9}'
Extract the modification time and size information of the file, grep -v '^$'
filter out blank lines, awk -F '[/:]' '{hour=substr ($1,1,2); if ((hour >= "00" & amp; & amp; hour < "08") || (hour >= "10" & amp; & amp; hour < "12") || (hour >= "14" & amp; & amp; hour <= "24")) print}'
Filter out daily Files modified during the 00:00-07:59, 10:00-11:59, 14:00-23:59
time periods, and output their modification time and size information, and append the results to the specified in the output file.
Script 9: This script is mainly used to monitor system resource usage, obtain and record disk usage, CPU idleness, memory idleness and the total number of processes, and output these print information to a log in the file.
#!/bin/bash date=$(date + %Y-%m-%d-%H:%M:%S) #1. Print disk usage DISK_1=$(df -h | awk '{printf $NF} {printf "usage:"} {print $5} '| grep appslog | grep -v 'Filesystem') DISK_2=$(df -h | awk '{printf $NF} {printf "usage:"} {print $5} '| grep bigdata | grep -v 'Filesystem') DISK_3=$(df -h / | awk '{printf $NF} {printf "usage:"} {print $5} '| grep / | grep -v 'Filesystem') #2. Print CPU idle status CPU=$(top -n 1 | grep Cpu | awk 'BEGIN {printf"CPU idle usage:"} {print $8}') #3. Print memory free status MEMORY=$(free -h | awk 'BEGIN {printf"Memory free usage:"} NR==2 {print $4}') #4. Print the total number of processes JINCHENG=$(ps aux | wc -l | awk 'BEGIN {printf"Total number of processes:"} {print $1}') echo -e "\\ $date\\ \\ $DISK_1\\ \\ $DISK_3\\ \\ $CPU\\ \\ $MEMORY\\ \\ $JINCHENG \\ " >> /opt/jacktian/inspection.log exit done
date=$(date + %Y-%m-%d-%H:%M:%S)
: Get the current date and time in the format: year-month-day-hour:minute :SecondDISK_1, DISK_2, DISK_3
: This variable is used to obtain disk usage. Use thedf -h
command to obtain disk information, and then useawk
to extract the usage and file system name.grep
is used to filter out disks with specific names (such as appslog and bigdata)CPU
: This variable obtains the idle usage ofCPU
. Use thetop -n 1
command to obtain the system status, and then usegrep
andawk
to extract the idle usage ofCPU
MEMORY
: This variable obtains the free usage of memory. Use thefree -h
command to obtain memory information, and then useawk
to extract the free memory usageJINCHENG
: This variable obtains the total number of processes in the system. Use theps aux
command to obtain process information, and then use thewc -l
command to count the number of lines, that is, the total number of processesecho -e "\\
: This part splices all the above printing information together and outputs it to the log file
$date\\
\\
$DISK_1\\
\\
$DISK_3\\
\\
$CPU\\
\\
$MEMORY\\
\ n $JINCHENG\\
"
Script 10: This script is mainly used to regularly cycle the access status of the FTP server, and send alarm information through the enterprise WeChat robot when there is an abnormality.
#!/bin/bash # FTP IP list FTP_IPS=("IP_1" "IP_2" "IP_3") #Enterprise WeChat robot address (needs to be configured according to the actual robot address) WEBHOOK_URL=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=XXX #Loop execution detection method while : do date=$(date + %Y-%m-%d-%H:%M:%S) # Loop through the FTP IP list for ip in "${FTP_IPS[@]}" do # Connect to FTP server ftp -n $ip <<EOF # Exit FTP server exit EOF # Check the exit status code of the previous command if [ $? -ne 0 ]; then # If the exit status code is not 0, it means that the command execution failed and an abnormal alarm will be sent to the enterprise WeChat robot. curl --location --request POST ${WEBHOOK_URL} \ --header 'Content-Type: application/json' \ -d '{"msgtype": "markdown", "markdown": {"content": "'$date' FTP access exception: $ip"}}' echo "$date FTP access exception:$ip" >>/opt/ftp_check/ftp_check.log else # If the exit status code is 0, it means the command was successfully executed and no normal alarm will be sent to the enterprise WeChat robot. echo "$date FTP access is normal:$ip" >>/opt/ftp_check/ftp_check.log fi done exit done
FTP_IPS
: This variable contains theIP
addresses of multipleFTP
serversWEBHOOK_URL
: This variable is the address of the enterprise WeChat robot. You need to replace the XXX part with the actual enterprise WeChat robot'skey
- A
while
loop is used to perform detection periodically. In each loop, the current date and time will be obtained and theFTP_IPS
list will be traversed. For eachIP
address in the list, the script attempts to connect to theFTP
server - If the connection command
ftp -n $ip
fails, the exit status code is not:0
, which meansFTP
access exception. In this case, the script will send an exception alert to the enterprise WeChat robot and write relevant information to the log file - If the connection command is executed successfully and the exit status code is:
0
, it means thatFTP
access is normal. The script will not send alarm information, but will only record normal access information in the log.
That’s all I want to share today.
If you think this article is of some use to you, please give it a like, leave a comment or forward it so that more friends can see it, because this will be my greatest strength in continuing to output more high-quality articles. power!