普遍账号需开的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 [email protected]$ip:/tmp/
expect {
"*(yes/no)*" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
expect eof
spawn ssh [email protected]$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}