我只是在改进我的问题,因为到目前为止我已经取得了成就:
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
if {[llength $argv] == 0} {
send_user "Usage: scriptname username \'password\' hostname\n"
exit 1
}
send_user "\n#####\n# $hostname\n#####\n"
spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname
expect {
timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
eof { send_user "\nSSH failure for $hostname\n"; exit 1 }
"Password: "
}
send "$password\r"
expect {
timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1}
"{severname:mike} "
}
send "ls -lrt\n"
expect {
"{severname:mike} " {
send "uname\n"
}
}
expect {
"{severname:mike} " }
send "exit\r"
close
我希望我是正确的,但如何获取本地记录的命令的输出 errlogs 和成功日志,其中命令是 ls、ls -lrt 等命令的列表
此外,当我这样说时:
spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname {ls -lrt;df -h}
它登录并执行,ls -lrt;df -h
但之后使用调试选项抛出错误。连接关闭可能是因为命令在 ssh 范围内执行。
[root@testgfs2 final]# ./workingscript.exp mike bar01 10.38.164.103
#####
# 10.38.164.103
#####
spawn ssh -q -o StrictHostKeyChecking=no [email protected] ls -lrt
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {19901}
expect: does "" (spawn_id exp6) match glob pattern "Password: "? no
Password:
expect: does "Password: " (spawn_id exp6) match glob pattern "Password: "? yes
expect: set expect_out(0,string) "Password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Password: "
send: sending "bar01\r" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "{severname:mike} "? no
expect: does "\r\n" (spawn_id exp6) match glob pattern "{severname:mike} "? no
total 6
-rw-r--r-- 1 mike other 136 Feb 26 08:39 local.cshrc
-rw-r--r-- 1 mike other 157 Feb 26 08:39 local.login
-rw-r--r-- 1 mike other 174 Feb 26 08:39 local.profile
expect: does "\r\ntotal 6\r\n-rw-r--r-- 1 mike other 136 Feb 26 08:39 local.cshrc\r\n-rw-r--r-- 1 mike other 157 Feb 26 08:39 local.login\r\n-rw-r--r-- 1 mike other 174 Feb 26 08:39 local.profile\r\n" (spawn_id exp6) match glob pattern "{severname:mike} "? no
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\ntotal 6\r\n-rw-r--r-- 1 mike other 136 Feb 26 08:39 local.cshrc\r\n-rw-r--r-- 1 mike other 157 Feb 26 08:39 local.login\r\n-rw-r--r-- 1 mike other 174 Feb 26 08:39 local.profile\r\n"
write() failed to write anything - will sleep(1) and retry...
send: sending "ls -lrt\n" to { exp6 send: spawn id exp6 not open
while executing
"send "ls -lrt\n""
(file "./workingscript.exp" line 30)
命令变量包含由分号分隔的命令列表,如下所示:
command 1; command 2;command 3(=$command)
我不知道如何在 Expect 脚本中一一执行它,因此我可以执行以下操作,而不是将命令放在 ssh 范围内:
spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname
IFS=';'
for i in $command
do
expect {
"{severname:mike} " {
send "$i\n"
} 2>> errorlog 1>> succeslog
}
done
我只是使用了 unix 语法和 Expect 语法相结合,只是为了让你明白我想要做什么。
答案1
Expect 的交互模型不包括 stdout 和 stderr 流的分离。当您生成一个进程时,您会创建一个新的伪 TTY,其行为与用户在终端中看到的行为几乎相同,并且用户通常无法区分这两者。尝试一下:
$ echo "This is stdout"
This is stdout
$ echo "This is stderr" 1>&2
This is stderr
不做某物为了标记差异,Expect 无法区分它们(就像命令行上的用户一样)。因此,对于您需要做的事情来说,Expect 可能是错误的工具。
现在,在不确切知道为什么需要这个的情况下,很难为您想出替代方法。对于您所需要的,看起来您可以使用单个 ssh 命令来完成此操作:
ssh -q -o StrictHostKeyChecking=no $username@$hostname "ls -lrt; df -h" 1>>successlog 2>>errorlog
对于另一种内置支持生成子进程并分别获取其 stdout 和 stderr 的脚本语言,请尝试 Python 及其 subprocess 模块。
import shlex
from subprocess import Popen, PIPE
cmd = Popen(shlex.split('ssh -q -o StrictHostKeyChecking=no $username@$hostname "ls -lrt; df -h"'), stdout=PIPE, stderr=PIPE)
cmd.wait() # Wait for the command to complete
success_output = cmd.stdout
error_output = cmd.stderr
但如果你真的真的想要在 Expect 中执行此操作,我认为最好的方法可能是检查最后一个进程的退出值,并在该值非零时写入错误日志文件。这是一个类似的例子:
spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname
# Receive password prompt and send the password here...
expect {
-ex $prompt { send "ls -lrt\r" }
timeout {
send_user "Everything is terrible forever.\n"
exit 1
}
}
expect {
-ex $prompt {
set output $expect_out(buffer)
send {echo RETVAL::$?::}
send "\r"
}
timeout {
send_user "Everything is terrible forever.\n"
exit 1
}
}
expect {
-ex {RETVAL::0::} {
set fp [open successlog a]
puts $fp $output
close $fp
}
-re {RETVAL::[1-9][0-9]*::} {
set fp [open errorlog a]
puts $fp $output
close $fp
}
timeout {
send_user "Everything is terrible forever.\n"
exit 1
}
}