Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

another EXPECT problem

Posted on 2001-09-10
2
Medium Priority
?
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
kyrmit earned 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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 how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

604 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