首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅将命令输出记录到文件中

仅将命令输出记录到文件中
EN

Stack Overflow用户
提问于 2015-04-17 16:23:34
回答 1查看 1.6K关注 0票数 0

我想要记录我在一个expect SSH衍生脚本中运行的几个命令的输出,但我希望输出非常干净。我已经在互联网上找到了一些关于日志的信息,但除了将命令发送到日志文件之外,它不起作用。

-从脚本中间开始,下面是我的步骤

代码语言:javascript
复制
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命令仅将命令发送到日志文件。我只希望将该命令的输出发送到日志文件中,日志中也允许该命令本身。

EN

回答 1

Stack Overflow用户

发布于 2015-04-18 00:51:50

你真正需要的是抑制send命令的输出,这基本上是在here中解释的。(我想这就是你的要求。仅写入命令的输出,而不是命令。)

因为在登录到文件时log_userlog_file绑定得很紧密,所以我们必须用send_log对其进行调整。

一个基本的想法可以概括为

代码语言:javascript
复制
# 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

现在,根据您的要求,您希望为每个命令将其发送到单独的文件中。您可以在单独的列表中为这些命令输出定义命令和文件名。

代码语言:javascript
复制
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
}

请注意,列表cmdsListlogFileList在命令和对应的日志文件名方面应该是同源的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29693977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档