Problema de captura de datos usando esperar en tcl

I have one doubt in expect. I want to store the result of command in a string variable. Can anyone tell me what is the mistake in my code? It is not working. I have used expect_out(buffer) pero no funciona.

#!/usr/bin/expect
package require Expect
set Argu1 [lindex $argv 0]
set Argu2 [lindex $argv 1]

if {$argc == 2} {

    if {[regexp {^(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[
        0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]
        \d|1\d\d|2[0-4]\d|[2][5][0-5])$} $Argu1 match ]} {
        puts "VALID IP-ADDRESS"
        set timeout $Argu2-1
        spawn ping $Argu1
        expect "Expect"
        set string $expect_out(buffer)
        puts $string
        puts "SUCCESS"
        exit 
    } else { 
        puts "INVALID IP-ADDRESS"
    } 
} else { 
    puts "YOU HAVE TO GIVE TWO ARGUMENTS\n FIRST ARGUMENT SHOULD BE 
    IP-ADDRESS \n SECOND ARGUMENT SHOULD BE THE NUMBER OF PACKETS
    YOU WANT TO SEND"
}

preguntado el 22 de mayo de 12 a las 07:05

1 Respuestas

I'm reluctant to answer your question, as you haven't responded to my answer on another question. However, seeing as nobody else has responded, I'll try to help you out.

No creo que necesites usar $expect_out(buffer) here. Assuming that a ping returning "0% loss" or "0% packet loss" means the ping was successful, you could try the following (untested). Obviously, you can change the "0%" to whatever you want to signify success.

#!/usr/bin/expect
package require Expect
set Argu1 [lindex $argv 0]
set Argu2 [lindex $argv 1]

if {$argc == 2} {

    if {[regexp {^(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[
        0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]
        \d|1\d\d|2[0-4]\d|[2][5][0-5])$} $Argu1 match ]} {
        puts "VALID IP-ADDRESS"
        set timeout $Argu2-1
        spawn ping $Argu1
        expect {
          "0%" {puts "The IP address was successfully pinged"}
          "default" {puts "Could not ping this IP address"}
        }
        exit 
    } else { 
        puts "INVALID IP-ADDRESS"
    } 
} else { 
    puts "YOU HAVE TO GIVE TWO ARGUMENTS\n FIRST ARGUMENT SHOULD BE 
    IP-ADDRESS \n SECOND ARGUMENT SHOULD BE THE NUMBER OF PACKETS
    YOU WANT TO SEND"
}

The reason I suggest "0%" is as follows. If you do a successful ping from a Windows machine you will see something like this (note the "0% loss"):

C:\Documents and Settings\Brian>ping 192.168.1.1

Pinging 192.168.1.1 with 32 bytes of data:

Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 1ms, Average = 0ms

From a Unix machine, it might look like this (note the "0% packet loss") :

[brian@rex 4]$ ping -c 1 192.168.1.87
PING 192.168.1.87 (192.168.1.87) 56(84) bytes of data.
64 bytes from 192.168.1.87: icmp_seq=0 ttl=64 time=42.1 ms

--- 192.168.1.87 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 42.153/42.153/42.153/0.000 ms, pipe 2

You could pick many indicators of a successful ping, I chose the fact that there was a 0% loss of packets (a clear indicator of success). The expect command searches the result of the ping and if it finds "0%", it outputs "The IP address was successfully pinged".

contestado el 25 de mayo de 12 a las 11:05

Thanks Brian for helping me...but still i have one doubt..actually i didn't understand the concept of 0%... and one more thing.. i didn't understand what you mentioned above..which question are talked about? - Pratibha Tomar

Hi @PratibhaTomar, I will edit the question to explain the concept of 0% - it's just one way to determine a succesful ping. The question I was refering to is this one stackoverflow.com/questions/10633281/… - TrojanName

I've added more info to the answer now - does that explain it better? - TrojanName

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.