Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-14-2012, 11:15 AM   #1
LQ Newbie
Registered: Mar 2008
Posts: 7

Rep: Reputation: 0
ssh to servers with spawn,expect,send by script

Hi all,

I have a power outage this week and I would like to safely close the DB on each server and turn it off.
(around 50 DB servers)

I thought of making a script that shutdown the DB on each server and shut it down. the script uses a file with the DB servers IP and their password (server.list).

#This script connect to a server, stop its oracle db and shut it down via ssh.
#The script write it's output to log file

line_sum=$(wc -l server.list |cut -d' ' -f1)
echo num of servers in list are - $line_sum
for ((count=1; count<=$line_sum; count++))
ipaddr=$(sed -n "$count"p server.list | cut -d':' -f1)
pass=$(sed -n "$count"p server.list | cut -d':' -f2)

/usr/bin/expect <<EOF
set timeout 5
log_file -a log
spawn ssh root@$ipaddr
expect "continue connecting (yes/no)?" {send "yes\r"}
expect "password:" {send "$pass\r"}
#expect "#" { send "the command to close the DB"}
expect "#" {send "poweroff\r"}
expect "#" {send "exit\r"}
expect "#" {send "\r"}



Is there a better way to achieve my goal?
Is there a way to insert echo command into a file like after sending the command poweroff?

I am a newbie and not sure that the script is well planed but it works.

Old 01-14-2012, 11:37 AM   #2
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
I saw a few things that wont seem to work, but if you say it does why do you want a better way?

We'd have to see the file server.list file then make suggestions based off that.You can also use the sed command to insert the command or anything you want in the file
Old 01-14-2012, 12:48 PM   #3
LQ Newbie
Registered: Mar 2008
Posts: 7

Original Poster
Rep: Reputation: 0
The server.list file is simple like this:

The script works but I would like to send a comment to a log file when the connection is establish and then another comment to the log file that the DB is closed, etc'. I don't know how to do it on the expect area.
Also I am asking for better way because it's a loop and in each loop i execute the command spawn which start a process, Is that OK? wouldn't it cause many unnecessary processes running?
Old 01-14-2012, 01:22 PM   #4
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
Well the first thing is you need to setup serverkeys so it doesnt ask for passwords when you ssh.No need to use spawn command either.Below is just how to do the two things you wanted to do.


#Send a message to the log file is the connection is successfull

ssh root@$ipaddr
if [ "$?" -eq "0" ]
echo Connection succesfull >log.txt

#To add the sed DB message simply add the line in the for loop.

sed'/$ipaddr/ a\Server DB shutdown/' server.list


echo Connection unsuccessful >log.txt

Last edited by cbtshare; 01-14-2012 at 01:24 PM.
Old 01-14-2012, 01:33 PM   #5
LQ Newbie
Registered: Mar 2008
Posts: 7

Original Poster
Rep: Reputation: 0
the problem with echo text to > file is that it dose not work on the /usr/bin/expect area:

invalid command name "echo"
while executing
"echo connected ok > log.file"


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
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
[SOLVED] spawn with Expect script najee1987 Linux - General 5 05-25-2011 01:53 AM
send: spawn id exp1025 not open in tcl/expect bharatbsharma Programming 3 04-22-2010 02:04 AM
[SOLVED] Using an expect Script to send the output of cat and append it to a file spoofedexistence Programming 1 09-29-2009 03:50 PM
Unexpected curly braces in expect script spawn command & bash suid problem slinx Programming 1 05-02-2008 01:47 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:50 PM.

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