LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-31-2011, 07:00 PM   #1
Striketh
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Rep: Reputation: Disabled
Bash script to read line by line and execute commands


Hello,

I'm trying to figure out how to piece together a script that will do the following:

- Read a .txt file line by line. Each line will be the name of a server.
- SSH into each of these servers (keys are setup so no need for username/password). Just "ssh <servername>" will work.
- Execute a command on each server.
- Return the results in a coherent manner and place them in a text file. If possible, splitting the results up into separate text files based on $servername would be preferable.

I'm not much of a scripter, and I don't know enough to put this together properly. Any guidance would be highly appreciated.

Thanks for your time.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-01-2011, 02:00 AM   #2
lior.okman
LQ Newbie
 
Registered: Sep 2006
Distribution: Debian GNU/Linux
Posts: 9

Rep: Reputation: 0
I am assuming the command is 'ls -rtl'. Substitute my assumption for your command.
Try this:

Code:
while read servername; do
       ssh $servername ls -rtl > $servername-result.txt
done < "the-servers.txt"
 
Old 11-01-2011, 05:31 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
I personally would use
Code:
#!/bin/bash
while read server dummy ; do
    [ "${server//[#\/;]/}" = "$server" ] || continue
    [ "${#server}" -gt 0 ] || continue
    ssh "$server" "$@" </dev/null &>"$server.log" &
done
wait
The command to run is specified as a parameter to the script, and the list of servers as input, i.e.
Code:
./script ls -laF < servers.txt
The dummy variable makes sure the server will only contain the first word on each line. You may wish to extend the format later, so requiring the first word to be the server name makes sense.

The first test makes sure the server name does not contain hashes (#), slashes (/), or semicolons (;). Hash and semicolon are often used to indicate a comment line. Slash would make it impossible to create a file based on the server name. None of the three should occur in a server name or address anyway.

The second test makes sure the server name is not empty. Empty lines are common at the end of files, so handling them gracefully is a good idea.

The ampersand (&) backgrounds each ssh command, and the final wait waits until all ssh commands have completed. Since the ssh commands are done in parallel, I redirect the input to the ssh commands from /dev/null -- no user input is possible. It would not make sense to allow it anyway, because you could not tell which ssh command you were supplying input to.
 
2 members found this post helpful.
Old 11-01-2011, 11:35 AM   #4
Striketh
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Nominal Animal View Post
I personally would use
Code:
#!/bin/bash
while read server dummy ; do
    [ "${server//[#\/;]/}" = "$server" ] || continue
    [ "${#server}" -gt 0 ] || continue
    ssh "$server" "$@" </dev/null &>"$server.log" &
done
wait
The command to run is specified as a parameter to the script, and the list of servers as input, i.e.
Code:
./script ls -laF < servers.txt
The dummy variable makes sure the server will only contain the first word on each line. You may wish to extend the format later, so requiring the first word to be the server name makes sense.

The first test makes sure the server name does not contain hashes (#), slashes (/), or semicolons (;). Hash and semicolon are often used to indicate a comment line. Slash would make it impossible to create a file based on the server name. None of the three should occur in a server name or address anyway.

The second test makes sure the server name is not empty. Empty lines are common at the end of files, so handling them gracefully is a good idea.

The ampersand (&) backgrounds each ssh command, and the final wait waits until all ssh commands have completed. Since the ssh commands are done in parallel, I redirect the input to the ssh commands from /dev/null -- no user input is possible. It would not make sense to allow it anyway, because you could not tell which ssh command you were supplying input to.
This is fantastic - also, thank you for the explanation. I knew what I wanted to do, and had some ideas on how to do it, but while I could figure out X and Z, I couldn't think of how to put it all together for XYZ

That worked perfectly, though. Again, thanks a lot for your help!
 
Old 11-06-2011, 12:38 PM   #5
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 183

Rep: Reputation: 25
please click "mark solved".
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 09:16 PM
Read a line of C comamnd and execute in Shell Script mark.cooray Linux - Newbie 6 11-11-2010 10:22 PM
BASH Script, command line, same commands, different returns (need help). SilversleevesX Linux - Newbie 4 02-04-2010 04:46 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM


All times are GMT -5. The time now is 06:54 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration