Operating System - HP-UX
1752790 Members
6221 Online
108789 Solutions
New Discussion юеВ

Expect script beginners help

 
F_8
New Member

Expect script beginners help

Hi,

I am trying to write an expect script that telnets into a server captures the size of a file within the server and then doe s a number of actions based on the size of the file. How do I capture this information. I know I do the following

send -- "telnet 111.111.111.111\r"
do all of the login stuff
....

send -- "ls -l test.raw"

how do I capture the response of this, also how can I use expect to write to a file on my system.

Thanks in advance
10 REPLIES 10
RAC_1
Honored Contributor

Re: Expect script beginners help

You can telnet to a box with password.

(you can use remsh, or secure ssh to login without password)

For sessions like telnet you can use utility expect to do this. Then you put your stuuf for file checking.

Hope this helps.
There is no substitute to HARDWORK
F_8
New Member

Re: Expect script beginners help

Thanks,

I'm actually more concerned with how I can isolate the size of the file and also analyse it using an if statement.

ie if(filesize < 160) then (......)
else(

)

but I'm not sure how I can get the size out of the string ie rwx--r-- directory directory 123 Aug 19 15:13 test.raw

I hope you can help;
F_8
New Member

Re: Expect script beginners help

Thanks,

I'm actually more concerned with how I can isolate the size of the file and also analyse it using an if statement.

ie if(filesize < 160) then (......)
else(

)

but I'm not sure how I can get the size out of the string ie rwx--r-- directory directory 123 Aug 19 15:13 test.raw

I hope you can help;
RAC_1
Honored Contributor

Re: Expect script beginners help

ll test.raw|awk '{print $5}' --> to get the file size.
There is no substitute to HARDWORK
curt larson_1
Honored Contributor

Re: Expect script beginners help

set filesize [ file size yourFileName ]

if $filesize < 160 {
do stuff
}
curt larson_1
Honored Contributor

Re: Expect script beginners help

write to a file:

set file [open /dir/file w]
puts $file "Hello World" ;# writes to /dir/file
close $file
Donny Jekels
Respected Contributor

Re: Expect script beginners help

here is a nice tool.
"Vision, is the art of seeing the invisible"
F_8
New Member

Re: Expect script beginners help

My expect script contains the following:

send -- "telnet 111.111.111.111\r"
expect -exact "login:"

send -- "username\r"
expect -exact "Password: "

send -- "password\r"
expect -exact "\$"

send -- "cd /home/username\r"
expect -exact "\$ "

send -- "ls -l test.raw\r"

ll test.raw|awk '{print $5}'


When I run it I get the following result:

$ can't read "5": no such variable
while executing
"ll test.raw|awk '{print $5}'"
(file "./script.exp" line 35)

What have I done wrong?
curt larson_1
Honored Contributor

Re: Expect script beginners help

the core of expect's language facilites are
provided by Tcl, a language used by many other tools. Your scripts needs to use tcl's syntax.

ll test.raw|awk '{print $5}' isn't in tcl's syntax. it is a unix shell command. they are not the same.

this is probably more what your wanting to do

expect -re "^.*$"
sent "ll test.raw|awk '{print $5}'"
set size $expect_out(0,string)
puts "file size = $size"

when the matching string is finally typed
expect returns. But, before returning, expect
stores the matched characters in a variable
called expect_out(0,string)