Search code examples
expecttelnet

Expect Script continues execute after ping response


Hello i want To Make A script to test ping after ping success will continues execute comand. Thanks For All Your Helps

below my code

set ip [10.10.10.1,10.10.10.2]
foreach hostname $ip {
    spawn ping -c 2 -i 3 -W 1 $hostname
    expect { "0%" {
            spawn telnet $hostname
            expect "*sername:"
            send "$userper\n"
            expect "*assword:"
            send "$passper\n"
            expect "#"
            send "exit\n"
            expect eof
        }
    }

}    

Solution

  • First, this set ip [10.10.10.1,10.10.10.2] will most likely give you invalid command name "10.10.10.1,10.10.10.2"

    To make a list, use set ip {10.10.10.1 10.10.10.2} -- braces and spaces.

    Next, that ping command does not require any interactivity, so don't spawn it, just exec it:

    set ping_output [exec ping -c 2 -i 3 -W 1 $hostname]
    

    And then check the output for 0% -- note, you don't want to match 100% so add a leading space to the pattern:

    if {[regexp { 0%} $ping_output]} {
        spawn telnet $hostname
        ...
    }
    

    is an extension of so all the Tcl commands are at your disposal