Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: parse /etc/passwd and output it as csv in hundred servers

by thanos1983 (Parson)
on Apr 13, 2018 at 14:36 UTC ( [id://1212815]=note: print w/replies, xml ) Need Help??


in reply to parse /etc/passwd and output it as csv in hundred servers

Hello garcimo,

Fellow Monk hippo answered your question already, but I would like to add some comments since you post your question.

Why you use 190 (passwords, username connections)? First of all it is less secure and second you use too many unnecessary resources, use ssh keys.

Second point of view, 190 connections will take more time and in case of one node is down you need to wait until timeout and proceed to next if not blocked. why not use Net::OpenSSH::Parallel?

Sample of code for both modules using ssh keys can be found here Re: Perl script works differently from Apache then CMD line.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: parse /etc/passwd and output it as csv in hundred servers
by garcimo (Novice) on Apr 13, 2018 at 16:21 UTC
    hello, I have a parallel script that i was trying to do :
    chomp (my @hosts = `cat /.root/sample.txt`); my $pssh = Net::OpenSSH::Parallel->new(); $pssh->add_host($_, user=> $user_name, master_stderr_discard =>1, master_opts => [-o => "StrictHostKeyChecking no" ]) fo +r @hosts; $pssh->push('*', command => 'egrep "^dba|^sea|^adm|^mwa|^ae" /etc/pass +wd'); $pssh->run;

    I use ssh keys where possible but the ssh public key is not added in all 190 servers.

    I do not use 190 different password.. just one that can connect to all the servers

    I cannot capture the output of the /etc/passwd found and produce a csv.. that is why I did not went through with parallel.. if you have a solution.. please let me know

      Hello again garcimo,

      Sorry for the late reply but I got busy and I missed your reply. Let's go through line by line what you said.

      I do not use 190 different password.. just one that can connect to all the servers

      I do not know if you use WindowsOS but in case you are using LinuxOS I would create a script and do the following (pseudo code):

      #!/usr/bin/perl use Expect; use strict; use warnings; my @devices = ("127.0.0.1", "localhost"); my $command = 'ssh'; my @params = ('-p', 22); # These is to bypass the prompt of each node for new ssh connection. foreach my $device (@devices) { # create an Expect object by spawning another process push @params, $device; my $exp = Expect->spawn($command, @params); $exp->send("yes"); } # Second step do the same with ssh-copy-id (ssh keys) # You said that all nodes share the same password so it should be very + easy to create ssh keys for all nodes from the main node.

      The idea of the script is to bypass the ssh prompt on all nodes and then create ssh keys on all nodes. See also relevant question SSH - Key Authentication for more information on that.

      After that it is upon you if you want to capture the output of each node on different file stored in specific dir or append each node on one file.

      If in any case my description is complicated comment under and I will try to reply with more details on the part that you do not understand.

      Hope this helps, BR

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        seems very nice. thank you

        I managed to remove the prompt asking for yes or no but cannot make it work for adding the public key using ssh-copy-id.

        this is the code, note that with push it never worked.
        #!/usr/bin/perl use Expect; use strict; use warnings; $Expect::Debug = 1; my $pass= '****'; my $timeout = 1; # chomp (my @devices = `cat sample.txt`); print "@devices\n"; # ## These is to bypass the prompt of each node for new ssh connection. foreach my $device (@devices) { # create an Expect object by spawning another process #push @params, $device; print $device; my $exp = Expect->spawn("ssh-copy-id $device"); if ($exp->expect($timeout, 'password')){ $exp->send("$pass\r"); } }
        any ideas? thank you

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1212815]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-03-29 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found