LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-14-2012, 12:15 PM   #1
itamarm
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).

#!/bin/bash
#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++))
do
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"}

EOF

done


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.

Thanks,
Itamar
 
Old 01-14-2012, 12:37 PM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

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, 01:48 PM   #3
itamarm
LQ Newbie
 
Registered: Mar 2008
Posts: 7

Original Poster
Rep: Reputation: 0
The server.list file is simple like this:
(ip-addressassword)
10.10.10.10:balidada
10.10.10.11:balimama
.
.
.
10.123.12.10:banana
10.90.90.12:archipat
.
.
.
etc'

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, 02:22 PM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

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.


Quote:

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

ssh root@$ipaddr
if [ "$?" -eq "0" ]
then
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



else

echo Connection unsuccessful >log.txt
exit
fi

Last edited by cbtshare; 01-14-2012 at 02:24 PM.
 
Old 01-14-2012, 02:33 PM   #5
itamarm
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"
 
  


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
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 10:06 PM
[SOLVED] spawn with Expect script najee1987 Linux - General 5 05-25-2011 02:53 AM
send: spawn id exp1025 not open in tcl/expect bharatbsharma Programming 3 04-22-2010 03: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 04:50 PM
Unexpected curly braces in expect script spawn command & bash suid problem slinx Programming 1 05-02-2008 02:47 PM


All times are GMT -5. The time now is 03:11 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration