我正在尝试编写一个期望脚本,它将ssh到服务器,发送sudo su,然后检查iptables状态并将输出放在服务器上的日志文件中。以下是剧本。
1 #!/usr/bin/expect
2 exp_internal 1
3 log_user 0
4 set timeout 10
5 set password "******"
6
7 spawn /usr/bin/ssh -l subhasish *.*.*.* -p 10022
8
9 expect {
10 -re "password: " {send "$password\r"}
11 -re "$ " {send "sudo su\r"}
12 -re "[sudo] password for subhasish:" {send "$password\r"}
13 -re "# " {send "service iptables status\r"}
14 }
15 set output $expect_out(buffer)
16 send "exit\r"
17 puts "$output\r\n" >> output.log
但是在调试模式下运行时,我会收到这样的错误;
expect -d testcase
expect version 5.44.1.15
argv[0] = expect argv[1] = -d argv[2] = testcase
set argc 0
set argv0 "testcase"
set argv ""
executing commands from command file testcase
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {24105}
invalid command name "sudo"
while executing
"sudo"
invoked from within
"expect {
-re "password: " {send "$password\r"}
-re "$ " {send "sudo su\r"}
-re "[sudo] password for subhasish:" {send "$password\r"}
..."
(file "testcase" line 9)
不确定我哪里出错了。它说无效的命令名称“sudo”,我想这是因为期望不了解这些命令。如何绕过它。请帮忙。感谢。
答案 0 :(得分:12)
问题出在这一行
-re "[sudo] password for subhasish:" {send "$password\r"}
在Tcl中(因此在期望中)方括号是命令替换的语法(如shell中的反引号)。因此,您需要转义括号或使用不同的引号来阻止各种扩展:
-re {[sudo] password for subhasish:} {send "$password\r"}
这会带来一个不同的问题:你期待看到这些确切的字符吗?因为您指示期望将其视为正则表达式,并且正则表达式中的方括号表示字符类,因此它将匹配单个字符,“s”,“u”,“d”或“o” ”。所以你可能需要的是:
-re {\[sudo\] password for subhasish:} {send "$password\r"}
或
-ex {[sudo] password for subhasish:} {send "$password\r"}
答案 1 :(得分:3)
感谢格伦,现在正在努力。它无法工作的另一个原因是需要在正常登录和sudo登录之间睡觉,否则在返回$ prompt之前发送“sudo su”,以供其他人参考,这里是代码,
#!/usr/bin/expect -f
#! /bin/bash
set timeout 60
log_user 1
set host *.*.*.*
set password ******
set user subhasish
set logfile output.txt
spawn ssh -p 10022 $user@$host
expect "*?assword:*"
send -- "$password\r"
#log_user 1
sleep 2
expect "$"
send -- "sudo su\r"
expect -gl {*password for subhasish:*}
send -- "$password\r"
expect "#"
send -- "service iptables status\r"
log_file /home/subhasish/output.log
expect "#"
log_file
send -- "exit\r";
send -- "exit\r";
exit 0