Hi,
I'm an Expect new user, and I'm looking
for any way to use expect to searching for a specified
string and log this string in a file.
I was using log_file option, but this generates a lot of data (I'm
using
Expect to access a menu made with Cache in AIX).
Thanks.
|
|
0
|
|
|
|
Reply
|
contracer11 (156)
|
1/27/2011 3:06:42 AM |
|
contracer wrote:
> Hi,
> I'm an Expect new user, and I'm looking
> for any way to use expect to searching for a specified
> string and log this string in a file.
> I was using log_file option, but this generates a lot of data (I'm
> using
> Expect to access a menu made with Cache in AIX).
> Thanks.
a plain pattern or something with glob or regexp syntax ?
set pattern "xdsflkas lsdflk klds"
expect "$pattern" {
# the array expect_out has all the relevant info of a match
# parray just to show whats there:
parray expect_out
# has the outermost match of your pattern
puts stderr "match!:$expect_out(0,string)"
} eof {
puts stderr "EOF in expect"
} timeout {
puts stderr Timeout,
}
uwe
|
|
0
|
|
|
|
Reply
|
Uwe
|
1/27/2011 9:33:00 AM
|
|
On 27 jan, 07:33, Uwe Klein <uwe_klein_habertw...@t-online.de> wrote:
> contracer wrote:
> > Hi,
> > I'm an Expect new user, and I'm looking
> > for any way to use expect to searching for a specified
> > string and log this string in a file.
> > I was using log_file option, but this generates a lot of data (I'm
> > using
> > Expect to access a menu made with Cache in AIX).
> > Thanks.
>
> a plain pattern or something with glob or regexp syntax ?
>
> set pattern "xdsflkas lsdflk klds"
>
> expect =A0"$pattern" {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # the array expect_out has all the releva=
nt info of a match
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # parray just to show whats there:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 parray expect_out
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # has the outermost match of your pattern
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts stderr "match!:$expect_out(0,string)=
"
> =A0 =A0 =A0 =A0 } eof {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts stderr "EOF in expect"
> =A0 =A0 =A0 =A0 } timeout {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts stderr Timeout,
> =A0 =A0 =A0 =A0 }
>
> uwe
This is my Expect script that I use to connect in a Menu:
#!/usr/local/bin/expect --
spawn ssh vdr@127.0.0.1
expect "*?assword:*"
send "Radhos123\r"
sleep 1
send "\r"
send "BLM"
expect "Shortage:"
send "?\r"
Set pattern is an expect command ?
|
|
0
|
|
|
|
Reply
|
contracer
|
1/31/2011 12:42:42 PM
|
|
contracer wrote:
> This is my Expect script that I use to connect in a Menu:
>
> #!/usr/local/bin/expect --
> spawn ssh vdr@127.0.0.1
# be exact in the expected tail of the passwd request.
# you won't need the sleep then and using sleep still
# carries a change of hickup.
> expect "assword: " {
send "Radhos123\r"
} timeout {
} eof {
# nix this:
> sleep 1
' ??
> send "\r"
> send "BLM"
> expect "Shortage:"
> send "?\r"
>
>
> Set pattern is an expect command ?
set <varname> <value>
is a tcl command.
expect is an extension package to tcl.
#!/usr/local/bin/expect
is equivalent to
#!/usr/bin/tclsh
package require Expect
ending expect patterns with variable length expected output like * .*
does not neccesarily do what you expect. What has been matches
is more of a timeing issue then as anything else. results may be random.
[sleep] may but does not guarantee tiding over these issues.
same with anchoring ^ $ your expression. ^ and $ denote the current
buffer start and end and not the beginning and end of a line.
uwe
|
|
0
|
|
|
|
Reply
|
Uwe
|
1/31/2011 1:33:11 PM
|
|
On 31 jan, 10:42, contracer <contrace...@gmail.com> wrote:
> On 27 jan, 07:33, Uwe Klein <uwe_klein_habertw...@t-online.de> wrote:
>
>
>
>
>
> > contracer wrote:
> > > Hi,
> > > I'm an Expect new user, and I'm looking
> > > for any way to use expect to searching for a specified
> > > string and log this string in a file.
> > > I was using log_file option, but this generates a lot of data (I'm
> > > using
> > > Expect to access a menu made with Cache in AIX).
> > > Thanks.
>
> > a plain pattern or something with glob or regexp syntax ?
>
> > set pattern "xdsflkas lsdflk klds"
>
> > expect =A0"$pattern" {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # the array expect_out has all the rele=
vant info of a match
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # parray just to show whats there:
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 parray expect_out
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # has the outermost match of your patte=
rn
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts stderr "match!:$expect_out(0,strin=
g)"
> > =A0 =A0 =A0 =A0 } eof {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts stderr "EOF in expect"
> > =A0 =A0 =A0 =A0 } timeout {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts stderr Timeout,
> > =A0 =A0 =A0 =A0 }
>
> > uwe
>
> This is my Expect script that I use to connect in a Menu:
>
> #!/usr/local/bin/expect --
> spawn ssh v...@127.0.0.1
> expect "*?assword:*"
> send "Radhos123\r"
> sleep 1
> send "\r"
> send "BLM"
> expect "Shortage:"
> send "?\r"
>
> Set pattern is an expect command ?- Ocultar texto das mensagens anteriore=
s -
>
> - Mostrar texto das mensagens anteriores -
I need find "Inquirer - mm/dd/yy" in this menu and put this string in
a file.
|
|
0
|
|
|
|
Reply
|
contracer
|
1/31/2011 1:42:17 PM
|
|
contracer wrote:
> I need find "Inquirer - mm/dd/yy" in this menu and put this string in
> a file.
expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
# debug, just to show:
parray expect_out
# drop line at end of logfile:
set fd [open ./logfile.log w+]
puts $fd $expect_out(1,string)
close $fd
}
see:
uwe@ben:~> tclsh
% package require expect
% package require Expect
5.40.0
% expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
# debug, just to show:
parray expect_out
# drop line at end of logfile:
set fd [open ./logfile.log w+]
puts $fd $expect_out(1,string)
close $fd
} Inquirer - 11/22/33
expect_out(0,string) = Inquirer - 11/22/33
expect_out(1,string) = 11/22/33
expect_out(buffer) =
Inquirer - 11/22/33
expect_out(spawn_id) = exp0
% cat logfile.log
11/22/33
uwe
|
|
0
|
|
|
|
Reply
|
Uwe
|
1/31/2011 1:53:34 PM
|
|
On 31 jan, 11:53, Uwe Klein <uwe_klein_habertw...@t-online.de> wrote:
> contracer wrote:
> > I need find "Inquirer - mm/dd/yy" in this menu and put this string in
> > a file.
>
> expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # debug, just to show:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 parray expect_out
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # drop line at end of logfile:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 set fd [open ./logfile.log w+]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts $fd $expect_out(1,string)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 close $fd
>
> }
>
> see:
> uwe@ben:~> tclsh
> % package require expect
> % package require Expect
> 5.40.0
> % expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # debug, just to show:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0parray expect_out
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # drop line at end of logfile:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0set fd [open ./logfile.log w+]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0puts $fd $expect_out(1,string)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0close $fd} Inquirer - 11/22/33
>
> expect_out(0,string) =3D Inquirer - 11/22/33
> expect_out(1,string) =3D 11/22/33
> expect_out(buffer) =A0 =3D
> Inquirer - 11/22/33
> expect_out(spawn_id) =3D exp0
> % cat logfile.log
> 11/22/33
>
> uwe
Thank you, Uwe.
I=B4ll try apply these commands in my script.
|
|
0
|
|
|
|
Reply
|
contracer
|
1/31/2011 3:03:21 PM
|
|
On Jan 31, 1:03=C2=A0pm, contracer <contrace...@gmail.com> wrote:
> On 31 jan, 11:53, Uwe Klein <uwe_klein_habertw...@t-online.de> wrote:
>
>
>
>
>
> > contracer wrote:
> > > I need find "Inquirer - mm/dd/yy" in this menu and put this string in
> > > a file.
>
> > expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # debug, just t=
o show:
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 parray expect_o=
ut
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # drop line at =
end of logfile:
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 set fd [open ./=
logfile.log w+]
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 puts $fd $expec=
t_out(1,string)
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 close $fd
>
> > }
>
> > see:
> > uwe@ben:~> tclsh
> > % package require expect
> > % package require Expect
> > 5.40.0
> > % expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # debug, just t=
o show:
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0parray ex=
pect_out
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # drop line at =
end of logfile:
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0set fd [o=
pen ./logfile.log w+]
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0puts $fd =
$expect_out(1,string)
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0close $fd=
} Inquirer - 11/22/33
>
> > expect_out(0,string) =3D Inquirer - 11/22/33
> > expect_out(1,string) =3D 11/22/33
> > expect_out(buffer) =C2=A0 =3D
> > Inquirer - 11/22/33
> > expect_out(spawn_id) =3D exp0
> > % cat logfile.log
> > 11/22/33
>
> > uwe
>
> Thank you, Uwe.
> I=C2=B4ll try apply these commands in my script.- Hide quoted text -
>
> - Show quoted text -
I got this message when I put that data in my script:
expect_out(spawn_id) =3D exp5 =3D &n=3Dsp;
=3D =E2=94=82
can't read "expect_out(1,string)": no such element in array
=3D
|
|
0
|
|
|
|
Reply
|
contracer11 (156)
|
2/3/2011 1:06:14 PM
|
|
contracer wrote:
> On Jan 31, 1:03 pm, contracer <contrace...@gmail.com> wrote:
>
>>On 31 jan, 11:53, Uwe Klein <uwe_klein_habertw...@t-online.de> wrote:
>>
>>
>>
>>
>>
>>
>>>contracer wrote:
>>>
>>>>I need find "Inquirer - mm/dd/yy" in this menu and put this string in
>>>>a file.
>>
>>>expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
>>> # debug, just to show:
>>> parray expect_out
>>> # drop line at end of logfile:
>>> set fd [open ./logfile.log w+]
>>> puts $fd $expect_out(1,string)
>>> close $fd
>>
>>>}
>>
>>>see:
>>>uwe@ben:~> tclsh
>>>% package require expect
>>>% package require Expect
>>>5.40.0
>>>% expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
>>> # debug, just to show:
>>> parray expect_out
>>> # drop line at end of logfile:
>>> set fd [open ./logfile.log w+]
>>> puts $fd $expect_out(1,string)
>>> close $fd} Inquirer - 11/22/33
>>
>>>expect_out(0,string) = Inquirer - 11/22/33
>>>expect_out(1,string) = 11/22/33
>>>expect_out(buffer) =
>>>Inquirer - 11/22/33
>>>expect_out(spawn_id) = exp0
>>>% cat logfile.log
>>>11/22/33
>>
>>>uwe
>>
>>Thank you, Uwe.
>>I´ll try apply these commands in my script.- Hide quoted text -
>>
>>- Show quoted text -
>
>
> I got this message when I put that data in my script:
>
> expect_out(spawn_id) = exp5 = &n=sp;
> = │
looks like something is missing or overwritten via a plain \r
in the stream. Maybe log into file and look with texteditor ?
>
> can't read "expect_out(1,string)": no such element in array
>
> =
if $expect_out(1,string) gives an error nothing has been matched to
the subexpression, the array element will not exist.
expand the expect statement with handling for timeout and eof.
( did it take about 10 seconds to respond ? )
uwe
|
|
0
|
|
|
|
Reply
|
Uwe
|
2/3/2011 3:18:06 PM
|
|
On Feb 3, 9:18=C2=A0am, Uwe Klein <uwe_klein_habertw...@t-online.de> wrote:
> contracer wrote:
> > On Jan 31, 1:03 pm, contracer <contrace...@gmail.com> wrote:
>
> >>On 31 jan, 11:53, Uwe Klein <uwe_klein_habertw...@t-online.de> wrote:
>
> >>>contracer wrote:
>
> >>>>I need find "Inquirer - mm/dd/yy" in this menu and put this string in
> >>>>a file.
>
> >>>expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# debug, just =
to show:
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0parray expect_=
out
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# drop line at=
end of logfile:
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0set fd [open .=
/logfile.log w+]
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0puts $fd $expe=
ct_out(1,string)
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0close $fd
>
> >>>}
>
> >>>see:
> >>>uwe@ben:~> tclsh
> >>>% package require expect
> >>>% package require Expect
> >>>5.40.0
> >>>% expect -re {Inquirer - (\d{2}/\d{2}/\d{2})} {
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# debug, just =
to show:
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 parray expect=
_out
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0# drop line at=
end of logfile:
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 set fd [open =
../logfile.log w+]
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 puts $fd $exp=
ect_out(1,string)
> >>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 close $fd} In=
quirer - 11/22/33
>
> >>>expect_out(0,string) =3D Inquirer - 11/22/33
> >>>expect_out(1,string) =3D 11/22/33
> >>>expect_out(buffer) =C2=A0 =3D
> >>>Inquirer - 11/22/33
> >>>expect_out(spawn_id) =3D exp0
> >>>% cat logfile.log
> >>>11/22/33
>
> >>>uwe
>
> >>Thank you, Uwe.
> >>I=C2=B4ll try apply these commands in my script.- Hide quoted text -
>
> >>- Show quoted text -
>
> > I got this message when I put that data in my script:
>
> > expect_out(spawn_id) =3D exp5 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =3D =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 &n=3Dsp;
> > =3D =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=E2=94=82
>
> looks like something is missing or overwritten via a plain \r
> in the stream. Maybe log into file and look with texteditor ?
>
>
>
> > can't read "expect_out(1,string)": no such element in array
>
> > =3D
>
> if $expect_out(1,string) gives an error nothing has been matched to
> the subexpression, the array element will not exist.
>
> expand the expect statement with handling for timeout and eof.
> ( did it take about 10 seconds to respond ? )
>
> uwe
<URL: http://wiki.tcl.tk/3173 > has more tips on debugging. These
symptoms ALWAYS arise when someone isn't matching what he thinks he
is.
Also, as Uwe already mentioned, don't [sleep]; to do so almost
certainly results from (and causes more!) confusion.
|
|
0
|
|
|
|
Reply
|
claird271 (51)
|
2/3/2011 8:10:03 PM
|
|
Cameron Laird wrote:
> Also, as Uwe already mentioned, don't [sleep]; to do so almost
> certainly results from (and causes more!) confusion.
Been thinking about it.
If [expect] gets into the $pattern arm expect_out() should have
the "right" things.
Which would indicate that contracer made an error in
applying the example to his problem.
Either the RE pattern is munged or the "match" script is
run out of the [expect] run.
uwe
|
|
0
|
|
|
|
Reply
|
uwe_klein_habertwedt (1488)
|
2/3/2011 8:36:28 PM
|
|
|
10 Replies
129 Views
(page loaded in 0.183 seconds)
Similiar Articles: search for a string in a file using expect.. - comp.lang.tcl ...Hi, I want to grep for a particular string in a file and get the value next to it. My data: user1: 11111111 user2: 22222222 code ===== se... find string matching whole word - comp.soft-sys.matlabsearch for a string in a file using expect.. - comp.lang.tcl ... find string matching whole word - comp.soft-sys.matlab search for a string in a file using expect.. - comp ... how to match "$" in expect, instead of using it as a wildcard ...search for a string in a file using expect.. - comp.lang.tcl ... how to match "$" in expect, instead of using it as a wildcard ... search for a string in a file using ... tcl and powershell - comp.lang.tclsearch for a string in a file using expect.. - comp.lang.tcl ... awk vs expect/tcl - comp.lang.awk search for a string in a file using expect.. - comp.lang.tcl ... Using Expect to spawn a perl process which spawns another perl ...search for a string in a file using expect.. - comp.lang.tcl ... Using Expect to spawn a perl process which spawns another perl ..... open FILE, ">file.txt" or die ... Search in *.rex files - comp.lang.rexx... for the character string '%2F' then Windows XP search on my machine finds that just fine. If you're looking for the ASCII character '/' = Hex'2F' then you should expect ... How to search for a string in a bunch of JAR files? - comp.unix ...search for a string in a file using expect.. - comp.lang.tcl ... How to search for a string in a bunch of JAR files? - comp.unix ... Hi, I want to search for an instance ... find string in file - comp.unix.shellsearch for a string in a file using expect.. - comp.lang.tcl ... Hi, I want to grep for a particular string in a file and get the value next to it. How to get a String? - comp.soft-sys.matlabsearch for a string in a file using expect.. - comp.lang.tcl ... Hi, I want to grep for a particular string in a file and get the value next to it. ld: Mismatched ABI (not an ELF file) - comp.sys.hp.hpuxsearch for a string in a file using expect.. - comp.lang.tcl ... Hi, I want to grep for a particular string in a file and get the value next to it. Exploring Expect: Chapter 3 Getting Started With Expectsend "but I only expected <$expect_out(0,string)>" The angle brackets do not do ... Finding unexpected data in the input does not bother expect. It keeps looking until it ... search for a string in a file using expect.. - comp.lang.tcl ...Hi, I want to grep for a particular string in a file and get the value next to it. My data: user1: 11111111 user2: 22222222 code ===== se... 7/23/2012 12:45:31 PM
|