Dren
Dren

Reputation: 319

lines from a file as variable in expect script

Need some help creating a loop in expect script where the variable is extracted from lines in a file.My current expect script is like below;

#!/usr/bin/expect -f
set i [open "samplelist"]
set hosts [split [read $i] "\n"]

set timeout -1
foreach host $hosts {
spawn /usr/bin/ssh appadm@$host
expect "appadm@$host:~>"
send "su epos\r"
expect "Password:"
send "pa55w0rd\r"
expect "epos@$host:/home/appadm>"
send "grep playlist /appl/epos/bin/cron.epos\r"
expect "epos@$host:/home/appadm>"
send "exit\r"
expect "appadm@$host:~>"
send "exit\r"
}
expect eof
close

However when i ran this script it does not terminate correctly after reading the last line of the file

Upvotes: 1

Views: 2735

Answers (1)

Dren
Dren

Reputation: 319

Final modified expect script after being guided by Glenn

#!/usr/bin/expect -f
set i [open "samplelist"]
set hosts [split [read -nonewline $i] "\n"]

set timeout -1
foreach host $hosts {
spawn /usr/bin/ssh appadm@$host
expect "appadm@$host:~>"
send "su epos\r"
expect "Password:"
send "p@ssw0rd\r"
expect "epos@$host:/home/appadm>"
send "grep playlist /appl/epos/bin/cron.epos\r"
expect "epos@$host:/home/appadm>"
send "exit\r"
expect "appadm@$host:~>"
send "exit\r"
expect eof
}

The scripts reads a list of hosts from a file, loop connects and run a command to the remote host until the end of the list.

Upvotes: 1

Related Questions