touriste75 has asked for the wisdom of the Perl Monks concerning the following question:
Hi all Working with a linux virtual machine without the possibility to install any module, my goal is to connect in SSH to several material and without the result in STDOUT, pass in console some command. I tried without success
use strict; use warnings; use Data::Dumper; use feature 'say'; no warnings 'uninitialized'; my @fail; my @CLI; my $pass; my $mire; my @ip; my $ssh_cli = 'ssh -o StrictHostKeyChecking=no -o ConnectTimeout=3 -l' +; print 'Login: '; chomp (my $login = <>); print 'Password: '; chomp (my $password = <>); print 'Fichier IP: '; chomp (my $SOURCE = <>); print 'Fichier ligne de commandes:'; chomp (my $commande = <>); open (my $source, "<", $SOURCE) or die "Can't open: $!"; while (my $line = <$source>) { $line =~ s/[\r\n]+//; push @ip, $line; } open (my $cli, "<", $commande) or die "Can't open: $!"; while (my $line = <$cli>) { $line =~ s/[\r\n]+//; push @CLI, $line; } foreach my $ip (@ip) { my $SSH = system "$ssh_cli $login $ip"; if ($SSH =~ /username/i) { my $pass = print "$login\n"; } else { say "Connexion SSH $ip echec"; push @fail, $ip; say '-' x 20; next; } if ($SSH =~ /password/i) { my $mire = print "$password\n"; } else { say "Connexion SSH $ip echec"; push @fail, $ip; say '-' x 20; next } if ($SSH =~ /[>]$/) { foreach (@CLI) { say "$_"; my $resultat = print "$_\n"; print "$resultat"; } } else { say "Commande show version KO"; push @fail, $ip; next; } } say; say '### Connexion SSH KO ###'; foreach (@fail) { say "$_"; }
RESULT:
ZZZ@YYY:~$ perl SSH.pl
Login: TOTO
Password: TATA
Fichier IP: SSH.txt
Fichier ligne de commandes:SSH-commandes.txt
User Access Verification
Username:
I made another trie simpler with the module IPC::Open3 in the core to read and write the process but no more success.
use strict; use warnings; use Data::Dumper; use feature 'say'; no warnings 'uninitialized'; use IPC::Open3; print 'Login: '; chomp (my $login = <>); print 'Password: '; chomp (my $password = <>); print 'Fichier IP: '; chomp (my $SOURCE = <>); print 'Fichier ligne de commandes:'; chomp (my $commande = <>); my $cmd = 'ssh -o StrictHostKeyChecking=no -o ConnectTimeout=3 -l'; my $pid = open3(\*WRITER, \*READER, \*ERROR, $cmd); #if \*ERROR is 0, stderr goes to stdout while( my $output = <READER> ) { print "output->$output"; if ($output =~ /username/i) { print WRITER "nsoc-iec\n"; } }
RESULT:
ZZZ@YYY:~$ perl SSH.pl
Login: TOTO
Password: TATA
Fichier IP: SSH.txt
Fichier ligne de commandes:SSH-commandes.txt
ZZZ@YYY ~/SCRIPT
$
Someone have a lead ?
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: SSH and expect without module
by haukex (Archbishop) on Mar 15, 2017 at 15:46 UTC | |
by touriste75 (Acolyte) on Mar 16, 2017 at 13:41 UTC | |
by haukex (Archbishop) on Mar 16, 2017 at 19:29 UTC | |
by touriste75 (Acolyte) on Mar 19, 2017 at 09:09 UTC | |
by haukex (Archbishop) on Mar 19, 2017 at 11:49 UTC | |
| |
|
Re: SSH and expect without module
by thanos1983 (Parson) on Mar 15, 2017 at 15:56 UTC | |
by cbeckley (Curate) on Mar 15, 2017 at 20:54 UTC | |
|
Re: SSH and expect without module
by shmem (Chancellor) on Mar 15, 2017 at 17:51 UTC | |
|
Re: SSH and expect without module
by salva (Canon) on Mar 15, 2017 at 18:48 UTC |