+ Reply to Thread
Results 1 to 7 of 7

Thread: Running commands on a batch of computers

  Share/Bookmark
  1. #1

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation

    Join Date
    Aug 2005
    Location
    London
    Posts
    2,810
    Blog Entries
    2
    Thank Post
    74
    Thanked 443 Times in 389 Posts
    Rep Power
    98

    Default Running commands on a batch of computers

    How can I remotely execute the same command on a batch of computers?

    In Windows I'd do something like:
    Code:
    for %i in (1 2 3 4) do psexec \\computer%i <some command>
    
    and as long as I'm admin on the remote computers the command gets executed.

    I know I can use SSH to remotely execute a command but it prompts for a password every time (and if I've not connected to the machine before I also get asked to accept the key). I'd like to do this without having to type the password lots of times or I might as well just go and log on to 36 machines!

  2. #2

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation
    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Alton, Hampshire
    Posts
    3,483
    Thank Post
    620
    Thanked 396 Times in 342 Posts
    Rep Power
    101

    Default

    Quote Originally Posted by srochford View Post
    I know I can use SSH to remotely execute a command but it prompts for a password every time (and if I've not connected to the machine before I also get asked to accept the key).
    You generate a public / private SSH key pair, place the public key in the .ssh/authorized_keys file in the home folder of whichever user you wish to log on as on the remote machine you wish to log on to, then you tell ssh to log on to that machine using your private key - something like:

    ssh -o stricthostkeychecking=no -i my.key root@SERVERNAME commandGoesHere

    The stricthostkeychecking=no bit will skip you being asked whether you wish to accept the remote signature key. You might have to delete the local .ssh/known_hosts beforehand.

    --
    David Hicks

  3. Thanks to dhicks from:

    srochford (27-04-2009)

  4. #3

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    1,632
    Thank Post
    60
    Thanked 244 Times in 153 Posts
    Rep Power
    69

    Default

    Not an elegant (or secure!) solution but you can use EXPECT in scripting to respond to interactive prompts if necessary.

    expect(1) - Linux man page
    SSH login expect shell script to supply username and password

    Code:
    #!/usr/bin/expect -f
    
    # This script needs three argument to(s) connect to remote server:
    # password = Password of remote UNIX server, for root user.
    # ipaddr = IP Addreess of remote UNIX server, no hostname
    # scriptname = Path to remote script which will execute on remote server
    # For example:
    #  ./sshlogin.exp password 192.168.1.11 who
    # set Variables
    
    set password [lrange $argv 0 0]
    set ipaddr [lrange $argv 1 1]
    set scriptname [lrange $argv 2 2]
    set arg1 [lrange $argv 3 3]
    set timeout -1
    # now connect to remote UNIX box (ipaddr) with given script to execute
    spawn ssh -p 22 root@$ipaddr $scriptname $arg1
    match_max 100000
    
    expect {
           -re ".*Are.*.*yes.*no.*" {
           send "yes\r"
           exp_continue
           #look for the password prompt
           }
    
           "password:" {
           send -- "$password\r"
           #he expect command will now return
           }
    }
    
    Last edited by somabc; 23-04-2009 at 05:14 PM.

  5. #4

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation

    Join Date
    Dec 2005
    Location
    In the server room, with the lead pipe.
    Posts
    2,616
    Thank Post
    132
    Thanked 306 Times in 246 Posts
    Rep Power
    86

    Default

    You didn't mention whether you were querying for info or making changes, but if it's the latter you could also look at using puppet to automate things. Make a change to the reference file on the puppetmaster server and the puppets will all pull in the change when they next check in with it. Start with something simple and small (adding the school name to /etc/motd, for example) and move on from there.

    puppet - Trac

    Redmonk episode that led to me implementing it here: People Over Process » Puppet at Google - RedMonk Radio Episode 48

  6. #5

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    1,632
    Thank Post
    60
    Thanked 244 Times in 153 Posts
    Rep Power
    69

    Default

    There is also sshpass, but again with the need to hardcode passwords

    Key based login is really the way to go!

    sshpass: Login To SSH Server / Provide SSH Password Using A Shell Script

    How do I login over ssh without using password less RSA / DSA public keys? How do I use ssh in a shell script? How do I login non-interactivly performing password authentication with SSH and shell scripts?

    A. You can use sshpass command to provide password for ssh based login. From the man page:

    sshpass is a utility designed for running ssh using the mode referred to as "keyboard-interactive" password authentication, but in non-interactive mode.

    ssh uses direct TTY access to make sure that the password is indeed issued by an interactive keyboard user. Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user.

    The command to run is specified after sshpass' own options. Typically it will be "ssh" with arguments, but it can just as well be any other command. The password prompt used by ssh is, however, currently hardcoded into sshpass.

  7. #6

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    1,632
    Thank Post
    60
    Thanked 244 Times in 153 Posts
    Rep Power
    69

    Default

    This is EXACTLY what you want!

    ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection.

    SourceForge.net: Cluster SSH - Cluster Admin Via SSH
    ssh on multiple servers Using cluster ssh -- Debian Admin
    ClusterSSH - Cluster Wiki

  8. Thanks to somabc from:

    srochford (27-04-2009)

  9. #7

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation

    Join Date
    Aug 2005
    Location
    London
    Posts
    2,810
    Blog Entries
    2
    Thank Post
    74
    Thanked 443 Times in 389 Posts
    Rep Power
    98

    Default

    Thanks all - lots of new toys for me to play with :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 34
    Last Post: 30-10-2008, 03:52 PM
  2. PHP commands
    By Silverman in forum Web Development
    Replies: 10
    Last Post: 03-06-2008, 03:24 PM
  3. Replies: 19
    Last Post: 09-01-2008, 05:28 PM
  4. Running batch files in vista
    By Kained in forum Windows Vista
    Replies: 6
    Last Post: 04-09-2007, 02:25 PM
  5. Running local commands remotely
    By MK-2 in forum Windows
    Replies: 6
    Last Post: 02-03-2007, 10:31 AM

Posting Permissions

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