时间格式:
判断用户是否存在:
修改Linux用户密码:
注意: !此处为取反。
1、批量添加用户:
1.1useradd.sh
#!/bin/bash
groupadd student1
for i in `seq 1 5`
do
useradd -G student student$i;
echo student$i | passwd student$i --stdin;
done
~
删除用户的命令:
1.2useradd.sh
#!/bin/bash
password="123456"
for USER in user1 user2 user3
do
useradd -m $USER
echo -e "${password}\n${password}" | passwd $USER
done
1.3useradd.sh
#!/bin/bash
DATE=$(date +%F__%T)
USER_FILE=user.txt
echo_color(){
if [ $1 == "green" ];then
echo -e "\e[32m$2\e[0m"
elif [ $1 == "red" ];then
echo -e "\033[31;40m$2\033[0m"
fi
}
if [ -s $USER_FILE ];then
mv $USER_FILE ${USER_FILE}-${DATA}.bak
echo_color green "$USER_FILE exits, rename $rename ${USER_FILE}-${DATE}.bak"
fi
echo -e "User \t Password" >> $USER_FILE
echo -e "----------------" >> $USER_FILE
for USER in stt{1..5};
do
if ! id $USER &> /dev/null;then
PASS=$(echo $RANDOM |md5sum | cut -c 1-8)
useradd $USER
echo $PASS | passwd --stdin $USER &>/dev/null
echo -e "$USER \t$PASS" >> $USER_FILE
echo_color green "$USER successfully created"
else
echo_color red "$USER already exist!"
fi
done
另外,echo -n是不换行输出:
1.4.1useradd.sh
#!/bin/bash
groupadd class1
i=1
while [ $i -lt 11 ]
do
if [ $i -le 9 ];then
USERNAME=stu0${i}
else
USERNAEM=stu${i}
fi
useradd -m $USERNAME
mkdir /home/$USERNAME
chown -R $USERNAME /HOME/$USERNAME
chgrp -R class1 /home/$USERNAME
i=$(($i+1))
done
1.4.2useradd.sh
#!/bin/bash
groupadd class2
for ((i=1;i<=10;i++))
do
if [ $i -lt 10 ]
then
USERNAME=stu0${i}
else
USERNAME=stu${i}
fi
useradd -M $USERNAME #-M表示不立即生成家目录
mkdir /home/$USERNAME
chown -R $USERNAME /home/$USERNAME
chgrp -R class2 /home/$USERNAME
done
~
~
2、批量修改远程主机密码
2passwd.sh
3、批量删除用户
3.1userdel.sh
#!/bin/sh
i=1
while [ $i -le 50 ]
do
userdel -r stu0${i}
i=$(($i+1))
done
3.2userdel.sh
#!/bin/bash
#set -vx
function echo_color () {
if [ $1 == "green" ];then
echo -e "\033[32m$2\033[0m"
elif [ $1 == "red" ];then
echo -e "\033[31m$2\033[0m"
elif [ $1 == "blue" ];then
echo -e "\033[34m$2\033[0m"
elif [ $1 == "yellow" ];then
echo -e "\033[33m$2\033[0m"
elif [ $1 == "purple" ];then
echo -e "\033[35m$2\033[0m"
elif [ $1 == "black" ];then
echo -e "\033[30m$2\033[0m"
elif [ $1 == "white" ];then
echo -e "\033[37m$2\033[0m"
fi
}
function get_user_account {
unset USER_ACCOUNT
ASK_COUNT=0
while [ -z "$USER_ACCOUNT" ]
# While no USER_ACCOUNT is given, keep asking.
do
ASK_COUNT=$[ $ASK_COUNT +1 ]
case $ASK_COUNT in
# If user gives no USER_ACCOUNT in time allotted(10s)
2)
echo
echo_color yellow "Pls answer the question."
;;
3)
echo
echo_color red "last try, Pls answer the question."
;;
4)
echo
echo_color red "Since you refuse to input anything..."
echo_color red "exiting program."
exit
;;
esac
if [ -n "$LINE2" ]
# if valiable LINE2 exits, Print 2 lines:
then
echo_color blue "$LINE1"
echo_color blue "$LINE2 \c"
# man echo搜索\\c可以看到,\c表示produce no further output,LINE2指定的字符串右侧其他字符不再屏显
else
# if not, Print 1 line:
echo_color blue "$LINE1"" \c"
fi
read -t 10 USER_ACCOUNT
done
# Do a little variable clean-up:
unset LINE1
unset LINE2
}
function get_answer {
unset ANSWER
ASK_COUNT=0
while [ -z "$ANSWER" ]
# While no answer is given, keep asking.
do
ASK_COUNT=$[ $ASK_COUNT +1 ]
case $ASK_COUNT in
# If user gives no answer in time allotted
2)
echo
echo_color yellow "Please answer the question."
echo
;;
3)
echo
echo_color red "One last try ... Please answer the question."
echo
;;
4)
echo
echo_color red "Since you refuse to answer the question..."
echo_color red "exiting program."
echo
exit
;;
esac
if [ -n "$LINE2" ]
# if valiable LINE2 exits, Print 2 lines:
then
echo_color blue "$LINE1"
echo_color blue "$LINE2"" \c"
else
# if not, Print 1 line:
echo_color blue "$LINE1"" \c"
echo
fi
# Allow 10 seconds to answer before time-out:
read -t 10 ANSWER
done
# Do a little variable clean-up:
unset LINE1
unset LINE2
}
function process_answer {
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES|YeS )
# If user answers "yes", do nothing:
;;
*)
# If user answers anything but "yes", exit scipt:
echo
echo_color red "$EXIT_LINE1"
echo_color red "$EXIT_LINE2"
echo
exit
;;
esac
# Do a little variable clean-up:
unset EXIT_LINE1
unset EXIT_LINE2
}
#########Main Script#########
echo_color blue "Step #1 -Determine User Account name to Delete"
LINE1="Please enter the username you wish to delete :"
get_user_account
### Check if USER_ACCOUNT valiable on this OS ###
USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)
# If the account is not found, exit scipt, else echo it:
if [ $? -eq 1 ]
then
echo
echo_color yellow "$USER_ACCOUNT not found. "
echo_color yellow "Leaving the script..."
echo
exit
fi
echo
echo_color green "I found this record:"
echo_color green $USER_ACCOUNT_RECORD
# Call the get_answer function:
LINE1="Is this the correct User Account? [y/n]"
get_answer
# Call process_answer function, if user answers anything but "yes", exit script:
EXIT_LINE1="Because the account $USER_ACCOUNT is not_ "
EXIT_LINE2="the one you wish to delete, we are leaving the script..."
process_answer
### Search for running processes belong to $USER_ACCOUNT ###
echo
echo_color blue "Step #2 - Find process on system belonging to user account"
echo
ps -u $USER_ACCOUNT > /dev/null
case $? in
1)
# No processes running for this User Account:
echo_color green "There are no processes for this account currently running."
echo
;;
0)
# Do have processes running for this User Account,
# Ask if killing them or not:
echo_color yellow "$USER_ACCOUNT has the following processes running: "
echo
echo_color yellow "`ps -u $USER_ACCOUNT`"
#
LINE1="Would you like me to kill the process(es)? [y/n]"
get_answer
#
case $ANSWER in
#If user answers "yes",kill User Account processes :
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES|YeS )
echo
echo_color green "Killing off process(es) of $USER_ACCOUNT..."
#
# List user processes running code in variable, COMMAND_1
COMMAND_1="ps -u $USER_ACCOUNT --no-heading"
#
# Create command to kill process in variable, COMMAND_3
COMMAND_3="xargs -d \\n -p /usr/bin/sudo /bin/kill -9"
#
# Kill processes via piping commands together
$COMMAND_1 | gawk '{print $1}' | $COMMAND_3
#
echo
echo "Process(es) killed."
;;
*) # If user answers anything but "yes", do not kill.
echo
echo "Will not kill the process(es)"
echo
;;
esac
;;
esac
### Create a report of all files owned by User Account ###
echo
echo_color blue "Step #3 - Find files on system belonging to $USER_ACCOUNT"
echo
echo "Creating a report of all files owned by $USER_ACCOUNT."
echo
echo "It's recommanded that you backup/archive these files,"
echo "and then do one of two things:"
echo " 1) Delete the files"
echo " 2) Change the files' ownership to a current user account."
echo
echo "Please wait. This may take a while..."
#
REPORT_DATE=$(date +%F)
REPORT_FILE=$USER_ACCOUNT"_"$REPORT_DATE".rpt"
#
find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null
#
echo
echo "Report is complete."
echo "Name of report: $REPORT_FILE"
echo "Location of report: $(pwd)"
echo
### Remove User Account ###
echo
echo_color blue "Step #4 - Remove user account"
echo
#
LINE1="Remove $USER_ACCOUNT's account from system? [y/n]"
get_answer
#
# Call process_answer function:
# if user answers anything but "yes", exit script
#
EXIT_LINE1="Since you do not wish to remove the user account,"
EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..."
process_answer
#
userdel $USER_ACCOUNT #delete user account
echo
echo "User account, $USER_ACCOUNT, has been removed"
echo
#
exit
4、定时备份
每月第一天备份并压缩/etc目录
存放于/root/bak目录
文件名为yymmdd_etc.tar.gz:yy为年,mm为月,dd为日
脚本名4fileback.sh存放在/usr/bin目录下
4fileback.sh
#!/bin/bash
SRC_DIR=/etc
DST_DIR=/root/bak
BACK=$(date +%Y%m%d)_etc.tar.gz
[ -d $DST_DIR ] || mkdir $DST_DIR
cd $DST_DIR && tar czvf $BACK $SRC_DIR &> $DST_DIR/cron.log && echo "etc backup finished!"
|| echo "Error, please check what happend!"
然后编写任务定时器:
echo "0 0 1 * * /bin/sh /usr/bin/fileback" > /root/etcbakcron
crontab /root/etcbakcron
或使用crontab -e 命令,再添加定时任务:
0 0 1 * * /bin/sh /usr/bin/fileback
检查crontab:
crontab -e
另外,请自行查阅crontab -e和vi /etc/crontab的区别
5、文件转移
要求能默写并解释其中一种(任选)
编写shell脚本:
将源目录下大于1024k的文件转移到目标目录下
a1wk.sh:反引号、if语句
#!/bin/bash
set -x
for FILE in `ls /root/shell/cnblog`
do
if [ -f $FILE ];then
if [ `ls -l $FILE | awk '{print $5}'` -gt 1024 ];then
cp $FILE /tmp
fi
fi
done
a2wk.sh:$()、if语句
#!/bin/bash
for FILE in $(ls /root/shell/cnblog)
do
if [ -f $FILE ] ; then
if [ $(ls -l $FILE | awk '{print $5}') -gt 1024 ] ; then
cp $FILE /tmp/
fi
fi
done
awk.sh:反引号、[[ ]]
#!/bin/bash
set -x
for FILE in `ls /root/shell/cnblog`
do
[[ -f $FILE ]] && [[ `ls -l $FILE | awk '{print $5}'` -gt 1024 ]] && cp $FILE /tmp/
done
两个中括号:可完全替代单个中括号
[[ -f $FILE ]] && [[ `ls -l $FILE | awk '{print $5}'` -gt 1024 ]]
[[ -f $FILE && ($(ls -l $FILE | awk '{print $5}') -gt 1024) ]]
a4wk.sh:在in里过滤
#!/bin/bash
for FILE in `ls -l /root/shell/cnblog
| awk '$5>1024'
| awk '{print $9}'`
#列出/root/shell/cnblog下的所有文件,然后管道送给awk,筛选出大于1024的行,再管道送给awk,打印这些行的第一列,即文件名
do
cp /root/shell/cnblog/$FILE /tmp
done
6.1、远程主机磁盘使用率自动报警
使用数组
6.1remote_df.sh
#!/bin/bash
#set -xv
FSMAX="50"
remote_user='root'
remote_ip=(127.0.0.1 192.168.159.129) #数组
ip_num="0"
while [ $ip_num -le $(expr ${#remote_ip[@]} - 1) ] #expr,运算符前后要有空格
#[]是取数组的元素,[@]是取数组所有元素,#表示计算数组元素个数
do
read_num='1'
ssh "$remote_user"@"${remote_ip[$ip_num]}" df -h > /tmp/diskcheck_tmp
grep '^/dev/*' /tmp/diskcheck_tmp | awk '{print $5}'|sed 's/\%//g' > /tmp/diskcheck_num_tmp
while [ "$read_num" -le $(wc -l < /tmp/diskcheck_num_tmp) ]
do
size=$(sed -n "$read_num"p /tmp/diskcheck_num_tmp)
if [ $size -gt $FSMAX ]
then
grep '^/dev/*' /tmp/diskcheck_tmp |sed -n $read_num'p' > /tmp/disk_check_mail
echo ${remote_ip[$ip_num]} >> /tmp/disk_check_mail
mail -v -s "diskcheck_alert" xxx@qq.com < /tmp/disk_check_mail
fi
read_num=$(expr $read_num + 1)
done
ip_num=$(expr $ip_num + 1)
done
6.2、本机磁盘使用率自动报警
6.2local_df.sh
#!/bin/bash
IP=$(ifconfig|sed -n '/broadcast/p'|awk '{print $2}')
SPACE=$(df -h|sed -n 2p|awk '{print $5}'|sed s/\%//)
#下面是if语句,只检查第一个分区
if [ $SPACE -ge 50 ]
then
echo "$IP分区使用率已超过50%"|mail -v -s "$IP分区使用率报警" xx@xx.com
fi
#或者用for循环,轮询所有分区
for i in $(df -h|awk '{print int($5)}')
do
if [ $i -ge 50 ]
then
echo "$IP分区使用率已超过50%"|mail -v -s "$IP分区使用率报警" xx@xx.com
fi
done