将Expected_output(缓冲区)保存到文件中 - Bash

时间:2016-07-21 10:43:55

标签: bash shell expect

我有这个bash脚本:

#!/bin/bash

read -p "Enter IP Address: " ip
read -p "login: " user
read -p "password: " password

/usr/bin/expect<<EOF
spawn telnet ${ip}
expect "login"
send "${user}\n"
expect -re "password"
send "${password}\n"
expect ">"
send "show system\r"
expect ">"
send "show interfaces 6/2 counters\r"
expect ">"
send "exit\r"
expect ">"
sleep 2
exit
EOF

我正在尝试将命令“show system”和“show interfaces 6/2 counters”返回的值保存在我的主目录中名为Logs.txt的文件中。

在bash中我使用

echo "Date creation $(date)" >> ~/ls.txt
echo >> ~/ls.txt
echo "show system output" >> ~/ls.txt
echo Hello >> ~/ls.txt
echo >> ~/ls.txt
echo "output" >> ~/ls.txt
echo Hello2 >> ~/ls.txt

如何在expect脚本中执行相同的操作?

提前致谢。

修改 我需要附加两个输出而不是覆盖。

1 个答案:

答案 0 :(得分:1)

select
    count(case when p_akt = 'A' then id_contract end) as total_count,
    count(
        case when p_akt = 'A' and d_start_dw > '2013-01-01' and d_start_dw <= '2013-01-31'
            then id_contract
        end
    ) as januar_count,
    count(
        case when p_akt = 'A' and d_start_dw > '2013-02-01' and d_start_dw <= '2013-02-28'
            then id_contract
        end
    ) as februar_count
from
    [DW].[dbo].[DW_D_CONT]

您可以将其写为文件,

set prompt "\r\n(.*?)>"
spawn telnet ${ip}
expect "login"
send "${user}\r"
expect -re "password"
send "${password}\r"
expect ">"
send "show system\r"
expect -re $prompt
# Extracting the 1st substring
lappend result expect_out(1,string);
send "show interfaces 6/2 counters\r"
expect -re $prompt
# Extracting the 1st substring
lappend result expect_out(1,string)