标签:else useradd ips nfa 自动 distrib set argc hosts
#1、添加用户gongli
useradd test01
echo 123456|passwd --stdin test01
#2、配置sudoers:
echo "test01 ALL= NOPASSWD:ALL" >> /etc/sudoers
visudo -c
id test01
su - test01
自动生成密钥、分发公钥、多台服务器一键安装vsftpd服务:
[test01@m01 ~]$ vim auto_deploy.sh
#!/bin/sh
. /etc/init.d/functions
#1、Create key pair
ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa >/dev/null 2>&1
if [ $? -eq 0 ]; then
action "create secret key" /bin/true
else
action "create secret key" /bin/false
exit 1
fi
#2、distribute the public key
for ip in 8 31 41 51
do
expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 172.16.1.$ip >/dev/null 2>&1
if [ $? -eq 0 ]; then
action "172.16.1.$ip" /bin/true
else
action "172.16.1.$ip" /bin/false
fi
done
#3、distribute scripts
for n in 8 31 41 51
do
if [ $n -eq 31 ]; then
scp -P 52113 -rp ~/scripts/ test01@172.16.1.$n:~
else
scp -P 22 -rp ~/scripts/test01@172.16.1.$n:~
fi
done
#4.install the VSFTP service
for m in 8 31 41 51
do
if [ $m -eq 31 ]; then
ssh -t -p 52113 test01@172.16.1.$m sudo bash ~/scrips/install.sh
else
ssh -t -p 22 test01@172.16.1.$m sudo bash ~/scrips/install.sh
fi
done
[test01@m01 ~]$ vim fenfa_sshkey.exp
#!/usr/bin/expect
if { $argc != 2 } {
send_user "usage: expect fenfa_sshkey.exp file host\n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
#set dir [lindex $argv 2]
set password "123456"
#set ip2 "172.16.1.31"
#spawn scp /etc/hosts root@10.0.0.142:/etc/hosts
#spawn scp -P52113 $file test01@$host:$dir
if { $host == "172.16.1.31" } {
spawn ssh-copy-id -i $file "-p 52113 test01@$host"
expect {
"yes/no" { send "yes\r";exp_continue }
"*password" { send "$password\r" }
}
expect eof
exit
} else {
spawn ssh-copy-id -i $file "-p 22 test01@$host"
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
exit
}
标签:else useradd ips nfa 自动 distrib set argc hosts
原文地址:https://www.cnblogs.com/lfh1127/p/9714393.html