嗨 B MR W 是的,我将发布一个期望脚本,因为这出现了很多:
E2A 指令适用于 linux
在 mac 上有期望https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/expect.1.html
这可能意味着弄乱脚本来满足mac的标准期望我对此没有足够的了解,但原则上看过下面的过程应该可以工作
您对 ssh-copy-id 或脚本的哪一点感兴趣?
要使脚本运行,您需要
sudo-apt-get install expect or
sudo yum install expect
一旦安装
这是一个典型的脚本:
#!/usr/bin/expect -f
set force_conservative 1
if {$force_conservative} {
set send_slow {1 .001}
proc send {ignore arg} {
sleep .001
exp_send -s -- $arg
}
}
;# Validate user input - make sure all fields required are given
if {$argc >= 1} {
;# Setting password
set user "MYUSER";
set supass "MYPASSWORD"
set host [lindex $argv 0]
# set command1 [lindex $argv 2]
set prompt "(:|#|%|>|\\\$|% |# |> |\\\$ )"
set prompt1 "(>)"
set timeout -1
;###############################################################
;#connect to specified host given by addstaff or globalstaff.
spawn ssh $user@$host
expect {
"*word:*" {}
"*(yes/no)?*" {
send - "yes\r"
expect "*word:" { }
}
}
send - "$supass\r"
expect eof exit -re $prompt
send - "sudo bash\r"
expect {
"*word:*" {
send "$supass\r"
expect eof exit -re $prompt
}
"*" {
expect eof exit -re $prompt
}
}
send - "whoami\r"
expect eof exit -re $prompt
interact
;#send - "$command\r"
;# expect eof exit -re $prompt
;#Logout of current ssh session
;#send - "exit\r"
;#expect eof exit -re $prompt
;#send - "exit\r"
;#expect eof exit -re $prompt
} else {
send - "echo Sorry require user host command \n\n\r";
exit
}
如果您注意到我已经注释掉了未发送的命令 - 它使用交互模式允许您在没有密码的情况下实际登录,在脚本顶部定义的用户密码......一旦登录,您就可以输入会按照正常的 ssh
这是我在本地运行它:
whoami
myuser
./ssh-connection.exp localhost
spawn ssh myuser@localhost
myuser@localhost's password:
Welcome to Ubuntu 12.04.2 LTS (GNU/Linux XXXX)
* Documentation: https://help.ubuntu.com/
Last login: Sat Sep 7 20:19:53 2013 from localhost
sudo bash
This is BASH 4.2- DISPLAY on localhost:0.0
Sat Sep 7 20:25:09 BST 2013
whoami
[20:25 myuser@myuser-DP ~] > sudo bash
whoami
This is BASH 4.2- DISPLAY on localhost:0.0
Sat Sep 7 20:25:09 BST 2013
[20:25 myuser@myuser-DP ~] > whoami
root
[20:25 myuser@myuser-DP ~] > whoami
root
在脚本中它还执行 sudo bash 这就是为什么它将我重新连接到 localhost 并且我成为 root