Find the answer to your Linux question:
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 ...
  1. #1
    Just 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?

  2. #2
    Linux 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

  3. #3
    Just 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?

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    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

  5. #5
    Just 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

  6. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    use cat, is what I'm trying to say

    Code:
    for i in `cat file` ; do
     echo $i
    done

  7. #7
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    Code:
    for address in `cat file`; do
       ssh user@$address 'command -to -execute'
    done
    You can authenticate your local user into all the servers using private/public keys to avoid the input of passwords on each server.

    Hugo
    EOF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...