Article 6M2XZ Expect called from bash; Unexpected EOF !

Expect called from bash; Unexpected EOF !

by
Honest Abe
from LinuxQuestions.org on (#6M2XZ)
I have few legacy machines which are about to be decommissioned. There is no scope of adding them to puppet/ansible etc. There are no ssh keys either as the user management is done via a third party tool and password is rotated weekly.

So, if I want to execute a script from a centralized jump box on these bunch of servers, expect is the only thing I can use to automate my tasks! I am only trying to fetch information from about ~20 servers.

These servers authenticate with a common username, but passwords are different for all.

Code sample-

Code:#!/bin/bash
serverlist=/path/to/map.txt
#username is common and password from file
user=admin
##login to each Server
#for i in $(cat $serverlist)
while IFS= read line
do
echo $line
set timeout 100
server= echo $line | awk -F ' ' '{print $1}' # first field is servername
password= echo $line | awk -F ' ' '{print $2}' # second field is password
#echo $server
#echo $password
/bin/expect << THIS
exp_internal 1
spawn ssh -q -o StrictHostKeyChecking=no -t "$user@$server"
expect ":"
send -- "$password\r"
expect "$ "
send "sudo -i\r"
expect ":"
send -- "$password\r"
expect "# "
send "hostname --fqdn;uname -a; who -rb; uptime \r"
expect "# "
send "exit \r"
THIS
done < $serverlistThe bash part of the script is very basic as I am merely setting up variables and extracting hostname and password from maps.txt (text file, fields separated by a single space). This part works without issues.
The problem starts with the EXPECT bit; specifically with the heredoc invocation.

Execution -
Code:$: bash -x test.sh
+ serverlist=/Path/to/map.txt
+ user=admin
test.sh: line 29: warning: here-document at line 15 delimited by end-of-file (wanted `THIS')
test.sh: line 30: syntax error: unexpected end of fileWhat I have done -
a. exp_internal 1 (adding or removing it does not give additional debugging info)
b. made sure my 'map.txt' is a unix file (verified with 'cat -A' and dos2unix to make sure there are no weird characters)
c. If I disable the expect block, the rest of the script does not have any errors.

What am I missing ?
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments