Net::SFTP causes script to exit if connection can't be made

2002-12-12 Thread Ian Zapczynski
All,

I just pulled down the latest Net::SFTP from CPAN and am using it w/ 
Perl 5.6.1.  I've used the module before and don't *think* I've seen 
this problem, but I can't say for sure and have only one machine to test 
it on.  What happens is that if an SFTP connection is refused to the 
specified host (i.e. the ssh server is down), my script below dies and 
does not print the failure message I've specified.

Is this a fault of my environment?  Of the module?  Is there a way that 
I can prevent this from happening?  If I can't connect to the SFTP 
server, I cannot allow my script to just die -- I need to send an SNMP 
message to inform someone of the problem.

As always, insight is mucho appreciato.  


#/usr/bin/perl -w

use strict;
use Net::SFTP;

my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
password=>"suite100");
if (!$sftp) {
   print "I can't connect!";
} else {
   print "SUCCESS!";
}





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Net::SFTP causes script to exit if connection can't be made

2002-12-12 Thread Mark Goland
how about 

 #/usr/bin/perl -w
> 
> use strict;
> use Net::SFTP;
> 
 my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
 password=>"suite100") || warn "connection failed $!";
> if (!$sftp) {
> print "I can't connect!";
> } else {
> print "SUCCESS!";
> }
> 


- Original Message - 
From: "Ian Zapczynski" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 12, 2002 4:51 PM
Subject: Net::SFTP causes script to exit if connection can't be made


> All,
> 
> I just pulled down the latest Net::SFTP from CPAN and am using it w/ 
> Perl 5.6.1.  I've used the module before and don't *think* I've seen 
> this problem, but I can't say for sure and have only one machine to test 
> it on.  What happens is that if an SFTP connection is refused to the 
> specified host (i.e. the ssh server is down), my script below dies and 
> does not print the failure message I've specified.
> 
> Is this a fault of my environment?  Of the module?  Is there a way that 
> I can prevent this from happening?  If I can't connect to the SFTP 
> server, I cannot allow my script to just die -- I need to send an SNMP 
> message to inform someone of the problem.
> 
> As always, insight is mucho appreciato.  
> 
> 
> #/usr/bin/perl -w
> 
> use strict;
> use Net::SFTP;
> 
> my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
> password=>"suite100");
> if (!$sftp) {
> print "I can't connect!";
> } else {
> print "SUCCESS!";
> }
> 
> 
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Net::SFTP causes script to exit if connection can't be made

2002-12-13 Thread Ian Zapczynski
Unfortunately, using the || as below doesn't change the behavior.  Once 
my script tries to make the connection and can't, it exists with a 
"connection failed to $host, etc. etc." message, whether I use warnings 
or diagnostics or neither.  If anyone can help me understand why this 
may be or if they experience the same, this would be terribly helpful. 
As I mentioned, I need to be able to trap this error and continue 
processing if a connection fails.

Thanks


Mark Goland wrote:

how about 

#/usr/bin/perl -w
 

use strict;
use Net::SFTP;

   

my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
password=>"suite100") || warn "connection failed $!";
 

if (!$sftp) {
   print "I can't connect!";
} else {
   print "SUCCESS!";
}

   



- Original Message - 
From: "Ian Zapczynski" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 12, 2002 4:51 PM
Subject: Net::SFTP causes script to exit if connection can't be made


 

All,

I just pulled down the latest Net::SFTP from CPAN and am using it w/ 
Perl 5.6.1.  I've used the module before and don't *think* I've seen 
this problem, but I can't say for sure and have only one machine to test 
it on.  What happens is that if an SFTP connection is refused to the 
specified host (i.e. the ssh server is down), my script below dies and 
does not print the failure message I've specified.

Is this a fault of my environment?  Of the module?  Is there a way that 
I can prevent this from happening?  If I can't connect to the SFTP 
server, I cannot allow my script to just die -- I need to send an SNMP 
message to inform someone of the problem.

As always, insight is mucho appreciato.  


#/usr/bin/perl -w

use strict;
use Net::SFTP;

my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
password=>"suite100");
if (!$sftp) {
   print "I can't connect!";
} else {
   print "SUCCESS!";
}





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

   





RESOLVED: Net::SFTP causes script to exit if connection can't be made

2002-12-13 Thread Ian Zapczynski
Sorry about the multiple messages on this.

I was able to resolve my problem with Net::SSH::Perl die()ing if a 
connection fails by using the following:

eval {
   $sftp = Net::SFTP->new($host, user=>$host, password=>$pass);
};

then I check if $sftp is defined to decide whether or not to send an 
SNMP trap.

Phew.


Mark Goland wrote:

how about 

#/usr/bin/perl -w
 

use strict;
use Net::SFTP;

   

my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
password=>"suite100") || warn "connection failed $!";
 

if (!$sftp) {
   print "I can't connect!";
} else {
   print "SUCCESS!";
}

   



- Original Message - 
From: "Ian Zapczynski" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 12, 2002 4:51 PM
Subject: Net::SFTP causes script to exit if connection can't be made


 

All,

I just pulled down the latest Net::SFTP from CPAN and am using it w/ 
Perl 5.6.1.  I've used the module before and don't *think* I've seen 
this problem, but I can't say for sure and have only one machine to test 
it on.  What happens is that if an SFTP connection is refused to the 
specified host (i.e. the ssh server is down), my script below dies and 
does not print the failure message I've specified.

Is this a fault of my environment?  Of the module?  Is there a way that 
I can prevent this from happening?  If I can't connect to the SFTP 
server, I cannot allow my script to just die -- I need to send an SNMP 
message to inform someone of the problem.

As always, insight is mucho appreciato.  


#/usr/bin/perl -w

use strict;
use Net::SFTP;

my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
password=>"suite100");
if (!$sftp) {
   print "I can't connect!";
} else {
   print "SUCCESS!";
}





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

   





Re: Net::SFTP causes script to exit if connection can't be made

2002-12-14 Thread zentara
On Fri, 13 Dec 2002 11:10:54 -0500, [EMAIL PROTECTED] (Ian
Zapczynski) wrote:

>Unfortunately, using the || as below doesn't change the behavior.  Once 
>my script tries to make the connection and can't, it exists with a 
>"connection failed to $host, etc. etc." message, whether I use warnings 
>or diagnostics or neither.  If anyone can help me understand why this 
>may be or if they experience the same, this would be terribly helpful. 
> As I mentioned, I need to be able to trap this error and continue 
>processing if a connection fails.

Wrap it in an eval statement.

##3
#!/usr/bin/perl -w
use strict;
use Net::SFTP;

my $sftp = undef;
eval{
$sftp = Net::SFTP->new("localhost", 
 user=>"zzztester",
 password=>"zzztest",
);
};
if ($@) { print "Sftp connection failed:\n  $@\n"; }

if (! $sftp) {
  print "I can't connect!\n";
}else{
  print "SUCCESS!\n";
}




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Net::SFTP causes script to exit if connection can't be made

2002-12-16 Thread Dave Storrs
Zentara is right about what you need to do, but a fuller explanation
of the solution would have been:

Net::SFTP is throwing an exception when it cannot make the
connection.  If the exception is not caught, it will cause your
program to exit.  The way you catch exceptions in Perl is to wrap the
code that throws them in an eval block.  After the eval block, the
exception will be stuffed into the $@ variable, so you can examine
that variable to see if there is some kind of useful information there
about what the exact problem was.  

--Dks


On Sat, Dec 14, 2002 at 09:50:33AM -0500, zentara wrote:
> On Fri, 13 Dec 2002 11:10:54 -0500, [EMAIL PROTECTED] (Ian
> Zapczynski) wrote:
> 
> >Unfortunately, using the || as below doesn't change the behavior.  Once 
> >my script tries to make the connection and can't, it exists with a 
> >"connection failed to $host, etc. etc." message, whether I use warnings 
> >or diagnostics or neither.  If anyone can help me understand why this 
> >may be or if they experience the same, this would be terribly helpful. 
> > As I mentioned, I need to be able to trap this error and continue 
> >processing if a connection fails.
> 
> Wrap it in an eval statement.
> 
> ##3
> #!/usr/bin/perl -w
> use strict;
> use Net::SFTP;
> 
> my $sftp = undef;
> eval{
> $sftp = Net::SFTP->new("localhost", 
>  user=>"zzztester",
>  password=>"zzztest",
>   );
> };
> if ($@) { print "Sftp connection failed:\n  $@\n"; }
> 
> if (! $sftp) {
>   print "I can't connect!\n";
> }else{
>   print "SUCCESS!\n";
> }
> 
> 
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: More on Net::SFTP causes script to exit if connection can't be made

2002-12-13 Thread Ian Zapczynski
Ok, so I found that my problem appears to come from the following 
portion of Net::SSH ::Perl:

  connect($sock, sockaddr_in($rport, $raddr))
   or die "Can't connect to $ssh->{host}, port $rport: $!";

Why should it die() and not just warn()?

I found something that suggests I can intercept die() as a signal, so I 
added the following at the top of my script:

$SIG{__DIE__} = sub {
   warn $@;
};

When the die() is called, it does enter this subroutine and attempt the 
warn(), but the script still exits. (!)  Am I not handling this properly?  

I now receive:

Warning: something's wrong at supplierftp.pl line 22 (#2)
   (W) You passed warn() an empty string (the equivalent of warn "") or
   you called it with no args and $_ was empty.
  
Can't connect to 10.25.3.150, port 22: Connection refused at 
/usr/local/lib/perl5/site_perl/5.6.1/Net/SSH/Perl.pm line 206.

Is there just no way to avoid a die() if called?  

Thanks again,

-Ian

Mark Goland wrote:

how about 

#/usr/bin/perl -w
 

use strict;
use Net::SFTP;

   

my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
password=>"suite100") || warn "connection failed $!";
 

if (!$sftp) {
   print "I can't connect!";
} else {
   print "SUCCESS!";
}

   



- Original Message - 
From: "Ian Zapczynski" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 12, 2002 4:51 PM
Subject: Net::SFTP causes script to exit if connection can't be made


 

All,

I just pulled down the latest Net::SFTP from CPAN and am using it w/ 
Perl 5.6.1.  I've used the module before and don't *think* I've seen 
this problem, but I can't say for sure and have only one machine to test 
it on.  What happens is that if an SFTP connection is refused to the 
specified host (i.e. the ssh server is down), my script below dies and 
does not print the failure message I've specified.

Is this a fault of my environment?  Of the module?  Is there a way that 
I can prevent this from happening?  If I can't connect to the SFTP 
server, I cannot allow my script to just die -- I need to send an SNMP 
message to inform someone of the problem.

As always, insight is mucho appreciato.  


#/usr/bin/perl -w

use strict;
use Net::SFTP;

my ($sftp) = Net::SFTP->new("10.25.3.150", user=>"administrator", 
password=>"suite100");
if (!$sftp) {
   print "I can't connect!";
} else {
   print "SUCCESS!";
}





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]