This is the 29th day of my participation in the August More Text Challenge
1. Check the running status of the specified process
#! /bin/bash
NAME=httpd Enter the process name here
NUM=$(ps -ef |grep $NAME |grep -vc grep)
if [ $NUM -eq 1 ]; then
echo "$NAME running."
else
echo "$NAME is not running!"
fi
Copy the code
2. Check whether the user exists
#! /bin/bash
read -p "Please enter user :" user
for i in `awk -F : '{print $1}' /etc/passwd`
do
if [ $i= =$user ]
then
echo "User already exists!
exit
else
isfind=false
fi
done
echo "The user does not exist!"
Copy the code
3. Generate random numbers
#! /bin/bash
read -p "Please enter a randomly generated number of digits :" num
echo $RANDOM | cksum | cut -c 1-$num
Copy the code
4. Check whether the software package is installed
read -p "Please enter the software package to query :" software
if rpm -q $software &>/dev/null ;
then
echo "Package installed!"
else
echo "Package not installed!"
fi
Copy the code
5. Check whether the host is alive
ip_list="192.168.254.1 192.168.254.10" Define a host list
for ip in $ip_list;do
NUM=1
while [ $NUM -le 3 ];do
if ping -c 1 $ip > /dev/null;then
echo "$ipExist in the network!"
break
else
echo "$ipDoes not exist in the network!"
break
fi
done
done
Copy the code
6. Check the status of the specified host port
The HOST = 127.0.0.1The host being scanned
PORT="22 80 8080" # Specify the ports to scan
for PORT in $PORT; do # for loop
if echo &>/dev/null > /dev/tcp/$HOST/$PORT; then
echo "$PORT open"
else
echo "$PORT close"
fi
done
Copy the code
7. Obtain the HOST IP address
#! /bin/bash
IP=$(ip address show | awk '/ ^ [0-9] + : / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
echo "$IP"
Copy the code
8. Check whether the file or directory exists
#! /bin/bash
if [ $# -eq 0 ];then
echo "No parameters have been entered, please enter parameters"
echo "Usage:$0[file name | directory name]"
fi
if [ -f The $1 ]; then
echo "The file exists"
ls -l The $1
elif [ -d The $1 ]; then
echo "This directory exists"
ls -ld The $1
else
echo "The file or directory does not exist"
fi
Copy the code
9. Display system information
#! /bin/bash
#
HOSTNAME=`hostname`
IP=`ip a show ens33 | grep "inet" | grep -v "inet6" | awk '{print $2}' | cut -d'/' -f1`
SYSTEM=`cat /etc/centos-release`
CORE=`uname -r`
CPU=`cat /proc/cpuinfo | grep 'model name' |uniq | cut -d: -f2`
MEMORY=`free -mh | grep -i "mem" | awk '{print$2}'`
DISKNUM=`fdisk -l | grep -i "/dev" | grep -i "disk" | wc -l`
systemname=("HOSTNAME" "IP" "SYSTEM VERSION" "CORE VERSION" "CPU" "MEMORY SIZE")
systeminfo=("$HOSTNAME" "$IP" "$SYSTEM" "$CORE" "$CPU" "$MEMORY")
for s in `seq 1 $DISKNUM`;do
eval DISK$s=`fdisk -l | grep -i "/dev" | grep -i "disk" | head -$s | tail -1| awk '{print $2$3$4}' | cut -d', ' -f1`
eval systeminfo[$(($s+5=))]'$'DISK$s
systemname[$(($s+5=))]"DISK$s SIZE"
done
for i in The ${! systeminfo[*]};do
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"
printf "|%-15s| %-43s|\n" "${systemname[$i]}" "${systeminfo[$i]}"
done
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"
Copy the code
10. Check whether the entered IP address is an IP address
#! /bin/bash
IP=The $1
if [[ $IP= ~ ^ [0-9] {1, 3} \. [0-9] {1, 3} \. [0-9] {1, 3} \. [0-9] {1, 3} $]];then
FIELD1=$(echo $IP|cut -d. -f1)
FIELD2=$(echo $IP|cut -d. -f2)
FIELD3=$(echo $IP|cut -d. -f3)
FIELD4=$(echo $IP|cut -d. -f4)
if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ];
then
echo "$IPIt's an IP address!"
else
echo "$IPNot an IP address!"
fi
else
echo "IP address format is wrong!"
fi
Copy the code
Recommended reading
Linux Shell programming basics!
Linux Sudo and Sudoers
Samba server deployed on Linux!
Linux Zabbix 5.0 installation details!
Docker docker-compose: docker-compose
Docker Dockerfile file details!