Connection Refused While connecting Host Through Net::FTP in perl - perl

When I run below code through perl script at that time I always get error like connection refused so please any body can suggest to solve this issue.
ftp_check.pl:
#!/usr/bin/perl
use strict;
use warnings;
use Net::FTP;
my $host = '111.118.248.24';
#-- connect to ftp server
my $ftp = Net::FTP->new($host, Debug => 1) or die "Error connecting to $host: $!";
Message getting like "Error connecting to 111.118.248.24: Connection refused at ftp_check.pl line 10."

i supposed you server works properly, just for the case you can make some pings on it. Anyway, do you have a possibility to run in a terminal ( with ssh or some client like putty if you are a windows user ) following commands:
netstat -tulpena | grep -i LIST <<ENTER>>
Now you can see is there a FTP-Server which is running in a listener mode.
The second step would be to to use some ftp-client to check, that you can connect with your FTP-Server properly, just to see is there a firewall-rule in between, actually you could run "iptables -L" on a server to, but it will be easier just to use some ftp-client to make a verification of your ftp-server.
An other suggestion would be to use something more secure
and not the net::ftp-module. Perl has nice CPAN-Modules for SFTP (Net::FTP).
Cheers.

Related

perl ssh to remote server, start service and capture pid

title: perl ssh to remote server, start service and capture pid
1-please tell me if i am not clear, or i if am otherwise frustrating as i ask questions - i do not want to bite the hand that feeds me!
2-original file is 180 lines long, but here is the gist:
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SSH::Perl;
use Net::SSH::Expect;
use Time::HiRes qw(usleep nanosleep);
Time::HiRes::sleep(5); #5 seconds #a look at "top" verifies this running .pl file pid.
my $remoteCmd = `ssh $remote_host -l $userID -i /home/$userID/.ssh/authorized_keys /sbin/service /etc/init.d/c3-mi-nodeserver start`;
my $servicePID = $$; #this should be pid for .pl file running.
print "servicePID: " . $servicePID . "\n"; #prints the pid of the running .pl file.
of course, you'll see variables that i populate to make it work.
one idea i have is: if i start a service, it will be the pid # of the currently running .pl file + 1; but, the new service started is on a remote server, so how can i capture it from the remote server and return it back to the local .pl file?
ideas?
With that ssh command there's no way to capture the PID of the process you just started on a remote host.
You would need to use another ssh to find the process id. But really - what are you trying to accomplish by doing so? Can you not use service status and service stop to manipulate it?
If you really need a pid - service status might do it - or running a ps command.

Connection error using Net::IMAPClient in Perl

