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
shortdefined? Whether host added as known hosts already? After spawning the SSH, you are waiting for either of password or some other pattern. Why? - Dinesh