1
votes

I have an expect script A to ssh that runs on host A, remotes to host B, runs bash script B on host B, then append output of script B back to a file on host A. After running this expect script, appended contents are exactly what I desire, BUT duplicated. For instance, if desired output is "abc", I'm getting "abcabc". The run timestamp for duplicate is same as original.

Please see expect script below:

#!/usr/bin/expect

 set ip ip

 set user "user"

 set password "password"

 set first " "

 spawn ssh $user@$ip /path/to/script/b/scriptb.sh

 expect { -re "(^ABC)|(^password)"

      set variable $expect_out(buffer)

      set first [cut -c1-3 $variable]

 }

 if {"$first" == "ABC"} {

      send "123"

      send "$password\r";

 } else {

      send "$password\r"

 }

 expect -re "(?s)-{30}\r\n(.*?)-{30}"

 puts $expect_out(0,string)

My hypothesis is that expect_out(buffer) has 2x the out and the 2nd expect -re is matching output twice. I tried using unset expect_out(buffer) unsuccessfully. I also played with the positioning of the second expect -re and puts statement with the if-statement to no avail. I apologize if fix is simple but have spent many hours on forums looking for it. Any ideas appreciated.

Regards,

Alan

1
Don't expect to ssh!! - F. Hauri
@F.Hauri Your comment is a little bit short. Why not explaining your concerns? - hek2mgl
@hek2mgl Sorry: Don't expect to ssh!! - F. Hauri
Where is the variable short defined? Whether host added as known hosts already? After spawning the SSH, you are waiting for either of password or some other pattern. Why? - Dinesh
F. Hauri - Thanks for the info, but it isn't practical to have ssh keys for me right now with number of servers that this script needs to go through. - Alan

1 Answers

0
votes

Fixed duplication issue; see updated code below:

expect { -re "(?s)-{30}\r\n(.*?)-{30}"

{puts $expect_out(0,string) }

}