Hello,

I'm having problems with the following code. It is supposed to get a
list of IP addresses from the IAD module and then create a few threads
to each log into those devices and make changes in the config. The
only device that is allowed to access the devices I'm configuring is
the 'manage.example.com'. From there, the threads will telnet to the
devices and make whatever changes need to be made.

---start---
#!/usr/local/bin/perl

# be safe, not stupid
use strict;
use warnings;

# include the code to generate the list of IPs
use IAD;

# check to ensure that we're threadable
use Config;
$Config{useithreads} or die('Recompile Perl with threads to run this program.');

# include the thread stuff
use threads;
use Thread::Queue;
use Thread::Semaphore;

# include the ssh stuff
use Net::SSH::Expect;
use Term::ReadPassword;

# get the username/password
my $exe_user = get_exe_user();
my $exe_pass = get_exe_pass();

# set up changes to be made
my $TheChange = get_input();

# set up param list
my @paramlist = ($TheChange, $exe_user, $exe_pass);

my $semaphore = Thread::Semaphore->new(3); # can only make three
connections to the management server at a time
my $DataQueue = Thread::Queue->new();
my $thread1 = threads->create(\&threadsub, @paramlist);
my $thread2 = threads->create(\&threadsub, @paramlist);
my $thread3 = threads->create(\&threadsub, @paramlist);

my $iad = IAD->new();
$DataQueue->enqueue(@{$iad->iads()});# $iad->iads() returns a
reference to a list of IP addresses to manage

$DataQueue->enqueue("DIE", "DIE", "DIE");

$thread1->join();
$thread2->join();
$thread3->join();
exit;

sub threadsub {
        #threads->detach();
        my $code = shift;
        my $exe_user = shift;
        my $exe_pass = shift;
        my $threadNumber = threads->tid();

        # create the ssh object
        my $ssh = Net::SSH::Expect->new (
                host => 'manage.example.com',
                user => $exe_user,
                password => $exe_pass,
                raw_pty => 1
        );

        # login to management server
        my $login_output = $ssh->login(); ######## this is line 65
        if ($login_output !~ /home]$/) {
                die "Login has failed. Login output was $login_output";
        }

        while (my $DataElement = $DataQueue->dequeue()) {
                last if ($DataElement eq "DIE");
                $semaphore->down();
                print("Thread $threadNumber got $DataElement\n");
 #               $ssh->exec("telnet $DataElement", 10);
 #               print $ssh->exec($code);
 #               $ssh->exec("exit");
                #sleep(1);
                $semaphore->up();
        }
        return;# "Thread $threadNumber is dying!\n";
}

sub get_input {
        print "Please enter commands below (end with a . on a line by
itself)\n";
        my $retval = '';
        while (defined(my $line = <STDIN>)) {
                last if ($line eq ".\n");
                $retval .= $line;
        }
        return $retval;
}

sub get_exe_user {
        print "Please enter management username: ";
        my $user = <>;
        chomp($user);
        return $user;
}

sub get_exe_pass {
        # get password from user
        my $password = read_password('Password: ');
        return $password;
}
---end---

However, When I try connecting, I get the following example output.
This is my first time writing threaded programs, so if I'm doing
anything wrong or inefficiently, please let me know since this program
will make a lot of connections once it's up and running. Thanks for
any help you can provide!

---start-output---
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied (publickey,password).
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied, please try again.
ssh_askpass: exec(/usr/local/bin/ssh-askpass): No such file or directory
Permission denied (publickey,password).
Permission denied (publickey,password).
thread failed to start: SSHConnectionAborted at ./test_threads.pl line 65
thread failed to start: SSHConnectionAborted at ./test_threads.pl line 65
thread failed to start: SSHConnectionAborted at ./test_threads.pl line 65
---end-output---

-- 
-- 
I'm nerdy in the extreme and whiter than sour cream

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to