LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2012, 05:36 PM   #1
Obscurious
LQ Newbie
 
Registered: Jun 2009
Distribution: Debian, RHEL, FreeBSD
Posts: 17

Rep: Reputation: 0
Bash and Expect to Update a Large List of Servers


I am attempting to write a script that will run system updates on about 30 linux servers (mixed distributions). For now I am focusing on debian to keep things simple. The script is composed of three parts: A bash script that reads a file containing the list of servers and executes an expect script located on each server. I have a decent grasp of bash and am very new to expect with little no knowledge of how bash and expect behave together.

The list of servers is written in a hosts file: all_hosts

Code:
? List of servers to update
? Preceeding question mark is a commented line
?
? port separated by percent then hostname
? port%server
22%server1
8912%server2
189%server3
The main bash script reads this file and loops through. This part of the script works. I have been using this for some time now to move files between all my servers. Here is the main bash script: update_all

Code:
 1 #!/bin/bash
  2 
  3 # update_all
  4 
  5 # There are three variables accepted via commandline
  6 # $1 = first parameter (/source_path/source_filename)
  7 # $2 = second parameter (/target_directory/)
  8 # $3 = third paramter (file that contains list of hosts)
  9 
 10 HOSTFILE=$1
 11 PASS=$2
 12 
 13 if [ $# -ne 2 ]; then
 14         echo "usage: $0 [hostsfile] [password]"
 15         exit 1
 16 elif [ -f $HOSTFILE ]; then
 17    printf "Begining update process ...\n"
 18    while read line; do
 19         COMMENT=`expr index "$line" ?`
 20         if [ $COMMENT -lt 1 ]; then
 21                 SEPER=`expr index "$line" %`
 22                 SERVER=${line:SEPER}
 23                 let SEPER-=1
 24                 PORT=${line:0:SEPER}
 25                 echo "$SERVER: "
 26                 ssh -p $PORT $SERVER '/home/user/update $2'
 27         fi
 28         done < $HOSTFILE
 29 else
 30    printf "File \"$HOSTFILE\" not found\n"
 31    exit 0
 32 fi
 33 exit 0
You can see on line 26 in update_all, the update process is executed via SSH. So on each server there is an expect script called update which is an expect script that looks like:

Code:
  1 #!/usr/bin/expect
  2 
  3 set timeout 10
  4 
  5 set pass [lindex $argv 0]
  6 
  7 spawn sudo apt-get clean
  8 expect "*?assword*"
  9 send "$pass\r";
 10 interact
 11 
 12 spawn sudo apt-get update
 13 expect "*?assword*"
 14 send "$pass\r";
 15 interact
 16 
 17 spawn sudo apt-get -yq upgrade
 18 expect "*?assword*"
 19 send "$pass\r";
 20 interact
Now this expect script works when executed locally on each server. So the command looks like:

Code:
$ ./update "mypassword"
Back on my central server if I put it all together:

Code:
$ ./update_all all_hosts "mypassword"
Begining update process ...
server1: 
spawn sudo apt-get clean
[sudo] password for user: interact: spawn id exp0 not open
    while executing
"interact"
    (file "/home/user/update" line 15)
I have scoured google around this error but I believe the problem is a fundamental misunderstanding on my part. All the information I have found points to using different methods alltogether, for example: stackoverflow.com/how-can-i-make-an-expect-script-prompt-for-a-password. My questions are thus:
  1. Does my approach seem reasonable? As in, using bash and expect in this manner.
  2. Does this approach have any security risks?
  3. What does the above error mean and how can I mitigate it?

Thanks.

Last edited by Obscurious; 10-16-2012 at 05:42 PM.
 
Old 10-16-2012, 05:48 PM   #2
Obscurious
LQ Newbie
 
Registered: Jun 2009
Distribution: Debian, RHEL, FreeBSD
Posts: 17

Original Poster
Rep: Reputation: 0
I should also add that I have a uniform account on all my servers hooked up to LDAP, so my sudo password is the same on all hosts. I also have SSH keys setup so the SSH command requires no password.
 
Old 10-17-2012, 12:14 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

In this line
Code:
  ssh -p $PORT $SERVER '/home/user/update $2'
you use single quotes, so this will not work unless your sudo password looks exactly like '$2' (literally).

From man bash:
Quote:
Enclosing characters in single quotes preserves the literal value of each character within
the quotes. A single quote may not occur between single quotes, even when preceded by a
backslash.

Enclosing characters in double quotes preserves the literal value of all characters within
the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The
characters $ and ` retain their special meaning within double quotes. The backslash retains
its special meaning only when followed by one of the following characters: $, `, ", \, or
<newline>. A double quote may be quoted within double quotes by preceding it with a back‐
slash. If enabled, history expansion will be performed unless an ! appearing in double
quotes is escaped using a backslash. The backslash preceding the ! is not removed.
Try double quotes "..." instead.

Last edited by firstfire; 10-17-2012 at 12:16 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh to servers with spawn,expect,send by script itamarm Linux - Newbie 4 01-14-2012 01:33 PM
how to use expect - send to execute list of commands inside a file auma78 Linux - General 1 02-02-2011 12:00 PM
Bash script to fgrep a large file. With list as source for searching. the_file Linux - Newbie 14 01-24-2011 03:42 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
Good list / forum for expect programmer? Hewson Programming 2 05-04-2007 02:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration