#!/bin/bash
for i in {1..20}
do
read -p "please input passwd$i: " passwd
if [ "$i" -le 9 ]
then
user=test0$i
else
user=test$i
fi
if id -u $user &> /dev/null
then
adduser $user &> /dev/null
echo $passwd | passwd --stdin $user &> /dev/null
fi
done
2、使用for循环语句,ping测试指网段的主机,网段由用户输入
#!/bin/bash
read -p "Input: " ip
for i in $ip.{10..20}
do
ping -c 2 $i &> /dev/null
if [ "$?" -eq 0 ]
then
echo "$i successful"
else
echo "$i Failed"
fi
done
3、使用for实现批量主机root密码的修改,成功或失败都必须记录。
ssh-keygen -t RSA
read -p "newpasswd: " pass
while read ip
do
ping -c 2 $ip &> /dev/null
if [ "$?" -eq 0 ]
then
ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
if [ "$?" -eq 0 ]
then
echo "sending successly"
fi
ssh $ip echo $pass | passwd --stdin root
if [ $? -eq 0 ]
then
echo "$ip" >> /root/shell_code/ok_`date +%F`.txt
else
echo "$ip" >> /root/shell_code/fail_`date +%F`.txt
fi
else
echo "$ip" >> /root/shell_code/no_`date +%F`.txt
fi
done < /root/shell_code/host.txt