LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-17-2012, 08:49 AM   #1
isourabh.wadhwa
LQ Newbie
 
Registered: Jul 2010
Posts: 21

Rep: Reputation: 0
Exclamation Script to execute command to get info from multiple servers


I dont have password-less authentication and no one will allow me ti set it up.

We are okay with specifying passwords in script.

I dont have root login but can do everything using sudo.

I want o/p of "df -h" and "hostname" from a list of servers to a file present on the server on which I'll make the script.


to summarise:
I have one server named "Host" and 100 servers "node1/2/3...100"

i want o/p of "df -h" and "hostname" of "node1/2/3...100" in a file "/home/user1/data.log" on server "Host"



P.s. I dont want this to happen to every server in list parallely, sequentialy accessing these servers will do the work for me
 
Old 05-17-2012, 10:29 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,298

Rep: Reputation: Disabled
I would use a simple script to loop through the list of servers, and an expect script to do the actual logon and run the commands.

The logic of the script would look something like this:
for every $entry in servers.list
do
call the expect script with $entry as an argument, perhaps also a username and a password, redirect output to a logfile
done
Here's an implementation of the above as a bash script:

Code:
#!/bin/bash

set login=user
set password=MyPassword
set server_list=./servers.lst
set expect_script=./my_expect_script
set logfile=./data.log

cat $server_list | while read $servername ; do
  expect $expect_script $servername $login $password >> $logfile
done
This is a rough draft and completely untested. At the very least, you would want to add some lines to the log file before and after calling the expect script to indicate which server the information is coming from.

As for the expect script, the logic of that could be something like this:

- grab server name and login details from the command line arguments
- suppress output
- connect to the server with ssh
- wait for the password prompt
- send the password
- wait for the shell prompt
- enable output
- send "sudo df -h"
- wait for the shell prompt
- send "sudo hostname"
- wait for the shell prompt
- suppress output
- send "logout"
Here's an implementation of the above (not guaranteed to be bug free):

Code:
set hostname [lindex $argv 0]
set login [lindex $argv 1]
set password [lindex $argv 2]
set timeout 20

log_user 0
spawn ssh $login@$hostname
expect "password:" {
  send "$password\r"
}
expect "~$" {
  log_user 1
  send "sudo df -h\r"
}
expect "~$" {
  send "sudo hostname\r"
}
log_user 0
send "logout\r"

Last edited by Ser Olmy; 05-17-2012 at 12:34 PM. Reason: Typo
 
  


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
Executing df -k command from java is not working for multiple execute riyas_ppt Programming 1 01-13-2012 10:20 AM
How to execute multiple commands via SSH in a script angrymachine Linux - General 4 10-12-2011 10:05 AM
script to execute a command multiple times snr8fl3 Programming 5 11-17-2008 12:49 PM
LXer: Execute commands simultaneously on multiple servers LXer Syndicated Linux News 0 02-20-2006 11:31 AM
LXer: Execute Commands on Multiple Linux or UNIX Servers LXer Syndicated Linux News 0 12-27-2005 06:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:51 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