服务器批量配置免密,修改主机名
批量修改主机名
脚本内容如下:
#!/bin/bash
i=1
for group in `cat /home/group`
do
expect <<!
spawn ssh root@$group
expect {
"yes/no" {send "yes\r"; exp_continue}
"password:" {send "111111\r"; exp_continue}
}
send "hostnamectl set-hostname c$i\r"
expect {
" " {send "y/n"; exp_continue}
}
!
let i+=1
done
注:需要把所有服务器的ip,写入到脚本中的/home/group文件中。
批量配置服务器免密码登录
脚本一内容如下:
#!/bin/bash
for group in `cat /home/group`
do
expect <<!
spawn ssh root@$group
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "111111\r"; exp_continue }
}
send "ssh-keygen -t rsa\r"
expect {
"/root/.ssh/id_rsa" { send "\n"; exp_continue}
"Overwrite (y/n)?" { send "\n"; exp_continue}
"empty for no passphrase" { send "\n"; exp_continue}
"Enter same passphrase again:" { send "\n"; exp_continue}
}
!
done
脚本二内容如下:
#!/bin/bash
i=1
for group in `cat /home/group`
do
expect <<!
spawn scp root@$group:/root/.ssh/id_rsa.pub /root/.ssh/public_key$i
expect {
"Are you sure you want to continue connecting (yes/no)? " { send "yes\r"; exp_continue}
"password:" { send "111111\r"; exp_continue}
}
!
cat /root/.ssh/public_key$i >> /root/.ssh/authorized_keys
let i+=1
done
脚本三内容如下:
#!/bin/bash
i=1
for group in `cat /home/group`
do
expect <<!
spawn scp /root/.ssh/authorized_keys root@$group:/root/.ssh/
expect {
"Are you sure you want to continue connecting (yes/no)? " { send "yes\r"; exp_continue}
"password:" { send "111111\r"; exp_continue}
}
!
let i+=1
done
注:需要把所有服务器的ip,写入到脚本中的/home/group文件中,按顺序执行脚本