some operation and maintenance commands

1. Filter all information of the process according to PID

#! /bin/bash

# Function: 根据用户输入的PID,过滤出该PID所有的信息

read -p "请输入要查询的PID: " P

n=`ps -aux| awk '$2~/^'$P'$/{print $11}'|wc -l`

if [ $n -eq 0 ];then

 echo "该PID不存在!!"

 exit

fi

echo "--------------------------------"

echo "进程PID: $P"

echo "进程命令:`ps -aux| awk '$2~/^'$P'$/{print $11}'`"

echo "进程所属用户: `ps -aux| awk '$2~/^'$P'$/{print $1}'`"

echo "CPU占用率:`ps -aux| awk '$2~/^'$P'$/{print $3}'`%"

echo "内存占用率:`ps -aux| awk '$2~/^'$P'$/{print $4}'`%"

echo "进程开始运行的时刻:`ps -aux| awk '$2~/^'$P'$/{print $9}'`"

echo "进程运行的时间:`ps -aux| awk '$2~/^'$P'$/{print $10}'`"

echo "进程状态:`ps -aux| awk '$2~/^'$P'$/{print $8}'`"

echo "进程虚拟内存:`ps -aux| awk '$2~/^'$P'$/{print $5}'`"

echo "进程共享内存:`ps -aux| awk '$2~/^'$P'$/{print $6}'`"

echo "--------------------------------"

2. Filter process information based on process name

All threads contained in the process name will be displayed


#! /bin/bash

# Function: 根据输入的程序的名字过滤出所对应的PID,并显示出详细信息,如果有几个PID,则全部显示

read -p "请输入要查询的进程名:" NAME

N=`ps -aux | grep $NAME | grep -v grep | wc -l` ##统计进程总数

if [ $N -le 0 ];then

  echo "该进程名没有运行!"

fi

i=1

while [ $N -gt 0 ]

do

  echo "进程PID: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $2}'`"

  echo "进程命令:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"

  echo "进程所属用户: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $1}'`"

  echo "CPU占用率:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $3}'`%"

  echo "内存占用率:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $4}'`%"

  echo "进程开始运行的时刻:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $9}'`"

  echo "进程运行的时间:` ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"

  echo "进程状态:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $8}'`"

  echo "进程虚拟内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $5}'`"

  echo "进程共享内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $6}'`"

  echo "***************************************************************"

  let N-- i++

done

3. Query the relevant information of the user according to the user name

#! /bin/bash

# Function: Query all information of the user according to the user name

read -p "Please enter the user name to be queried:" A

echo "------------------------------"

n=`cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}' | wc -l`

if [ $n -eq 0 ];then

echo "The user does not exist"

echo "------------------------------"

else

  echo "The user's username: $A"

  echo "The user's UID: `cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $3}'`"

  echo "The user's group is: `id $A | awk {'print $3'}`"

  echo "The GID of this user is: `cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $4}'`"

  echo "The user's home directory is: `cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $6}'`"

  Login=`cat /etc/passwd | awk -F: '$1~/^'$A'$/{print}'|awk -F: '{print $7}'`

  if [ $Login == "/bin/bash" ];then

  echo "This user has permission to log in to the system!!"

  echo "------------------------------"

  elif [ $Login == "/sbin/nologin" ];then

  echo "This user does not have permission to log in to the system!!"

  echo "------------------------------"

  fi

fi

4. Some configurations of the reinforcement system

#! /bin/bash

# Function: Some reinforcement of account passwords

read -p "The maximum number of days that the password can be set without modification:" A

read -p "Set minimum number of days between password changes:" B

read -p "Set the minimum password length:" C

read -p "Set the number of days before the password expires to notify the user:" D

sed -i '/^PASS_MAX_DAYS/c\PASS_MAX_DAYS '$A'' /etc/login.defs

sed -i '/^PASS_MIN_DAYS/c\PASS_MIN_DAYS '$B'' /etc/login.defs

sed -i '/^PASS_MIN_LEN/c\PASS_MIN_LEN '$C'' /etc/login.defs

sed -i '/^PASS_WARN_AGE/c\PASS_WARN_AGE '$D'' /etc/login.defs

echo "The password has been reinforced, the new user must not be the same as the old password, and the new password must contain numbers, lowercase letters, and uppercase letters at the same time!!"

sed -i '/pam_pwquality.so/c\password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= difok=1 minlen=8 ucredit=-1 lcredit=-1 dcredit=-1' /etc/pam.d/system-auth

echo "The password has been strengthened, if the wrong password is entered more than 3 times, the account will be locked!!"

n=`cat /etc/pam.d/sshd | grep "auth required pam_tally2.so "|wc -l`

if [ $n -eq 0 ];then

sed -i '/%PAM-1.0/a\auth required pam_tally2.so deny=3 unlock_time=150 even_deny_root root_unlock_time300' /etc/pam.d/sshd

fi

echo "The root user has been set to prohibit remote login!!"

sed -i '/PermitRootLogin/c\PermitRootLogin no' /etc/ssh/sshd_config

read -p "Set the number of historical commands to save:" E

read -p "Set account automatic logout time:" F

sed -i '/^HISTSIZE/c\HISTSIZE='$E'' /etc/profile

sed -i '/^HISTSIZE/a\TMOUT='$F'' /etc/profile

echo "It has been set that only users in the wheel group can use the su command to switch to the root user!"

sed -i '/pam_wheel.so use_uid/c\auth required pam_wheel.so use_uid ' /etc/pam.d/su

n=`cat /etc/login.defs | grep SU_WHEEL_ONLY | wc -l`

if [ $n -eq 0 ];then

echo SU_WHEEL_ONLY yes >> /etc/login.defs

fi

echo "Accounts in the system are about to be checked...."

echo "Users with login authority in the system are:"

awk -F: '($7=="/bin/bash"){print $1}' /etc/passwd

echo "********************************************"

echo "Users with UID=0 in the system are:"

awk -F: '($3=="0"){print $1}' /etc/passwd

echo "********************************************"

N=`awk -F: '($2==""){print $1}' /etc/shadow|wc -l`

echo "Users with empty passwords in the system: $N"

if [ $N -eq 0 ];then

 echo "Congratulations, there are no empty password users in the system!!"

 echo "********************************************"

else

 i=1

 while [ $N -gt 0 ]

 do

    None=`awk -F: '($2==""){print $1}' /etc/shadow|awk 'NR=='$i'{print}'`

    echo "------------------------"

    echo $None

    echo "A password must be set for an empty user!!"

    passwd $None

    l

Guess you like

Origin blog.csdn.net/huzia/article/details/127502939