Solved

another EXPECT problem

Posted on 2001-09-10
2
495 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 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

726 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