use Net::SSH2::Cisco; use Getopt::EvaP; use Parallel::ForkManager; use Data::Dumper; my $max_processes = 1; my $tftp_config_directory = "switches/conf/"; my $config_directory = "/srv/tftp/"; my $tftp_server = "192.168.0.206"; my $lp="********"; my $user = "cworker"; my @wd; my $nexus = 'cisco(9372|3048|6001|1000v)'; my $pm = new Parallel::ForkManager($max_processes); while (<>) { @wd = split /,/; my $pid = $pm->start and next; &talkto(@wd); $date = scalar localtime; sleep 1; $pm->finish; } $pm->wait_all_children; sub talkto { local($host,$model) = @_; my $error; my $configfile = $tftp_config_directory . $host . "-confg"; my $sok = Net::SSH2::Cisco->new( host => $host, Dump_Log => '/tmp/backup.log', Output_log => '/tmp/output.log', Input_log => '/tmp/input.log', Waitfor_clear => '0', #as a 0 it doesn't prepend Ctrl-Z to the command. ); $sok->login(Name =>$user, Password =>$lp, ); my @cmdoutput; my $vrf = 'default'; if ($model =~ /$nexus/) { my $output; @cmdoutput = $sok->cmd( String => "copy startup-config tftp://$tftp_server/$configfile vrf $vrf", Errmode => "return", ); print Dumper @cmdoutput; if ($sok->errmsg){ ($error) = $sok->errmsg; print "Nexus IP: $host: Command failed reason: $error. Trying again...\n" ; @cmdoutput = $sok->cmd( String => "copy startup-config tftp://$tftp_server/$configfile vrf management", Errmode => "return", ); $sok->waitfor ( String => '#', Timeout => '5', Waitfor_clear => '1', ); print "Output: $output\n"; print Dumper @cmdoutput; print "Nexus IP: $host: Command failed reason: $error\n" ; } } else { @cmdoutput = $sok->cmd( String => "copy running-config tftp $tftp_server $configfile", ); if ($sok->errmsg){ ($error) = $sok->errmsg; print "Catalyst IP: $host Command failed reason: $error"; } } $sok->close; }