多主机推送公钥、修改配置、修改密码脚本

版权声明:本文为小胡子博主原创文章,转载请附上博文链接! https://blog.csdn.net/woshaguayi/article/details/89480765

推送公钥:

!#/bin/bash
if [! -f ~/.ssh/id_rsa ] ;then
     ssh-keygen -P "" -f ~/.ssh/id_rsa
fi

for i in `seq 100`
do 
  {
    ip=192.168.1.$i
    ping  -c1 $ip &>/dev/null
    if [ $? -eq 0];then
        echo $ip |tee addreee
        /usr/bin/expect<<-EOF
        set time 10
         spawn ssh-copy-id $ip
          expect {
                 "(yes/no)" {send "yes\r";exp_continue}
                 "password" {send "up\r"}
                 }
        expect eof
        EOF
    } &
done

wait

修改配置:

#!/bin/bash
for i in `cat address.txt`
do
{
 ssh $ip "sed -ri '/^#UseDns/c\UseDns no' /etc/ssh/sshd_config"
 
}&
done
wait

修改密码:

#!/bin/bash
read -p "please input password" :pass
for ip in `cat address.txt`
do
{
  ping -c1 -W1 $ip &>/dev/null
  if [ $? -eq 0];then
        ssh $ip "echo $pass | passwd --stdin root"&>/dev/null
     if [ $? -eq 0 ];then
         echo "$(date +%F) $ip">>ok.txt
      else
        echo "$(date +%F) $ip">>fail.txt
  fi
  else
    echo "$(date +%F) $ip">>fail.txt
fi
}&
done
wait

批量创建用户:

#!/bin/bash
pass="121212"
red=\e[1;31m
redset=\e[0m
if[ $# -eq 0];then
   echo "参数小于0"
fi
if [ -f $1 ];then
  echo "error file"
  return 2
fi
for user in `cat $1`
do
  id $user &>/dev/null
   if [ $? -eq 0 ];then
      echo "user is exits"
    else
    useradd $user
   echo $pass | passwd --stdin $user"&>/dev/null
    if [ $? -eq 0 ];then
     echo -e  "$red $user $redset created"
   fi
done

猜你喜欢

转载自blog.csdn.net/woshaguayi/article/details/89480765