Solved

another EXPECT problem

Posted on 2001-09-10
2
488 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
ID: 6532857
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
ID: 6630724
its works! thanx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

930 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

16 Experts available now in Live!

Get 1:1 Help Now