用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#