Anand S Nair
Anand S Nair

Reputation: 27

Suppress expect output

I have the following script which logins to a router and execute command. how can i accomplist only the output and not show anything else.

#!/usr/bin/expect -f
set timeout 3000
log_user 0
spawn ssh -o StrictHostKeyChecking=no [email protected]
expect {
"*assword:" {
send "anismi@123\n"
expect "*#"
log_user 1
send "show bgp vrf IKA summary | include 10.155.192.50\n"
expect "*#"
}
}

This is the output of the above script.

[root@cap-nwmon-01 anand]# ./migrate.sh
show bgp vrf IKA summary | include 10.155.192.50
Sun Feb 25 07:17:25.150 EET
10.155.192.50     0 65256 3107179 4237248  1624156    0    0    5d17h        322

This is the only output i want from this script.

Sun Feb 25 07:17:25.150 EET
10.155.192.50     0 65256 3107179 4237248  1624156    0    0    5d17h        322

Upvotes: 1

Views: 2915

Answers (3)

user2510799
user2510799

Reputation: 51

try this

        if {[string match -nocase *password* $prompt]} {
            log_user 0
        } else {
            log_user 1
        }
        send -- "$answer\r"
        if {[string match -nocase *password* $prompt]} {
            expect "\n"
            log_user 1
            puts "\n"
        } 

Upvotes: 0

meuh
meuh

Reputation: 12255

You see the extra line because you gave the command log_user 1 before send "show...", which causes the echo of "show..." to be logged to the stdout. If you move this command one line later, after the send, and also wait for the echo to complete by adding an expect "\n", then you will see less:

expect "#"
send "show bgp vrf IKA summary | include 10.155.192.50\n"
expect "\n"
log_user 1
expect "#"

Note, the * glob character at the start of a pattern serves no purpose.

Alternatively, instead of changing log_user you should look at using variable $expect_out(buffer) which holds what matched up to the pattern, inclusive.

Upvotes: 2

MarAvFe
MarAvFe

Reputation: 498

Maybe try to redirect the unwanted output like

send "show bgp vrf IKA summary | include 10.155.192.50\n" &> /dev/null

Redirecting it's stdout and stderr to the nothingness of oblivion

Upvotes: 0

Related Questions