Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by garcimo (Novice)
on Apr 13, 2018 at 16:21 UTC ( [id://1212816]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: parse /etc/passwd and output it as csv in hundred servers
by thanos1983 (Parson) on Apr 15, 2018 at 16:15 UTC

    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

        Hello garcimo,

        There are plenty of reasons that the script that I told you could fail. I will explain just the basic ones:

        1) Are you sure that all the nodes (190) have ssh keys generated and the ssh-agent is added? If not follow this perfect tutorial from gitHub Generating a new SSH key and adding it to the ssh-agent. 2) Are you sure that the password that you provide is correct? I mean if you have special characters inside your password it could fail. You need to escape special characters e.g. \@, \$, \_ etc etc...make sure your password is correct. 3) Firewall, are you sure that you can ssh to all nodes? Many other reasons I could add here but there is no point, I would suggest that first make sure that you can ssh to all nodes, all nodes have generated ssh-keys, the agents are added and all can communicate with the main node.

        I put a small script that assumes that all the information is correct and checked and then it does the rest for you. It will ssh-copy-id for each node from main to the rest of the nodes and in case of either success or error it will print the output for you, so you can identify the node with the problem. Keep in mind that this is a minimum sample of script, you should hash the output of each node so you can identify the problem easier, or maybe some nodes will fail some will pass etc...etc...

        #!/usr/bin/perl use Expect; use strict; use warnings; use Data::Dumper; my $path_to_file = "test.txt"; open my $handle, '<', $path_to_file or die "Could not open file '".$path_to_file."': $!"; chomp(my @devices = <$handle>); close $handle or warn "Could not close '".$path_to_file."': $!"; # print Dumper \@devices; my @params; my $username = "user"; my $ssh_copy_id = "ssh-copy-id ".$username."\@"; my $password = "password"; # These is to bypass the foreach my $device (@devices) { # create an Expect object by spawning another process my $command = join('', $ssh_copy_id, $device); # print $command . "\n"; my $session = Expect->spawn($command) or die "Error calling external program: $!"; my $output; $session->expect(10, [ qr/passphrase/i, sub { my $self = shift; $self->send("$password\n"); exp_continue; }], [ qr/my comments/i, sub { my $self = shift; $output = $self->exp_before; exp_continue; }], ); print $output . "\n" if $output; $session->soft_close; }

        In case that all of that is too much work for you and you decide that you want to proceed with simple password procedure (for my point of view you should not) there is an old question that I raised that I was testing multiple ways on doing that and even applying sudo commands see here Best module to execute administrator commands on external operating systems (Linux), similar with the ssh parrallel module Net::OpenSSH::Parallel with sudo commands.

        In case that you come across other problems I would suggest raise another question, because this question is not related with other problems. :)

        Hope this helps, BR.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-19 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found