sending an ssh -t command to multiple systems simultaneously (without ansible)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



sending an ssh -t command to multiple systems simultaneously (without ansible)



Because of the nature of the script (done at work, on a work RHEL machine) I cannot show the code, but I can at least provide pseudocode to help with a starting point. Currently:



start loop



1) read in the first line of a host text file (then the next and such per
the loop) of a file and assign it to a variable (host name)



2) send ssh -t command to the host (which takes anywhere between 2 to 6
minutes to receive a response back)



3) log response to a text file (repeat loop with new host from read in
text file)



end loop



Currently I have to run this script over night because of how many systems this script hits.



I want to be able to achieve the same goal and get the response from the command in that file per host, but I want the command to be sent out at the same time so that it takes anywhere between 2 to 6 minutes all together.
But because this is for work, I am not allowed to install ansible on the system; would there be another way to achieve this goal? If so please provide some areas or point me in the right direction.





gnu-parallel or append a simple &?
– Cyrus
Aug 10 at 18:09


&





& is the best option. Psuedocode answer: ssh $hostname "somecommand >./locallogfile" & in a loop, then scp the ./locallogfiles to your main system 2-6 minutes later.
– jeremysprofile
Aug 10 at 18:13


&


ssh $hostname "somecommand >./locallogfile" &


scp





@jeremysprofile probably should use >> to write the file here
– Inder
Aug 10 at 18:24





@Inder, why? If he only wants the output of this command, each logfile for the remote systems should use >. He'll have to scp and cat once he moves them to his main system. I'll admit that I probably should have named it remotelogfile to be more clear, but it won't let me edit the comment anymore.
– jeremysprofile
Aug 10 at 18:26



>


scp


cat


remotelogfile





I am not allowed to install ansible instead of reinventing the wheel by introducing a ton of race conditions and bad timeout handlings you should ask where this rule comes from and why (not necessarily for this specific tool but any of its kind, you have in the same way chef, puppet, or libraries like fabric in Python, etc...) by explaining that this would do a far better job than any local homegrown solution. I am sure they did not write the local bash and ssh but installed them from outside, so why forbidding other tools that are relevant to the job?
– Patrick Mevzek
Aug 10 at 18:45


chef


puppet


fabric


bash


ssh




2 Answers
2



With GNU Parallel:


parallel -j0 --slf hosts.txt --nonall mycommand > out.txt



But maybe you want a bit more info:


parallel -j0 --slf hosts.txt --joblog my.log --tag --nonall mycommand > out.txt



I did this using sh years ago using something like:


sh


while true
do
if [ numberOfFileinSomeDir -lt N ]
then
(touch SomeDir/hostname; ssh hostname ... > someotherDir/hostname.txt ; rm SomeDir/hostname) &
...



But this stops working after ~100 hosts. It sucks - don't do it. If less than about ~500 hosts pssh may be the easiest - maybe you can install in your home directory?



Google something like python parallel execute process multiple and someone's bound to have a script that will do what you need already.


python parallel execute process multiple



More than ~500 hosts and you really need to start installing some tools as others have mentioned in the comments.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard