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);}
		timeout { puts "i am timing out !!!\n"; }
        }

expect "Desktop>"

puts "Outcome1 = $outcome1\n"
puts "Outcome = $outcome\n"
send "exit\r"

I am trying to capture result of
"type C:\Users\Administrator\Desktop\\inputfile.txt "
outcome is returning empty. :confused:

Help is really appreciated.

Show us your input C:\Users\Administrator\Desktop\\inputfile.txt

Input file :-

It is reading correctly, but seems like buffer doesn't copy to "outcome".

I see the buffer filled with the content of the inputfile when i try running it in debug mode.

expect: set expect_out(0,string) "\u001b[3;32Htype C:\Users\Administrator\D\u001b[3;61Hesktop\inputfile.txt\u001b[5;1HSILENT = YES\u001b[6;1HIGNOREERRORS = ServerNotFound,BadPassword,FailedDependencies\u001b[7;1HSKIPTARGET = NO\u001b[8;1HSOURCEPATH = C:\Users\Administrator\Desktop\swpackages\u001b[9;1H[TARGETS]\u001b[10;1HHOST = 16.186.36.42\u001b[11;1HUID = Administrator\u001b[12;1HPWD = Administrator\u001b[13;1H[END]\u001b[14;1H[TARGETS]\u001b[15;1HHOST = 16.153.135.184\u001b[16;1HUID = Administrator\u001b[17;1HPWD = administrator\u001b[18;1H[END]\u001b[20;1HC:\Users\Administrator\Desktop>"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\u001b[3;32Htype C:\Users\Administrator\D\u001b[3;61Hesktop\inputfile.txt\u001b[5;1HSILENT = YES\u001b[6;1HIGNOREERRORS = ServerNotFound,BadPassword,FailedDependencies\u001b[7;1HSKIPTARGET = NO\u001b[8;1HSOURCEPATH = C:\Users\Administrator\Desktop\swpackages\u001b[9;1H[TARGETS]\u001b[10;1HHOST = 16.186.36.42\u001b[11;1HUID = Administrator\u001b[12;1HPWD = Administrator\u001b[13;1H[END]\u001b[14;1H[TARGETS]\u001b[15;1HHOST = 16.153.135.184\u001b[16;1HUID = Administrator\u001b[17;1HPWD = administrator\u001b[18;1H[END]\u001b[20;1HC:\Users\Administrator\Desktop>"
  • ignore those special character it's bcoz of VT100 conversion.

There are a few things I'm dubious about in that expect script:

no spawn command
prompt not set to Desktop>
$con not defined
function exp_send not defined

Anyway this example works for me and might be a good starting point for you:

#!/usr/bin/expect -f
match_max 500000
set timeout 30

spawn -noecho cmd

send "PROMPT=Desktop\$G\r"

set outcome1 {}
set outcome2 {}
set inputfile C:\\Users\\Administrator\\Desktop\\inputfile.txt

expect "Desktop>" {
    send "type $inputfile \r"
}
set timeout 30
 
expect {
    -re "\r\n(.*)\r\n.*Desktop>" { set outcome $expect_out(1,string) }
    timeout { puts "i am timing out !!!\n"; }
}

send "exit\r"
expect "Desktop>"

puts "Outcome1 = $outcome1\n"
puts "Outcome = $outcome\n"