1

I searched high and low to find an app to make my life easier.. basically I have a list of hundreds of IPs all with the same SSH key that I want to connect to perform a couple of commands and then exit..

Right now all I can do is opening each individually and executing the commands, I've used PuTTy and MobaXterm, but doing that is VERY time consuming for something that's not that hard..

So I was looking for a piece of software that'd allow me to connect to multiple SSH sessions at once from an IP list and execute the same commands over them.. or at least something to make my life easier..

Thank you.

3 Answers 3

0

May not be feasible for "hundreds of IPs" at once, but if you want to perform the same steps on multiple machines while having visual feeback for each machine take a look at ClusterSSH:

The command opens an administration console and an xterm to all specified hosts. Any text typed into the administration console is replicated to all windows. All windows may also be typed into directly.

I use that to administrate my little "farm" at home, e.g. logging into 4 Linux machines at once to perform apt-get update && apt-get upgrade. LinuxMint has a nice screenshot of this:

ClusterSSH

And if you want to see it in action, here's a small Youtube video (more demos linked from there).

I use ClusterSSH for years (don't even remember when I started), and am quite satisfied with it. Ships in the repo of most Linux distributions – on Debian, Ubuntu and derivates, simply apt install clusterssh.

1

It's possible to automate the command execution through SSH using plink.exe which is include in Putty installation.

Using plink

plink.exe allow you to use Putty in command line. For exemple in a batch script :

plink.exe <user>@<server.ip> -P <SSH.port> -pw <password> -m command_list.txt

It is also possible to retrieve the result of the command with :

plink.exe <user>@<server.ip> -P <SSH.port> -pw <password> -m command_list.txt >> result_computerXX.txt

You can download plink.exe here.

You can also use WinSCP.com (here) which is doing the same.

3
  • How would it look like if I use an SSH key instead of a password?
    – Helme
    Commented Aug 11, 2017 at 14:06
  • when I used -i instead of -pw and then pasted my private key it gave me this error Unable to use key file "ssh-rsa" (unable to open file) FATAL ERROR: Disconnected: No supported authentication methods available (server sent: publickey)
    – Helme
    Commented Aug 11, 2017 at 14:26
  • @Helme Please note that Putty accept only "linear" key like ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIE.... If you give a key like ---- BEGIN SSH2 PUBLIC KEY ---- Comment: "rsa-key-20121022" AAAAB3NzaC1yc2EAAAABJQAAAIEAh... Putty will not accept it. If it's not clear i can update my previous post.
    – Guillaume
    Commented Aug 12, 2017 at 14:59
1

You can use Python with the paramiko module:

import paramiko

commands = [ "/home/user/firstscript.sh", "/home/user/secondscript.sh" ]
host_list=[{"host":"1.2.3.4", "user":"thisuser"},{"host":"2.3.4.5", "user":"thatuser"}]

k = paramiko.RSAKey.from_private_key_file("/Users/whatever/Downloads/mykey.pem")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for user,host in hostlist.items():

    print "connecting to " + str(host)
    c.connect( hostname = host, username = user, pkey = k )
    print "connected"

    for command in commands:
        print "Executing {}".format( command )
        stdin , stdout, stderr = c.exec_command(command)
        print stdout.read()
        print( "Errors")
        print stderr.read()

    c.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.