自动生成公钥并自动写入到特定服务器脚本
普遍账号需开的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}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。