Results 1 to 7 of 7
All,
I am looking for a bash script that will take a text file with a list of host names and then go through a loop and run an SSH ...
- 10-30-2009 #1Just Joined!
- Join Date
- Jun 2008
- Posts
- 8
Remote SSH command bash script with text file input
All,
I am looking for a bash script that will take a text file with a list of host names and then go through a loop and run an SSH command on each host in the list.
Any help?
- 10-30-2009 #2Linux Newbie
- Join Date
- Nov 2007
- Location
- Planet Earth
- Posts
- 152
Check the script in:
Shell Script Utility To Read a File Line By Line
and, into the processLine function, use:
Code:processLine(){ line="$@" # get all args ssh user@$line 'command -to -execute' }EOF
- 11-02-2009 #3Just Joined!
- Join Date
- Jun 2008
- Posts
- 8
Hugo, that script seems to have a lot of extra lines of code than what I am needing. Is there a smaller script you know of?
- 11-02-2009 #4
sounds like homework question, I would look at bash tutorial, this is a pretty simple task, should only be a few lines of script
Code:man cat
- 11-02-2009 #5Just Joined!
- Join Date
- Jun 2008
- Posts
- 8
lol not a homework question, this is for work. I need to run remote ssh commands but I don't want to have to paste the server list in the script, but want it to read a list of hostnames in a text file.
here is what I have so far, with the hostnames in the actual script.
#!/bin/bash
# Linux/UNIX box with ssh key based login
#Hosts
SERVERS=" servernames"
# SSH User name
USR="root"
# connect each host and pull up user listing
for host in $SERVERS
do
ssh $USR@$host 'command'
done
- 11-02-2009 #6
use cat, is what I'm trying to say
Code:for i in `cat file` ; do echo $i done
- 11-02-2009 #7Linux Newbie
- Join Date
- Nov 2007
- Location
- Planet Earth
- Posts
- 152
You can authenticate your local user into all the servers using private/public keys to avoid the input of passwords on each server.Code:for address in `cat file`; do ssh user@$address 'command -to -execute' done
HugoEOF


Reply With Quote