0

I have noticed that my TCL/Expect Script is not writing to the Output Buffer.

`#!/usr/bin/expect -f

exp_internal 1; #Debug Expect

set username [lindex $argv 0]; #Takes Outside Argument
set password [lindex $argv 1]; #Takes Outside Argument
set hostFile [open hosts.txt r]; #Reads from a local file


while { [gets $hostFile hostname] >= 0 } {
spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname
expect "password: "

send  "$password\r"
expect "$ "

send "pbrun /bin/su -"
expect {
 "Operations Team.*" {
  puts "NewLine Was Caught After pbrun /bin/su - command"
  #break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
 }
 "Rejected.*" {
  puts "NewLine & Return Carraige Was Caught After pbrun /bin/su - command"
  #break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
 }
 #"^" { ;#This command will work because in the expect only picks up a    "^" character from the [send "pbrun /bin/su -"] command. This is an issues as the pbrun command has more ouput that just a "^", but expect only picks up the "^" character. You can use any other command like "ls", and expect does not write the ls command ouput to the buffer, only a newline character will be written to the buffer. This is an issue as I am unable to grab anything from expect.
 # puts "Newline Character Met"
 #}
}

send "hostname\r"
expect {
 -re {".{8}[0-9]{5}"} {; #Never Works
  puts "BUFFER:$expect_out(buffer):REFFUB"
 }
 "^" {; #Again, this will be met
  puts "Newline Character Met"
 }
}
}`

Any Help Is Much appreciated. I ran this in debug mode and indeed see that there is no output being written to the buffer for the pbrun /bin/su - or hostname command except for the ^ character.

  • 1
    you seem to be missing a \r at the end of send "pbrun /bin/su -". – meuh Feb 9 '17 at 15:29
1

Your actual problem: the pattern {".{8}[0-9]{5}"} will match a string containing a double quote followed by 8 characters followed by 5 digits followed by a double quote -- are those double quotes meant to be in your pattern? Sometimes people get confused by Tcl's quoting, so my question is not meant to carry any judgement with it.

Also, when you have 2 patterns in an expect block, any one can match first: are you expecting the 8 chars and 5 nums to be seen before the first newline?

Wait, ^ will match at the start of the first line. If you want to explicitly match a newline, use \n.


Unfortunately Tcl's comment handling can be confusing. The comment character # only starts a comment if it is seen where a command can go.

The comments you put inside your expect block do not comment out those lines.

expect {
 "Operations Team.*" {
  puts "NewLine Was Caught After pbrun /bin/su - command"
  #break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
 }
 "Rejected.*" {
  puts "NewLine & Return Carraige Was Caught After pbrun /bin/su - command"
  #break #Exit While loop, which will exit the server since there is only one While Loop aprund the whole SSH Session
 }
 #"^" { ;#This command will work because in the expect only picks up a    "^" character from the [send "pbrun /bin/su -"] command. This is an issues as the pbrun command has more ouput that just a "^", but expect only picks up the "^" character. You can use any other command like "ls", and expect does not write the ls command ouput to the buffer, only a newline character will be written to the buffer. This is an issue as I am unable to grab anything from expect.
 # puts "Newline Character Met"
 #}
}

You are essentially passing to the expect command a list containing pattern and action pairs. The 3rd pattern is literally the 4 characters #"^" and the 3rd action is a string containing a script that contains 3 comments.

  • Hi Glen, Thanks For your quick response! the issue begins in the second send command send "pbrun /bin/su -". See, my debug output looks like this in the second statement command send: sending "pbrun /bin/su -" to { exp6 } expect: does "" (spawn_id exp6) match glob pattern "Rejected*"? no "Operations Team*"? no pbrun /bin/su -. The first issue I see is that nothing is written to expects buffer right after executing the send command, which it should. So then expect decides to rexecute the second command. In the last send command, I removed the "", but the issue is the same as well. – chromechris Feb 9 '17 at 20:01
  • By the issue being the same as well, I mean that expect does not write to the buffer, so only "" is searched as the expect_out(buffer), instead of the actual output that my send commands triggered. So then expect times out on the pbrun command. Next is the hostname command. For some reason, expect compares the output of hostname to be pbrun /bin/su -, which is just weird. Finally, expect continues to send the hostname command with the pbrun command appended to it, and since one of the commands it sends is pbrun /bin/su -hostname\r\n, that incorrect command actually triggers an – chromechris Feb 9 '17 at 20:10
  • error, which outputs a string that matches the pattern .{8}[0-9]{5}, so puts "BUFFER:$expect_out(buffer):REFFUB" is printed. The problem I am seeing is that expect is simply sending the pbrun without writing the first output to the buffer, which is where the problems arise since the expected ouput should be comapred against the preeceding expect statement, but none are met, because expect never wrote the output of the send command after executing". However, in the first ssh & password command, expect writes the output to it's buffer, so the proceeding expect statements are met. – chromechris Feb 9 '17 at 20:16
  • You'll need to post the debugging output as a gist or pastebin or something. Did you notice meuh's comment about sending \r? That's like typing a command at the shell prompt and forgetting to hit Enter. – glenn jackman Feb 9 '17 at 20:49
  • 1
    Also, the commands you send will show up in the buffer. For the exact same reason that characters you type are displayed in the terminal. If you don't want that, try stty -echo – glenn jackman Feb 9 '17 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.