#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0

    Newbie problem with Net::SSH::Expect


    Hello,

    I am writing a perl script to login to a router with my username and password. I would then like to execute some commands (show version & show card) , and get the results in a file.

    Unfortunately, my script crashes with the following error : " SSHProcessError The ssh process was terminated." Making some tests and changes I have found that the problem is the variable 'user=> $thm

    If I write my username manually the script is working fine. But if I have the username as an input then the script doesn't work. It is weird because I don't have the issue with the other two variables (crs and sspassword).

    Here is my code
    Code:
    #!/usr/bin/perl    
    use Net::SSH::Expect;    
    open(OUT, ">resultsthomas.txt") || die ("Could not open output file!");    
    my $thm;  
    my $sspassword;  
    my $crs;  my $output;    
    print "\nPlease input your username: "; 
    $thm=<>;  print "\nPlease input your password: ";  $sspassword=<>;  print "\n\nPlease input CRS hostname: ";  $crs=<>;  
    do  {    
    print "\n Please if the info are correct type yes otherwise no\n";       
    $option=<>;  
    } until ($option =~ /yes/ || $option =~ /no/);    
    
    if ($option =~ /yes/) {          
    my $ssh = Net::SSH::Expect->new (host => $crs, password=> $sspassword, user => $thm, raw_pty => 0,timeout => 5);                            
    my $login_output = $ssh->login();                            
    $ssh->send("show version");
    $ssh->send("show card");                              
      						  
    $output = $ssh->read_all();  						  
    print OUT "$output\n";}  
    close (OUT);
    I have tried already to use '$thm' and "$thm" but still no luck.

    Please help me..... Thanks for your help in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,938
    Rep Power
    1225
    You forgot to chomp your input vars.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Thanks FishMonger. That was the issue. It is working.

    But it is very weird because I don't have the same issue with the other two vars.

    Thanks a lot. You saved my weekend
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    $option is used only in a regex, having a trailing line return does not prevent the regex from working correctly. It is not the same with $thm.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    I mean for the $crs, and $sspassword .
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    For them, you don't really know what the Net::SSH::Expect module is doing with these parameters.

    It may be that it chomps the values or uses them in a different way in which the trailing line return does not cause any problem, but it would be more secure for you to also chomp them, so that your script will continue to work even if the implementation of this module changes.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Thanks for your advice Laurent_R.

IMN logo majestic logo threadwatch logo seochat tools logo