expect使用中报错:“send: spawn id exp8 not open while executing”

我在全球村 2012-10-19 02:32:53
用expect写了一个实现ssh自动交互取消密码登陆服务端的脚本,但是(1)在进行服务端的ssh登陆 操作前出现了如题的报错;初次写脚本,里面(2)还有很多错误或者比较奇怪的地方,希望大侠们帮忙看下,给点建议;(3)另外就是,每条命令执行中的时间如何控制啊?先感谢了
程序:

1 #!/usr/bin/expect -f
2
3 spawn echo "######running to generate a ssh-public-key######"
4 spawn ssh-keygen -t rsa
5 expect " Enter file in which to save the key (/root/.ssh/id_rsa):"
6 send "\r"
7 expect {
8 "Overwrite (y/n)" {send "y\r" ;exp_continue }
9 "password:" { send "loongson\r" }
10 "Enter passphrase (empty for no passphrase):"{send "123456\r"}
11 }
12 expect "Enter same passphrase again:"
13 send "123456\r\n"
14 set timeout 30
15 expect -re "\](\$|#) "
16 spawn scp /root/.ssh/id_rsa.pub @172.16.1.63:/root
19 set timeout 30
20 expect "password:"
21 send "loongson\r"
22 set timeout 30
23 expect -re "\](\$|#)"
24 send "\r"
25 #send "ssh 172.16.1.63"
26 spawn ssh 172.16.1.63
27 expect {
28 "yes/no" {send "yes\r" ;exp_continue }
29 "password:" { send "loongson\r" }
30 }
31 expect -re "\](\$|#) "
32 send "cat /root/id_rsa.pub /root/.ssh/authorized_keys \r"
33 expect -re "\](\$|#) "
34 send "exit\r"
35
36 expect eof
37 exit

执行结果:
Loong:~/ssh_test# ./all.sh
spawn echo ######running to generate a ssh-public-key######
spawn ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again: spawn scp /root/.ssh/id_rsa.pub @172.16.1.63:/root
root@172.16.1.63's password:
id_rsa.pub 100% 392 0.4KB/s 00:00
send: spawn id exp8 not open
while executing
"send "\r""
(file "./all.sh" line 24)
Loong:~/ssh_test#
...全文
2032 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
honty 2012-11-15
  • 打赏
  • 举报
回复
看来做人还得靠自己
我在全球村 2012-11-15
  • 打赏
  • 举报
回复 1
终于把问题解决了,
#expect -re "\](\$|#) "
 27 #send "ls\r"
 28 #send "ssh 172.16.1.63"
是这几行的问题,注释掉之后问题消失了!
Loong:~/ssh_test# ./all.sh 
spawn echo ######running to generate a ssh-public-key######
spawn ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: spawn scp /root/.ssh/id_rsa.pub @172.16.3.63:/root
root@172.16.3.63's password: 
id_rsa.pub                                                          100%  392     0.4KB/s   00:00    
spawn ssh root@172.16.3.63 cat /root/id_rsa.pub >>/root/.ssh/authorized_keys
root@172.16.3.63's password: 
可以执行到下面的远程登录命令了,哈哈! 看来做人还得靠自己
我在全球村 2012-10-23
  • 打赏
  • 举报
回复
顶下!求解释

23,115

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