Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

SSHing in multiple servers and execute command on each

by sabas (Novice)
on Jun 26, 2018 at 23:14 UTC ( #1217479=perlquestion: print w/replies, xml ) Need Help??

sabas has asked for the wisdom of the Perl Monks concerning the following question:

Can someone help me provide a sample perl script where: 1. I will ssh user@xx.xxx.xx.xx into 25 different servers (25 different IP addressess) 2. Automatically login 3. execute some command like df -h on each of the 25 servers 4. write/capture the logfile and get the %use of the HD of each server (ip address) Thanks,

  • Comment on SSHing in multiple servers and execute command on each

Replies are listed 'Best First'.
Re: SSHing in multiple servers and execute command on each
by thanos1983 (Parson) on Jun 27, 2018 at 09:33 UTC

    Hello sabas,

    I had a similar case some time ago and I created multiple scenarios and solutions to my problem. My way of approaching your problem would be:

    Net::OpenSSH::Parallel already Anonymous Monk proposed it. Alternatively you can create a foreach loop to connect to all nodes one by one. I would not use password authentication if I was you I would use ssh key authentication (more secure). The approach of looping one by one to the nodes I do not recommend it as it can result in time consuming and also if one node for some reason can not be reached can block the rest of the queue.

    Sample of code for Net::OpenSSH::Parallel can be found here Re: multiple machines disk space alert. Sample of code using foreach loop and Net::OpenSSH can be found here Re: Simple SSH based chat client.

    Hope this helps. BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: SSHing in multiple servers and execute command on each
by NetWallah (Canon) on Jun 26, 2018 at 23:47 UTC
    Not addressing your immediate question, but in the long term, I'd suggest researching Ansible for such tasks.

    well worth the time investement for configuring multiple servers.

                    Memory fault   --   brain fried

Re: SSHing in multiple servers and execute command on each
by Anonymous Monk on Jun 26, 2018 at 23:31 UTC
Re: SSHing in multiple servers and execute command on each
by soonix (Canon) on Jun 27, 2018 at 09:17 UTC
Re: SSHing in multiple servers and execute command on each
by mr_mischief (Monsignor) on Jun 27, 2018 at 21:37 UTC

    This is not Perl specific, but are you aware of GNU Parallel? It can be used with or without Perl. Also, a task like this is unnecessary if you're using statsd, collect, Prometheus, or a number of other statistics gathering tools. Also, good old NetSNMP and an SNMP monitoring system (potentially written in Perl) solves this problem nicely.

    None of this is to say that your approach is the wrong wheel. It's just that there's a tire and rim shop next door already.

Re: SSHing in multiple servers and execute command on each
by Anonymous Monk on Jun 27, 2018 at 11:54 UTC
    Also Nagios, if you need to monitor the status of a lot of computers and periodically do things on them. You will find several open-source tools that already do this very common task.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1217479]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2020-11-26 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?