Expect Script - Not Seeing Output from While Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect Script - Not Seeing Output from While Loop
# 1  
Old 02-04-2016
Expect Script - Not Seeing Output from While Loop

I know something simple is missing here, "log_user 1" is set . . . after this utility opens ${InFile} (handle? for IntInFile) it needs to look for something to appear in the file ${IntInFile} and then send it to the spawned process. Then I am locking the file ${IntInFile} and clearing it out - expecting more activity later from another process which locks it the same way.

The purpose of this utility is to allow strings to appear and re-appear in a file, and drive the spawned process via those strings, which most likely will be options to menu programs etc.

All that works fine, I know that the activities are happening as directed by options/etc read from ${IntInFile}, except I never see any output from those activities. I do see standard out from the initial spawn (perhaps when ${MenuPrompt} gets matched?). Once I get past this I'll add some detection for when the spawned process ends to get out of the loop, that should be about it.

I've tried several things to get some output during these while loops, like the "expect *" and "puts . . .", nothing yet. Can anyone tell me what I'm missing here?

Code:
log_user 1

# Read hostname to connect to from arguments
set hostname    [ lindex $argv 0 ]
# Read ID
set adminID     [ lindex $argv 1 ]
# Read prompt to look for, to initiate interaction
set MenuPrompt  [ lindex $argv 2 ]
# Read input file used for interaction
set IntInFile   [ lindex $argv 3 ]
set IntInFileNm [ exec basename ${IntInFile} ]
set IntInFileDr [ exec dirname ${IntInFile} ]
# Read command to run
set RunCmd      [ lindex $argv 4 ]
# Set time to sleep between checks for input
set Sleep       5
# Set time to sleep between checks for input
set MyPID       [ pid ]
# Initialize var for commands to run to clean up after interaction input file
set Cmd         ""
# File handle/identifier for interaction input file.
set InFile      [ open ${IntInFile} ]

# The command/program should have started within this amount of time.
set timeout     25
# Set to name of running program.
set Me          "${argv0}"
# Set to name of running program's directory.
set MyDir       [ exec dirname ${Me} ]
# Set command to be spawned
set SpawnCmd    "ssh ${adminID}@${hostname} -oStrictHostKeyChecking=no \
    ${RunCmd}"

# Spawn SSH shell connection to target machine and execute command.
eval spawn -noecho ${SpawnCmd}

# Wait for IntInFile to have size, the send the contents as input to the
# spawned process, lock the IntInFile, empty it and then unlock it.
# Look for the specified menu prompt from the command/program called.
expect -re ${MenuPrompt} {
    while { 1 } {
        # Wait between checks for input to send.
        while { [ file size ${IntInFile} ]==0 } {
            expect * {
                puts "$expect_out(buffer)"
                sleep ${Sleep}
            }
        }
        # Send the chararacters in IntInFile to the process.
        gets ${InFile} Buff
        send -- "${Buff}\r"

        # Lock instance to clean out interaction input file.
        set Cmd "${MyDir}/InstLock.sh -n1 -wf \
            ${IntInFileDr}/Lock.${IntInFileNm} -p ${MyPID}"
        if [ catch [ eval exec ${Cmd} ] ] {
            puts "ERROR: executing ${Cmd} - ${CmdOut}"
        }
        # Remove interaction input file.
        set Cmd "rm ${IntInFile}"
        if [ catch [ eval exec ${Cmd} ] ] {
            puts "ERROR: executing ${Cmd} - ${CmdOut}"
        }
        # Re-initialize interaction input file.
        set Cmd "touch ${IntInFile}"
        if [ catch [ eval exec ${Cmd} ] ] {
            puts "ERROR: executing ${Cmd} - ${CmdOut}"
        }
        # Remove lock file.
        set Cmd "rm ${IntInFileDr}/Lock.${IntInFileNm}.1"
        if [ catch [ eval exec ${Cmd} ] ] {
            puts "ERROR: executing ${Cmd} - ${CmdOut}"
        }
    }
} timeout {
    puts "No response from command invoked - ${SpawnCmd}"
    puts "$expect_out(buffer)"
    send -- "^C"
    exit 1
}

Many Thanks in Advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture output from expect script

Hi I am new to Expect scripting. I have to connect to a remote server and capture the output. Here I need output of " send "list registered\r"" to be stored in a file. but after execution, /tmp/capture.txt is of 0 byte #!/usr/bin/expect spawn ssh abc@10.10.10.10 -p 5022 expect... (2 Replies)
Discussion started by: bns928
2 Replies

2. Shell Programming and Scripting

How do I use grep output in an expect script?

Hi, I am using expect to ssh to a remote host and run a program on the remote machine which has a variable runtime. I need to wait until it finishes so I can grab the output file of this program. I am trying to use the output of grep to know when the process finishes. I am trying to capture... (0 Replies)
Discussion started by: vagabond1964
0 Replies

3. Shell Programming and Scripting

How to extract an ipaddress and use it in for loop in expect script?

set fid set content close $fid ## Split into records on newlines set records send "records splited\n" send $records\n set a "test\n" send $a foreach rec $records { ## Split into fields on colons set fields #set fields puts $fields for {set x 1} {$x < 4} {incr x} {... (1 Reply)
Discussion started by: rachanaPatel
1 Replies

4. Shell Programming and Scripting

Help capturing output of expect script

match_max 500000 set timeout 30 set outcome1 {} set outcome2 {} set inputfile C:\\Users\\Administrator\\Desktop\\inputfile.txt send -i $con "\r"; expect -i $con "Desktop>" { exp_send "type $inputfile \r" } set timeout 30 expect { "Desktop>" { set outcome $expect_out(0,string);}... (3 Replies)
Discussion started by: cityprince143
3 Replies

5. Shell Programming and Scripting

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (14 Replies)
Discussion started by: thisissouvik
14 Replies

6. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

7. Shell Programming and Scripting

Loop with output to script?

Hey guys, I am VERY new to linux scripting and was wondering if you could help me with the following: essentially the use case is the following...a service crashes and a script must be executed to rerun 3000 entries one at a time....your options are to do each of those manually, 1 at a time... (6 Replies)
Discussion started by: wrnganswr
6 Replies

8. Shell Programming and Scripting

Expect script called in loop from Bash Script

Having issues with an expect script. I've been scripting bash, python, etc... for a couple years now, but just started to try and use Expect. Trying to create a script that takes in some arguments, and then for now, just runs a pwd command(for testing, final will be command I pass). Here is... (0 Replies)
Discussion started by: cbo0485
0 Replies

9. Shell Programming and Scripting

Expect - Comparison of expect value and loop selection

Hello All, I am trying to automate an installation process using expect and sh script. My problem is that during the installation process the expected value can change according to the situation. For Example if this is a first time installation then at step 3 I'll get "Do you want to accept... (0 Replies)
Discussion started by: alokrm
0 Replies

10. Shell Programming and Scripting

Expect script without user seeing output or input

I want a shell script to call an expect script but I want the expect script to run in the background so the user is not bothered with what is going on. Is there any way to do this? ---------- Post updated at 08:23 PM ---------- Previous update was at 07:39 PM ---------- got it it was ... (1 Reply)
Discussion started by: los21282
1 Replies
Login or Register to Ask a Question