自动生成公钥并自动写入到特定服务器脚本
创新互联公司专业为企业提供广宗网站建设、广宗做网站、广宗网站设计、广宗网站制作等企业网站建设、网页设计与制作、广宗企业网站模板建站服务,十余年广宗做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
普遍账号需开的sudo权限
zhi.yang ALL=(ALL:ALL)NOPASSWD:/usr/bin/tee -a /root/.ssh/authorized_keys,/bin/mkdir /log/*
# expect/usr/local/bin/sshkey.exp 10.0.0.3 shaa01 zhi.yang xxxxx
#!/usr/bin/expect
set ip [lindex $argv 0]
set hostname [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]
spawn ssh-keygen
expect {
"*id_rsa):" {send "\r";exp_continue}
"*passphrase):" {send "\r";exp_continue}
"*again:" {send "\r"}
}
expect eof
spawn scp -r /root/.ssh/id_rsa.pub $user@$ip:/tmp/
expect {
"*(yes/no)*" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
expect eof
spawn ssh $user@$ip
expect {
"*Password:" {send "$password\r"}
}
expect eof
expect "$*"
send "sudo mkdir /log/$hostname\r"
send "cat /tmp/id_rsa.pub | sudo tee -a /root/.ssh/authorized_keys\r"
send "rm -rf /tmp/id_rsa.pub\r"
send "exit\r"
expect eof
exec sh -c {/bin/sh /usr/local/bin/diskG9.sh}