码迷,mamicode.com
首页 > 其他好文 > 详细

自动生成公钥并自动写入到特定服务器脚本

时间:2017-05-04 23:08:47      阅读:492      评论:0      收藏:0      [点我收藏+]

标签:expect


自动生成公钥并自动写入到特定服务器脚本

普遍账号需开的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}


本文出自 “linux” 博客,请务必保留此出处http://yangzhiming.blog.51cto.com/4849999/1922105

自动生成公钥并自动写入到特定服务器脚本

标签:expect

原文地址:http://yangzhiming.blog.51cto.com/4849999/1922105

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!