I am trying to connect to an Outlook email server via IMAP and the error I am getting is curious. Here is a sample of my code:
use Mail::IMAPClient;
my $imap = Mail::IMAPClient->new;
$host='outlook.office365.com';
$username='.....';
$password='.....';
$folder='INBOX';
$imap=Mail::IMAPClient->new(
Server => $host,
User => $username,
Password => $password,
Port => 993,
Ssl => 1,
Clear=> 5,
Folder => $folder,
Uid => 0,
) or die "Cannot connect to $host as $username: $#";
When I run this, the output line looks like this:
Cannot connect to outlook.office365.com as [...]: Socket closed while reading data from server.
More specifically, Socket closed while reading data from server is what is confusing me.
I specify port 993, so is the only remaining possible issue that there's a firewall in place that is preventing this? I have emailed my school's (this is a school email account) tech department (quite some time ago) and they have yet to get back, but hopefully I will hear from them soon.
I get an even stranger error when I remove the line specifying the port, I am including it only in the hopes it is somehow relevant or helpful:
Cannot connect to outlook.office365.com as [...]: Error sending '1 Login "baldassaren#wit.edu" {15}
[password is shown here, along with a newline character I can't seem to force here]
' to IMAP: Bad file descriptor at ./test.pl line 10.
Please check directly with openssl:
openssl s_client -connect outlook.office365.com:993
This should give you a connection and at the end the welcome message from the IMAP server:
* OK The Microsoft Exchange IMAP4 service is ready. ....
If this does not work the connection is blocked by firewall or so. If this works try connecting with IO::Socket::SSL, which is the module Mail::IMAPClient uses for SSL connections:
perl -MIO::Socket::SSL -e 'print IO::Socket::SSL->new(q[outlook.office365.com:993])->getline.""'
This should also give you the welcome message. If it does not there might be problems with certificate checking or so. In this case please post the versions of modules you use and the OS, e.g.
perl -e 'print "version=$^V, os=$^O\n"'
perl -MIO::Socket::SSL -e 'print IO::Socket::SSL->VERSION,"\n"'
perl -MMail::IMAPCient -e 'print Mail::IMAPClient->VERSION,"\n"'
But, if IO::Socket::SSL gets a successful connection please add the Debug => 1 option to Mail::IMAPClient->new and add the output to your question.

[Perl][net::ssh2] How to keep the ssh connection while executing remote command

I'm working on a perl script using net::ssh2 to make a SSH connection to a remote server.
(I'm working on windows)
I chose Net::SSH2 because i had to make some SFTP connections in the same script.
For now, my sftp connections work perfectly. The problem is when i try to execute a "long-duration" command. I mean a command which execution can take more than 30sec.
$ssh2 = Net::SSH2->new();
$ssh2->connect('HOST') or die;
if($ssh2->auth(username=>'USER', password=>'PSWD'))
{
$sftp = Net::SFTP::Foreign->new(ssh2=>$ssh2, backend=>'Net_SSH2');
$sftp->put('local_path', 'remote_path');
$channel=$ssh2->channel();
##
$channel->shell('BCP_COMMAND_OR_OTHER_PERL_SCRIPT');
# OR (I tried both, both failed :( )
$channel->exec('BCP_COMMAND_OR_OTHER_PERL_SCRIPT');
##
$channel->wait_closed();
$channel->close();
print "End of command";
$sftp_disconnect();
}
$ssh2->disconnect();
When i execute this script, the connection is successfull, the file is correctly sent but the execution is not (completely) performed. I mean, I think the command is sent for execution but terminated immediatly or not sent at all, i'm not sure.
What i want is the script waits until the command is completly finished before disconnect everything (just because sometimes, i need to get the result of the command execution)
Does anyone know how to solve this? :( The cpan documentation is not very explicit for this
Thanks!
PS: I'm open to any remarks or suggestion :)
Edit: After some test, i can say that the command is sent but is interrupted. My test was to start another perl script on the remote server. This script writes in a flat file. In this test, the script is started, the file is half-filled. I mean, the file is brutaly stopped in the middle.
In the other hand, when i performed a "sleep(10)" just after the "$channel->exec()", the script goes to the end successfully.
Problem is, that I can't write a "sleep(10)" (i don't know if it will take 9 or 11 seconds (or more, you see my point)
You can try using Net::SSH::Any instead.
It provides a higher level and easier to use API and can use Net::SSH2 or Net::OpenSSH to handle the SSH connection.
For instance:
use Net::SSH::Any;
my $ssh = Net::SSH::Any->new($host, user => $user, password => $password);
$ssh->error and die $ssh->error;
my $sftp = $ssh->sftp;
$sftp->put('local_path', 'remote_path');
my $output = $ssh->capture($cmd);
print "command $cmd output:\n$output\n\n";
$sftp->put('local_path1', 'remote_path1');
# no need to explicitly disconnect, connections will be closed when
# both $sftp and $ssh go out of scope.
Note that SFTP support (via Net::SFTP::Foreign) has been added on version 0.03 that I have just uploaded to CPAN.

Whilst using Perl::FTP, What shall I put in the host field for connecting to a Local computer connected on the network

I am using the Net::FTP in perl to do some file transfers. when I run the following code :-
Are there any issues with using the IP address ? Am I correct in providing this in the host field ?
use strict;
use Net::FTP;
my $host = "10.77.69.124";
my $user = "administrator";
my $password = "Password";
my $f = Net::FTP->new($host, Debug =>0) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";
The code is not able to connect to the remote host. Why is this happening ? Shouldn't this work with the IP address provided in the $host ?
The constructor of Net::FTP allows you to pass a single scalar value or an array of hosts to try. The value of this field should be the same as the PeerAddr from IO::Socket::INET (either a hostname or an ip address).
Have a closer look at what is happening by specifying Debug. If you are behind a firewall or a NAT setup, you should probably also set Passive to a non-zero value and make sure to check if the constructor failed by printing out $#.
my $ftp = Net::FTP->new(Host=>$host, Debug=>1, Passive=>1) || die $#;
If the constructor succeeded, you might want to check if any of the other methods fail:
$ftp->login($user, $pass) || die $ftp->message;
$ftp->cwd($path) || die $ftp->message;
By the way: If you are unsure if you've used the correct host parameter, you can ask Net::FTP which host it tried to connect to:
print $ftp->host, "\n";
If this still doesn't work, please provide a detailed output of your application.
Hope this helps.
First be sure that you can reach the remote side:
From command line use telnet (available on linux and windows too, a it different in syntax)
telnet host 21
If you are not able to connect the from commandline, check for firewall rules or maybe your FTP server running on different port?
If you are able to connect try out login with plain FTP commands:
USER user#remote.host
PASS yourpassword
This will use ACTIVE ftp connection to the remote. This is the old way.
Nowadays most ftp server use PASSIVE ftp. To test try this command out (from linux commandline)
ftp -v -p host
In perl you could use passive mode this way:
my $f = Net::FTP->new($host, Debug =>1, Passive => 1) or die "Can't open $host\n";
I hope this will help you.

problem making an SSH session using perl

use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new('$host',22);
$ssh->login('$user','$pass');
my $out = $ssh->cmd("show clock");
print $out;
I have the above script to have an ssh session using perl but I'm having the error message
"Can't map service name 'ssh' to port number". I'm using Windows OS. Please advise me where I'm wrong.
Try adding ssh to your services file. The services file is located at:
%SystemRoot%\system32\drivers\etc\services
The line that you'll want to add will look like:
ssh 22/tcp # Secure Shell Login