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
Fixed duplication issue; see updated code below:
expect { -re "(?s)-{30}\\r\\n(.*?)-{30}"
{puts $expect_out(0,string) }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.