Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Net::OpenSSH pass command

by touriste75 (Acolyte)
on May 11, 2021 at 17:42 UTC ( #11132420=perlquestion: print w/replies, xml ) Need Help??

touriste75 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I try to use the module Net::OpenSSH with a Cisco to make simple test. I wish to execute 3 commands, without saving the result. The comman number 4 must capture the result. I use the system command for the 3 first and capture for the command 4. Unfortunatly, the command system seems to make problem.
use Net::OpenSSH; my $host = '192.168.109.10'; my $user = 'admin'; my $password = 'admin'; my $port = '22'; my $ssh = Net::OpenSSH->new( host =>$host, user => $user, port => $port, password => $password); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error +; my $fichier_dst = "$host\.txt"; open (FH_OUT, '>', '/home/alexandre/Python_script/CISCO/' . $fichier_d +st);; my $cmd1 = 'conf t'; my $cmd2 = 'hostname TOTO'; my $cmd3 = 'exit'; $ssh->system("$cmd1"); $ssh->system("$cmd2"); $ssh->system("$cmd3"); my $cmd4 = 'sh run \| i hostname'; my ($out, $err) = $ssh->capture2("$cmd4"); $ssh->error and die "remote find command failed: " . $ssh->error; print "$out\n"; print FH_OUT $out; close(FH_OUT);

Here the resultat.
Seems to make two connexions and blocked with password

If I use only the command capture, no problem of connexion.

**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS  *
* education. IOSv is provided as-is and is not supported by Cisco's      *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any       *
* purposes is expressly prohibited except as otherwise authorized by     *
* Cisco in writing.                                                      *
**************************************************************************
Enter configuration commands, one per line.  End with CNTL/Z.Connection to 192.168.109.10 closed by remote host.

**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS  *
* education. IOSv is provided as-is and is not supported by Cisco's      *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any       *
* purposes is expressly prohibited except as otherwise authorized by     *
* Cisco in writing.                                                      *
**************************************************************************Password: 

Thanks for your help.

Replies are listed 'Best First'.
Re: Net::OpenSSH pass command
by salva (Canon) on May 12, 2021 at 06:48 UTC
    Enable debugging on Net::OpenSSH and/or ssh in order to see what is going on:
    # Before using Net::OpenSSH: $Net::OpenSSH::debug = ~512; ... # And then, on the constructor my $ssh = Net::OpenSSH->new(..., master_opts => '-vvv');
      thanks for help. I have this result

      # open_ex: 'ssh','-V' # io3 mloop, cin: 0, cout: 1, cerr: 0 # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 60 at offset 0 #> 4f 70 65 6e 53 53 48 5f 37 2e 36 70 31 20 55 62 75 6e 74 75 2d 34 75 62 75 6e 74 75 30 2e 33 2c | OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, #> 20 4f 70 65 6e 53 53 4c 20 31 2e 30 2e 32 6e 20 20 37 20 44 65 63 20 32 30 31 37 0a | OpenSSL 1.0.2n 7 Dec 2017. # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 0 at offset 60 # leaving _io3() # waiting for slave, timeout: 10, remaining: 10, sleep: 1 # _waitpid(3335) => pid: 3335, rc: 0, err: Interrupted system call # OpenSSH version is 7.6p1 # ctl_path: /home/alexandre/.libnet-openssh-perl/94db24a6e865065afdff63da68dff381, ctl_dir: /home/alexandre/.libnet-openssh-perl/ # _is_secure_path(dir: /home/alexandre/.libnet-openssh-perl, file mode: 16832, file uid: 1000, euid: 1000 # _is_secure_path(dir: /home/alexandre, file mode: 16877, file uid: 1000, euid: 1000 # set_error(0 - 0) # call args: 'ssh','vvv','-o','ServerAliveInterval=30','-o','ControlPersist=no','-2MNx','-o','NumberOfPasswordPrompts=1','-o','PreferredAuthentications=keyboard-interactive,password','-S','/home/alexandre/.libnet-openssh-perl/94db24a6e865065afdff63da68dff381','-l','admin','-p','22','192.168.109.10','--' # master state jumping from _STATE_START to _STATE_LOGIN # file object not yet found at /home/alexandre/.libnet-openssh-perl/94db24a6e865065afdff63da68dff381, state:_STATE_LOGIN # file object not yet found at /home/alexandre/.libnet-openssh-perl/94db24a6e865065afdff63da68dff381, state:_STATE_LOGIN ssh: Could not resolve hostname vvv: Name or service not known # file object not yet found at /home/alexandre/.libnet-openssh-perl/94db24a6e865065afdff63da68dff381, state:_STATE_LOGIN # set_error(1 - unable to establish master SSH connection: bad password or master process exited unexpectedly) # master state jumping from _STATE_LOGIN to _STATE_GONE Couldn't establish SSH connection: unable to establish master SSH connection: bad password or master process exited unexpectedly at perl.pl line 17. # DESTROY(Net::OpenSSH=HASH(0x166e3a4), pid: <undef>)

        You need to pass the verbose flag to ssh command as -vvv (and not just plain vvv). And next time you report computer output kindly enclose it within code tags <c>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-12-07 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?