Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by garcimo (Novice)
on Apr 13, 2018 at 13:46 UTC ( #1212808=perlquestion: print w/replies, xml ) Need Help??
garcimo has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I need to scan about 190 servers for the presence of some users and output in csv in the following format:

hostname,user1,user2..userx

this snip of code grabs the desired lines from all the /etc/passwd but I do not know how to output the csv.
... chomp (my @hosts = `cat ssh-list.txt`); #print "@hosts\n"; my (%ssh, %ls); for my $host (@hosts) { $ssh{$host} = Net::OpenSSH->new($host, user=> $user_name, password= +> $password,master_stderr_discard => 1, async=>1, master_opts => [-o => "StrictHostKeyChecking no" ] ); } for my $host (@hosts) { chomp (my $user = $ssh{$host}->capture('egrep "^dba|^sea|^adm|^mwa|^ae +" /etc/passwd'));

could I use getpwent to parse the output of $user?

I am rather new to perl so sorry if the code is bad.

Replies are listed 'Best First'.
Re: parse /etc/passwd and output it as csv in hundred servers
by hippo (Abbot) on Apr 13, 2018 at 14:23 UTC
    could I use getpwent to parse the output of $user?

    No, and if all you want are the usernames you need nothing so fancy anwyay.

    #!/usr/bin/env perl use strict; use warnings; my $user = <<EOT; root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt EOT chomp ($user); # as you have done $user =~ s/:.*$//mg; $user =~ s/\n/,/g; print "hostname,$user\n";

    ... and don't start doing master_opts => [-o => "StrictHostKeyChecking no" ] without very good reason.

      THank you what is the meaning of the two lines you added:
      $user =~ s/:.*$//mg; $user =~ s/\n/,/g;

        Same thing hippo did but more idiomatic and less readable. :P

        say join ",", "hostname", $user =~ /^([^:]+)/gm;
Re: parse /etc/passwd and output it as csv in hundred servers
by thanos1983 (Vicar) on Apr 13, 2018 at 14:36 UTC

    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!
      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!
Re: parse /etc/passwd and output it as csv in hundred servers
by diotalevi (Canon) on Apr 16, 2018 at 03:32 UTC

    Hi, you could solve this in perl alone but this is also a practical use for parallel-ssh with a snip of perl to make your report.

    apt install pssh parallel-ssh -h HOSTNAMES_TXT -o /tmp/users 'cut -d: -f1 /etc/passwd' perl -le ' for my $file (@ARGV) { my ($host) = $file =~ m{/([^/]+)$}; open my($fh), $file or die "Error opening $file: $!"; my @users = <$fh>; chomp @users; print join(",", $host, @users) . "\n"; } ' /tmp/users/*

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: parse /etc/passwd and output it as csv in hundred servers
by Anonymous Monk on Apr 16, 2018 at 04:25 UTC
    Also if you are actually managing 190 servers using passwd files ... "oh my!" Your life would be made very-considerably easier if you could switch those machines to use a different, centralized source of authentication/authorization such as LDAP (MS OpenDirectory) or Kerberos.
      this is a new mission I got and yeah the former sys admin administer each server passwd locally. so I need to clean up all the one that have left the cie and still on the passwd and indeed move to ldap.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1212808]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-05-26 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?