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

hi,
I would run SSH by thread,but with error message `SSHConnectionAborted`.
if I run it in main,it runs well.
Can anybody please help me in this?

use Net::SSH::Expect; use Expect; use thread; my $thr=threads->new(\&job,$console,$username,$password,$timeout); $thr->join(); sub job{ my ($console,$username,$password) = @_ my $consoleprompt = 'yes\/no|assword|[\$#>\]]$|[\$#>\]] $|>.$|].$'; my $ssh = Net::SSH::Expect->new( host => $console, user => $username, password => $password, timeout => $timeout, log_file => "./log/$console", raw_pty => 1 ); my $ssh->run_ssh() or die "SSH process coundn't start:$!"; $ssh->waitfor($consoleprompt,15); my $match=$ssh->match(); if($match =~ /yes\/no/) { print "known_hosts_tables not existing the device"; $ssh->send("yes"); $ssh->waitfor('assword',5); $ssh->send("$password"); } elsif($match =~ /assword/) { $ssh->send("$password"); } $ssh->waitfor($consoleprompt,5); $ssh->send("sup"); ... }

Replies are listed 'Best First'.
Re: SSH by thread
by BrowserUk (Patriarch) on May 14, 2015 at 10:03 UTC

    Based on nothing more than the little you've said in your post -- ie. no first hand experience whatsoever -- it sounds like the library you are using is not thread-safe.

    So, as you say "if I run it in main,it runs well.", why bother trying to run it in a thread?

    That is to say, if you need to do other stuff concurrently, run that other stuff in the thread, and leave this bit to run in main.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      Indeed. Probably lotsa globals here. If you need to run multiple parallel SSH connections, consider using processes, not threads.
Re: SSH by thread
by james28909 (Deacon) on May 14, 2015 at 05:38 UTC
    elsif($match =~ /assword/)
    I hope that is a typo lol ;)

      "/assword/" ... I hope that is a typo lol ;)

      More likely it is a simple shortened /[Pp]assword/ .

      Cheers, Sören

      Créateur des bugs mobiles - let loose once, run everywhere.
      (hooked on the Perl Programming language)

        Dunno about that - OP is also $ssh->waitfor('assword',5);

        I just thought it was kind of funny with the whole 'assword', any words coming from there, you dont want to be around to hear and will likely throw an warning if not handled correctly LOL /sarcasm off