#!/bin/bash
Auto_Ssh(){
/usr/bin/expect <<EOF
spawn ssh root@$1 hostname
expect {
"yes/no"{
send "yes\r";exp_continue }"password"{
send "Asimov\r"}}
expect eof
EOF
}for IP in {
1..10}do
ping -c1 -w1 172.25.254.$IP &> /dev/null
if["$?"-eq"0"]
then
echo"`Auto_Ssh 172.25.254.$IP |tail -n 1 |sed 's/^M//g'` A 172.25.254.$IP" >> /mnt/host
fi
done
编写脚本实现创建指定文件中的用户并且设置用户的密码
vim userfile
user1
user2
user3
vim passwdfile
123
456
789
vim create_user.sh
#!/bin/bashif[ $# -lt 2 ]
then
echo"Error:please input two files following script!!!"
elif [!-e $1]
then
echo:"Error:$1 is not exit"
elif [!-e $2]
then
echo:"Error:$1 is not exit"
else
Num=`awk 'BEGIN{N=0}{N++}END{print N}' userfile`
for I in `seq 1 $Num`
do
USERNAME=`sed -n ${
I}P userfile`
PASSWD=`sed -n ${
I}P passwdfile`
getent passwd $USERNAME &> /dev/null
if["$?" = ""0 ]
then
echo$USERNAME is exitelse
useradd $USERNAMEecho$PASSWD|passwd --stdin $USERNAME >/dev/null
echo$USERNAME is create
fi
done
fi
sh create_user.sh userfile passwdfile
编写脚本实现10s监控一次系统的负载并且显示到shell终端界面
while true
do
clear #先将屏幕清空
uptime #查看系统负载sleep 3 #停留3s
done
编写脚本检测同一个机房里的服务器哪些是通的
#!/bin/bashfor IP in {
1..10}定义循环的变量以及范围
do
ping -c1 -w1 172.25.254.$IP &> /dev/null
["0"="$?"] && {
echo 172.25.254.&IP
}
done