Control-CLI

 view release on metacpan or  search on metacpan

examples/usingthreads-ex1.pl  view on Meta::CPAN

sub cliThread { # Thread to each device
	my $host = shift;
	my $tid = threads->tid();
	my ($cli, $output);

	#
	# Create CLI object
	#
	$cli = new Control::CLI(
		Use			=> $connectionType,
	  	Timeout 		=> $timeout,
		Connection_timeout	=> $connectionTimeout,
		Input_log		=> $debug ? $host.'.in' : undef,
		Output_log		=> $debug ? $host.'.out' : undef,
		Dump_log		=> $debug ? $host.'.dump' : undef,
       		Debug			=> $debug,
	);

	#
	# Connect to host
	#
	$cli->connect(
		Host			=>	$Devices{$host},
		Username		=>	$username,
		Password		=>	$password,
	) or die "ERROR => Unable to connect to $host";
	print "$connectionType connected to $host (thread $tid)\n";

	#
	# Login to host
	#
	$cli->login(
		Username		=>	$username,
		Password		=>	$password,
	) or die "ERROR => Unable to login to $host";
	print "Logged into $host (thread $tid)\n";

	#
	# Send commands
	#
	foreach my $cmd (@{$Cmds{$host}}) {
		$output .= $cli->cmd($cmd) or die "ERROR => $host failed command: $cmd";
	}
	print "Sent commands to $host (thread $tid)\n";

	#
	# Disconnect
	#
	$cli->disconnect;

	#
	# Return output if any
	#
	$output = "\nOutput from $host:\n---------------------\n" . $output if length $output;
	return $output;
}


MAIN:{
	#
	# Get credentials if not already set
	#
	$username = promptClear("Please enter username to use for hosts") unless defined $username;
	$password = promptHide("Please enter password to use for hosts") unless defined $password;

	#
	# Start threads
	#
	print "\nStarting threads to connect to hosts:\n";
	foreach my $host (keys %Devices) {
		print " - $host\n";
		threads->create({'scalar' => 1}, 'cliThread', $host);
	}
	print "\n";

	# Wait for threads to complete
	sleep 0.1 while (scalar threads->list(threads::running));
	
	foreach my $thread (threads->list(threads::joinable)) {
		my $output = $thread->join();
		print $output if length $output;
	}
}



( run in 0.524 second using v1.01-cache-2.11-cpan-e1769b4cff6 )