perl_n00b_01 has asked for the wisdom of the Perl Monks concerning the following question:
Debugging output is listed below. I notice that the last 5 lines or so, show that my loop print statement execute, but when it tries to execute the $ssh->capture command, the session is DESTROYed and it errors out.sub ssh_to_ip_simple { my $timeout = 10; $Net::OpenSSH::debug = 0; #open command file and execute each line chomp ($user, $password, $host_ip); $ssh = Net::OpenSSH->new($host_ip, user => $user, password => $pas +sword); &parse_commands; foreach (@cmd_array) { print "Attempting to execute command $_ on host $host_ip"; $ssh->capture($_); print @output."\n"; } } sub parse_commands { #Open cmd file specified #Parse file and pass each line to the $ssh->system value if (defined $cmdfile) {} else {(print "\nCommand file not found.\n + Specify command file with -c option\n") && die} print "\nOpening command file $cmdfile\n"; open_cmd {}; for $line_cmd (<CMDFILE>) { chomp ($line_cmd); print "Processing command $line_cmd from file $cmdfile\n\n\n"; #my @output = $ssh->capture({timeout => 10 }, $line_cmd); #$ssh->error and warn "Operation didn't complete successfully: + \n".$ssh->error; push (@cmd_array, $line_cmd); } foreach (@cmd_array) {print "Parse_commands command is ".$_."\n"} close CMDFILE; }
What do I have to do to keep the existing session alive while looping through the commands? Am I doing this correctly or is there an easier way to pipe the command file or array into the function? I have been able to successfully get this to work by calling the connection string during the loop, but based on what I read about the module I thoguht it would support this ability. Any help is greatly appreciated. Thank you. TonyExecuting subroutine ssh_to_ip # ctl_path: /home/tmcurti/.libnet-openssh-perl/cisco-10.250.10.100-442 +1-812531, ctl_dir: /home/tmcurti/.libnet-openssh-perl/ # _is_secure_path(dir: /home/tmcurti/.libnet-openssh-perl, file mode: +16832, file uid: 1000, euid: 1000 # _is_secure_path(dir: /home/tmcurti, file mode: 16877, file uid: 1000 +, euid: 1000 # set_error(0 - 0) # call args: ['ssh','-xMN','-o','NumberOfPasswordPrompts=1','-o','Pref +erredAuthentications=keyboard-interactive,password','-S','/home/tmcur +ti/.libnet-openssh-perl/cisco-10.250.10.100-4421-812531','-o','User=c +isco','--','10.250.10.100'] # passwd requested (Password:) # call args: ['ssh','-O','check','-T','-S','/home/tmcurti/.libnet-open +ssh-perl/cisco-10.250.10.100-4421-812531','-o','User=cisco','--','10. +250.10.100'] # open_ex: ['ssh','-O','check','-T','-S','/home/tmcurti/.libnet-openss +h-perl/cisco-10.250.10.100-4421-812531','-o','User=cisco','--','10.25 +0.10.100'] # io3 mloop, cin: 0, cout: 1, cerr: 0 # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 27 at offset 0 #> 4d 61 73 74 65 72 20 72 75 6e 6e 69 6e 67 20 28 70 69 64 3d 34 34 3 +2 32 29 0d 0a | Master running (pid=4422).. # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 0 at offset 27 # leaving _io3() # _waitpid(4424) => pid: 4424, rc: Opening command file cmd.txt Processing command show ver | inc uptime from file cmd.txt Processing command show run | inc interface.*5 from file cmd.txt Parse_commands command is show ver | inc uptime Parse_commands command is show run | inc interface.*5 Test array value is show ver | inc uptime Test array value is show run | inc interface.*5 # call args: ['ssh','-S','/home/tmcurti/.libnet-openssh-perl/cisco-10. +250.10.100-4421-812531','-o','User=cisco','--','10.250.10.100','show +ver | inc uptime'] # open_ex: ['ssh','-S','/home/tmcurti/.libnet-openssh-perl/cisco-10.25 +0.10.100-4421-812531','-o','User=cisco','--','10.250.10.100','show ve +r | inc uptime'] Attempting to execute command show ver | inc uptime on host 10.250.10. +100# io3 mloop, cin: 0, cout: 1, cerr: 0 # io3 fast, cin: 0, cout: 1, cerr: 0 # stdout, bytes read: 61 at offset 0 #> 20 63 73 2d 6c 61 62 36 35 30 36 20 75 70 74 69 6d 65 20 69 73 20 3 +4 20 77 65 65 6b 73 2c 20 34 | cs-lab6506 uptime is 4 weeks, 4 #> 20 64 61 79 73 2c 20 32 31 20 68 6f 75 72 73 2c 20 33 30 20 6d 69 6 +e 75 74 65 73 0d 0a | days, 21 hours, 30 minutes.. # io3 fast, cin: 0, cout: 1, cerr: 0 Connection to 10.250.10.100 closed by remote host. # stdout, bytes read: 0 at offset 61 # leaving _io3() # _waitpid(4425) => pid: 4425, rc: 0 # set_error(1 - master ssh connection broken) Attempting to execute command show run | inc interface.*5 on host 10.2 +50.10.1000 # DESTROY(Net::OpenSSH=HASH(0x913ed78), pid => 4422) # killing master # sending exit control to master # _kill_master: 4422 # waitpid(master: 4422) => pid: 4422, rc:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Net::OpenSSH Multiple Commands - Session Destroyed
by salva (Canon) on Mar 17, 2011 at 09:28 UTC | |
by Anonymous Monk on Mar 17, 2011 at 16:04 UTC | |
by salva (Canon) on Mar 17, 2011 at 21:18 UTC |