Automation 无法正确分析expect脚本中命令的输出
我已经编写了一个expect脚本,用于通过ssh登录到远程机器,并执行命令以获取系统信息详细信息。我可以登录到远程机器,运行命令并获得输出。我在解析输出时遇到一些问题。我得到了一些不需要的文本和作为输出的一部分执行的命令,需要您的帮助来消除这些文本并将相关文本作为输出 expect脚本发布在下面Automation 无法正确分析expect脚本中命令的输出,automation,expect,Automation,Expect,我已经编写了一个expect脚本,用于通过ssh登录到远程机器,并执行命令以获取系统信息详细信息。我可以登录到远程机器,运行命令并获得输出。我在解析输出时遇到一些问题。我得到了一些不需要的文本和作为输出的一部分执行的命令,需要您的帮助来消除这些文本并将相关文本作为输出 expect脚本发布在下面 #!/usr/bin/expect set timeout 10 log_user 0 set promptEn "(>|#|%|\\$)" set ip [lindex $argv 0]
#!/usr/bin/expect
set timeout 10
log_user 0
set promptEn "(>|#|%|\\$)"
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set query [lindex $argv 3]
if {[llength $argv] == 0} {
send_user "Usage: scriptname Ip-address Username password query\n"
exit 1
}
spawn ssh -q -o StrictHostKeyChecking=no "$user\@$ip"
expect {
timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
eof { send_user "\nSSH failure "; exit 1 }
"*assword"
}
send "$password\r"
expect {
timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1}
#-re "$promptEn"
-re $promptEn
}
send_user "\nPassword is correct\n"
#command to be executed
send "sudo dmidecode -t system |grep -w \"$query:\"\r"
expect {
timeout2 { send_user "\nFailed to get password prompt on remote machine \n"; exit 1 }
eof { send_user "\nSSH failure "; exit 1 }
"*assword"
}
send "$password\r"
expect {
timeout
{
send_user "\n Command Failed \n"; exit 1
}
-re "$promptEn"
}
expect -re "$query:"
puts "$expect_out(buffer)"
send "exit\r"
我使用命令行参数运行脚本,如下所示
预期sshtest.exp 192.168.4.42测试帐户Password@123制造商
我得到的解析输出是
Password is correct
sudo dmidecode -t system |grep -w "Manufacturer:"
Manufacturer: LENOVO
testaccount@santhosh-Lenovo-G560:~#
我只希望最后一行制造商:LENOVO作为输出,而不是另一行
我可以如下调用脚本
预期sshtest.exp 192.168.4.42测试帐户Password@123制造商| tail-2 | head-1
获取线路,但我不想这样做,因为如果系统无法连接,我将丢失错误消息 在“密码正确”之后添加:log\u用户0
然后,改变
puts "$expect_out(buffer)"
到
set lines [split $expect_out(buffer) \n]
foreach line [lsearch -inline -glob $lines {*Manufacturer:*}] {
puts $line
}