Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:02:19+00:00 2026-06-14T21:02:19+00:00

I use expect in perl to do ssh, following is my code snipet. my

  • 0

I use expect in perl to do ssh, following is my code snipet.

my $exp = Expect->spawn("ssh renjithp\@192.168.1.12") or die "could not spawn ssh";
my $op = $exp->expect(undef,'renjithp>');
print "*$op*";

I wanted to handle the error if the host is not reachable (destination IP is down), I was in a impression die will hit when the ip is not reachable, but when i give a wrong IP script is not terminating and it continuous execution.

what is the right way to handle this scenario?

EDITED
I am observing $op value is 1 when the ssh is succesfull, and 0 when the destination IP is not up. is it right way to use $op to take a decision?

I have one more doubt, when the destination IP is not reachable why the control coming out of expect,i mean ‘$exp->expect(undef,’renjithp>’);’ should return only after its getting the prompt right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:02:20+00:00Added an answer on June 14, 2026 at 9:02 pm

    If you need to make the ssh connection contingent on IP address reachability with the expect module, you should test connectivity with a nc first…

    my $addr = "192.168.1.12";
    if (system("nc -w 1 -z $addr 22")==0) {
        my $exp = Expect->spawn("ssh renjithp\@$addr") or die "could not spawn ssh";
        my $op = $exp->expect(undef,'renjithp>');
        print "*$op*";
    } else {
        print "Host $addr is unreachable\n";
    }
    

    The nc command is netcat… nc -z tests for TCP port open

    Alternatively, you could use a module like Net::OpenSSH that makes error handling a bit easier…

    use Net::OpenSSH;
    
    my $ssh = Net::OpenSSH->new($host);
    $ssh->error and
       die "Couldn't establish SSH connection: ". $ssh->error;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not getting the result expect from the following code: #!/bash/perl use strict; use
I want to use Net::SSH::Expect in my unix box but unfortunately, it is not
The following Perl script outputs SUCCESS as you'd expect: use Fcntl qw(:DEFAULT :flock); sysopen(LF,
In the following Perl code, I would expect to be referencing an array reference
I've wrapped Perl's Net::SSH::Expect with a small module to reduce the boilerplate code needed
the following code #!/usr/bin/env perl use strict; use warnings; my @foo = (0,1,2,3,4); foreach
I have the following code: #!/usr/bin/perl # splits.pl use strict; use warnings; use diagnostics;
I have the following small Perl (v5.10) program: use strict; my @nums; my $i
I want to use the linux expect scripts in windows. I tried with perl
I tried to use google test framework and can't see why the following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.