我想要记录我在一个expect SSH衍生脚本中运行的几个命令的输出,但我希望输出非常干净。我已经在互联网上找到了一些关于日志的信息,但除了将命令发送到日志文件之外,它不起作用。
-从脚本中间开始,下面是我的步骤
expect -re ">"
log_file "settings.txt";
send_user "show device\n"
expect -re ">"
log_file "rules.txt";
send "show access-rules\n"
expect -re ">"
log_file "interfaces.txt";
send "show interface all\n"
expect -re ">"
log_file "cfs.txt";
send "show tsr content-filtering\n"
expect -re ">"
log_file "routes.txt";
send "show routes\n"
expect -re ">"
log_file "nat.txt";
send "show nat\n"
expect -re ">"
send "exit\n"与send_log命令一样,send_user命令仅将命令发送到日志文件。我只希望将该命令的输出发送到日志文件中,日志中也允许该命令本身。
发布于 2015-04-18 00:51:50
你真正需要的是抑制send命令的输出,这基本上是在here中解释的。(我想这就是你的要求。仅写入命令的输出,而不是命令。)
因为在登录到文件时log_user和log_file绑定得很紧密,所以我们必须用send_log对其进行调整。
一个基本的想法可以概括为
# We escaped the `$` symbol with backslash to match literal '$'
# The second dollar sign is meant to match the end of a line
set prompt "#|>|\\\$$"
log_file output.log
send "show command here\r"
log_user 0
expect -re "\r\n(.*)$prompt"; # match actual command output
log_user 1
send_log "$expect_out(1,string)"; # Sending it to the log file现在,根据您的要求,您希望为每个命令将其发送到单独的文件中。您可以在单独的列表中为这些命令输出定义命令和文件名。
spawn ssh <host> <port>
# Further your logic to handle the login
proc getCmdOutput {cmd} {
global cmdsList logFileList
set prompt "#|>|\\\$$";
# Finding the log file name based on the 'cmd' passed
log_file -noappend [lindex $logFileList [lsearch $cmdsList $cmd]]
send "$cmd\r"
log_user 0
expect -re "\r\n(.*)$prompt"
log_user 1
send_log "$expect_out(1,string)"; # Sending command output to file
log_file; # Removing logging of file.
}
set cmdsList {
"show version"
"show interfaces"
"show services"
"show vlan"
}
set logFileList {
version.log
interfaces.log
services.log
vlan.log
}
foreach cmd $cmdsList {
getCmdOutput $cmd
}请注意,列表cmdsList和logFileList在命令和对应的日志文件名方面应该是同源的。
https://stackoverflow.com/questions/29693977
复制相似问题