脚本一
要求:ping同一网段下1到10号机,若ping通,输出up,否则输出down
#!/bin/bash
for((i=0;i<=10;i++))
do
ping -c1 -w1 172.25.254.$i &> /dev/null && echo "$i is up" || echo "$i is down"
done
脚本二
要求:一分十秒的倒计时
#!/bin/bash
MIN=1
for ((SEC=10;SEC>=0;SEC--))
do
echo -ne "After ${MIN}:${SEC}s is end"
sleep 1
echo -ne "\r \r"
while [ "$SEC" -eq "0" -a "$MIN" -gt "0" ]
do
echo -ne "After ${MIN}:${SEC}s is end"
echo -ne "\r \r"
((MIN--))
SEC=60
done
done
脚本三
要求:通过从指定文件得到用户密码并创建用户,若用户存在,提示报错,若用户数和密码数不同,提示报错
#!/bin/bash
if
[ -n "$1" -a -n "$2" ]
then
if
[ -e "$1" -a -e "$2" ]
then
MAXUSER=`wc -l $1 | cut -d " " -f 1`
MAXPASS=`wc -l $2 | cut -d " " -f 1`
[ "$MAXUSER" -eq "$MAXPASS" ]&&(
for NUM in $( seq 1 $MAXUSER )
do
USERNAME=`sed -n ${NUM}p $1`
PASSWORD=`sed -n ${NUM}p $2`
CKUSER=`getent passwd $USERNAME`
[ -z "$CKUSER" ]&&(
useradd $USERNAME
echo $PASSWORD |passwd --stdin $USERNAME
)||echo "$USERNAME exist !!"
done
)||(
echo $1 and $2 have different lines
)
elif
[ ! -e "$1" ]
then
echo "ERROR:$1 is not exist"
else
echo "ERROR:$2 is not exist"
fi
else
echo "ERROR: Please input userfile and password file after command!!"
fi
脚本四
要求:本地创建用户文件和密码文件,通过ping同一网段的多台机器,若ping通,ssh连接机器,并在连接的机器上创建从用户文件和密码文件中读取的相关用户
#!/bin/bash
if
[ -n "$1" -a -n "$2" ]
then
if
[ -e "$1" -a -e "$2" ]
then
MAXUSER=`wc -l $1 | cut -d " " -f 1`
MAXPASS=`wc -l $2 | cut -d " " -f 1`
[ "$MAXUSER" -eq "MAXPASS" ]&&(
for NUM in $( seq 1 $MAXUSER )
do
USERNAME=`sed -n ${NUM}p $1`
PASSWORD=`sed -n ${NUM}p $2`
for (( i=2;i<=4;i++ ))
do
ping -c1 -w1 172.25.254.$i &> /dev/null
if [[ $? -eq 0 ]]
then
/home/kiosk/Desktop/ssh.exp 172.25.254.$i redhat
CKUSER=`getent passwd $USERNAME`
[ -z "$CKUSER" ]&&(
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
)||echo "$USERNAME exist!"
exit
else
echo "ERROR: Please input userfile and password file after command"
fi
done
)||(
echo $1 and $2 have different lines
)
else
echo 172.25.254.$i is down
fi
done
ssh.exp:
#!/usr/bin/expect
set IP [lindex $argv 0]
set PASS [lindex $argv 1]
set COMM [lindex $argv 2]
spawn ssh [email protected]$IP $COMM
expect {
"yes/no"
{send "yes\r";exp_continue}
"password:"
{send "$PASS\r"}
}
interact