problem making an SSH session using perl - 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

Related

Perl - Best way to establish an SFTP connection on windows

I need to put files in a remote directory using SFTP on a Windows machine. I've tried Net::SFTP::Foreign though I can't use it because it needs IO::Pty which is not available on Windows machine. What's the best/simplest way to do this?
Update With Requested Info:
Here are the versions I'm using:
Net::SFTP::Foreign : v 1.89
Net::SSH2 : v 0.69
Net::SFTP::Foreign::Backend::Net_SSH2 : v 0.09
and here is the gist of my code:
$ssh2 = Net::SSH2->new();
$ssh2->connect($host) || die "connect failed";
$ssh2->auth_password($user, $pass) || die "password auth failed";
$sftp = Net::SFTP::Foreign->new(ssh2 => $ssh2,
backend => 'Net_SSH2');
$sftp->error and
die "Unable to stablish SFTP connection: ". $sftp->error;
Right now I'm just trying to establish a connection. I will need to put files on the server. The erro I'm receiving is as follows:
Net::SSH2::timeout(ss, timeout) at C:/Strawberry/perl/site/lib/Net/SSH2.pm line 111, <STDIN> line 1.
The preferred way to use Net::SFTP::Foreign in Windows is probably to use the Net::SFTP::Foreign::Backend::Net_SSH2 backend (update: a Perl module available from CPAN) which uses Net::SSH2 under the hood (which is already included with Strawberry Perl, update: otherwise, you will need to build and install libssh2 yourself which sometimes is not as easy as it should be).
Another option is to tell Net::SFTP::Foreign to use the plink command to run the SSH connection (search for plink on the module docs). Update:plink is part of the PuTTY application distribution, a very popular SSH client that may be already installed in that machine.
Finally you can also try using Net::SSH::Any which provides its own backend for Net::SFTP::Foreign and can run on top of several SSH clients and modules... but it is still in beta!
I have several cross-platform scripts that are using Net::SFTP::Foreign using plink on windows and openssh on linux and it works great. Windows is using the latest strawberry perl release.
my $sftp = Net::SFTP::Foreign->new(
host=> $server,
ssh_cmd => $plink, #Contains full path to plink or path to ssh
user=> $user,
more => ['-i', $keyfile],
stderr_discard => 1,
);
The only thing about using the plink backend is that you have to first manually establish a connection using psftp or Putty gui so it stores the trust confirmation in the registry. After that it is good to go from the script.
The nice thing is you can just have the actual path to ssh or to plink defined in a system level config file and the script just reads what is needed on that particular platform. ie (...\bin\Putty\plink.exe or /usr/bin/ssh )
Not a pure Perl solution, but has been very robust. I don't see IO::Pty on any of my windows boxes so no dependency there for plink.
SFTP is actually SSH with a wrapper to simulate the FTP like commands, so a mdir is actually a ssh 'ls /path/to/dir'.
While there might be a Perl SFTP package that avoids IO::Pty, you might get to your solution much faster by just translating the "FTP" commands into their ssh / scp equivalents and looking at Net::SSH
--- Adding an Example as requested ---
#!/bin/env perl
use Net::OpenSSH ();
my $connection = Net::OpenSSH->new('somehost.com', user => 'myuser', password => 'mypassword' );
my #ls = $connection->capture("ls");
printf "remote listing is %s\n", join(', ', #ls);
This should list the files to your console.
You mentioned not having IO::Pty because it is not available for a windows machine. Perhaps you should be attempting to install IO::Pty::Easy. Note that Net::OpenSSH uses IO::Pty too, but that should be possible to do on windows, provided you also install Glib as indicated in this post http://www.perlmonks.org/bare/?node_id=856863
Good luck!

Connection Refused While connecting Host Through Net::FTP in 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.

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.

How can I get STDERR and STDOUT of a command executed on a remote machine through SSH in Perl?

I have to connect to a remote machine via SSH executed through Expect module. I have to access the STDERR/STDOUT separately if possible and look at the output. I can redirect the output of the script e.g.
$command = "ssh <script> 2>/tmp/stderr.output"
$exp = Expect->spawn($command) or die "Cannot spawn\n";;
my #command1= $exp->expect(5);
but then I have to remotely connect again to check stderr.output.
Is there a way that expect returns the STDERR/STDOUT separately.
You might want to consider using Net::OpenSSH which provides a capture2 method:
($output, $errput) = $ssh->capture2(\%opts, #cmd)
captures the output sent to both stdout and stderr by #cmd on the remote machine.

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.