Solved

another EXPECT problem

Posted on 2001-09-10
2
485 Views
Last Modified: 2012-06-27
Dear Solutions,

I have now one problem with expect where I am trying to generate one script to automate file transfer. The script will be run from machine A which will then telnet to machine B and create another Expect script on machine B which will then spawn another process into machine C and do the file transfer.

Since there could be more than 1 file to be transfer, i wanted to have a loop where, if there
is still file on then, continue transfering.

This is how the script looks like:

#!/usr/local/bin/expect -f

spawn telnet fieldu6
expect "login:"
send "spett\r"
expect "Password:"
send "spett\r"

expect "spett@fieldu6:"
send "cat>next-hop<<EOF\r"
expect "?"
send "#!/usr/local/bin/expect -f\r"
expect "?"
send "spawn ftp eqm31s14p2\r"
expect "?"
send "set transfer_flag 1 \r"
expect "?"
send "expect \"Name (eqm31s14p2:spett):\"\r"
expect "?"
send "send \"root\\r\"\r"
expect "?"
send "expect \"Password:\"\r"
expect "?"
send "send \"wpp_admin\\r\"\r"
expect "?"
send "expect \"ftp>\"\r"
expect "?"
send "send \"bin\\r\"\r"
expect "?"
send "send \"cd /Core/log/\\r\"\r"
expect "?"
send "expect \"ftp>\"\r"
expect "?"
send "send \"mget *.Z\\r\"\r"
expect "?"
send "send \"mget *.Z\\r\"\r"
expect "?"
send "expect \"*Z?\"\r"
expect "?"
send "send \"y\\r\"\r"
expect "?"
send "while {\$transfer_flag} { \\\r" #problem.
expect "?"
send "if {expect \"*Z?\"} { \\\r"
expect "?"
send "send \"y\\r\" \\\r"
expect "?"
send "} else { \\\r"
expect "?"
send "set transfer_flag 0 \\\r"
expect "?"
send "} \r"
expect "?"
send "interact\r"
expect "?"
send "EOF\r"
expect "spett@fieldu6:"
send "chmod +x next-hop\r"
expect "spett@fieldu6:"
send "./next-hop\r"
interact

>>end of script
....continue here
? expect "ftp>"
? send "mget *.Z\r"
? expect "*Z?"
? send "y\r"
? while {$transfer_flag} { \  #problem here
transfer_flag: Undefined variable.
spett@fieldu6: /home/spett                       42 > if {expect "*Z?"} { \
? send "y\r" \
? } else { \
? set transfer_flag 0 \
? }
Missing }.
spett@fieldu6: /home/spett                        43 > interact


When i try to run this code, i have this error message, which to me seems that I didnt really escape the '$'sign which i think i already did.
How to solve this problem.

thanks alot.
0
Comment
Question by:mafendee
2 Comments
 
LVL 2

Accepted Solution

by:
kyrmit earned 100 total points
Comment Utility
Your main problem is that the cat<<EOF>next-hop is a shell command and thus subject to the usual shell escapes. So not only does expect interpolate the $, but also the shell.

You can avoid a lot of hassle caused by the interpolation of expect here by using {} :
send {cat<<EOF>next-hop
...
while { \$transfer_flag } {
..
EOF\r
}

this allows the secondary script to be passwd without any interpolation, i.e. $ stay $ and " stay ", particularely the \$ stays \$ for the shell.

However, you may find the ftp command 'prompt' useful which
toggles the question during mget from on to off. See the ftp man page for it's usage. By issuing "bin" and "prompt" the ftp client will never ask you if you really want to get a file during mget. This bypasses the need for the while loop in this case altogether.

One more thing: there is an error in the loop in that expect is used as the parameter to an if. This won't work, try something the like. I am using a catchall fallback by
using the regular expression .*.

while { $transfer_flag } {
        expect -re ".*\? " {
                send "y\r"
        } -re ".*" {
                set transfer_flag 0
        }
}

Hope this helps
0
 

Author Comment

by:mafendee
Comment Utility
its works! thanx
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now