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
In reply to SSH and expect without module by touriste75
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |