I am new to perl programming also to networking .
My Task is to connect to remote server and read file from server using Perl script.I know how to read file from local machine but not how to read from remote server?
Code to read file from local machine but not know how to connect to remote server and read file?
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
open(FH, 'C:\Users\saqib riaz\Desktop\saqi\properties.txt');
while(<FH>)
{
"$_";
}
close(FH);
I am using window operating system And strawberry with padre ide latest version
The following works for me with Strawberry Perl version 5.30. It uses Net::SSH2 and prints a file file.txt on the server:
use strict;
use warnings;
use Net::SSH2;
my $host = 'my.host.com';
my $user = 'user_name';
my $password = 'xxxxxxx';
my $ssh2 = Net::SSH2->new();
$ssh2->connect($host) or $ssh2->die_with_error;
$ssh2->check_hostkey('ask') or $ssh2->die_with_error;
$ssh2->auth_password($user, $password);
my $chan = $ssh2->channel() or $ssh2->die_with_error;
$chan->exec('cat file.txt') or $ssh2->die_with_error;
print while <$chan>;
$chan->close;
Note: Net::SSH2 comes preinstalled with Strawberry Perl so no need to install it.
Note: I also tried Net::OpenSSH but could not get it to work.
Related
I am using Net:SSH2 to put file on a remote server with scp_put.
It returns unknown error:
-43, LIBSSH2_ERROR_UNKNOWN(-43), SCP failure
It seems that the error comes after some timeout/delay, as it takes several minutes to return.
Connection to sftp-server is working. I can get a directory list from the directory.
I have access rights to that directory as I can put files there with SFTP-client.
I am using Strawberry Perl in Windows environment.
use warnings;
use strict;
use Net::SSH2;
my $dir1 = '.';
my $file = 'D:\\test\\test.txt';
my $ssh2 = Net::SSH2->new();
$ssh2->connect('testserver') or die "Unable to connect Host $# \n";
$ssh2->auth_password('test','test') or die "Unable to login $# \n";
if($ssh2->scp_put($file, $dir1)) {
print "File $file transferred to $dir1\n";
} else {
print "Couldn't transfer file $file to $dir1\n";
print join ', ', $ssh2->error;
print "\n";
}
SCP support in libssh2 is quite rudimentary and buggy.
Better alternatives are Net::SSH::Any which has a proper pure-perl implementation of SCP or Net::SFTP::Foreign for SFTP. Both can work on top of Net::SSH2.
I'm trying to create a script in Perl that does the following
On a Windows 2008 R2 server, connects to a local OpenWRT router.
Send some commands to the router and save the output (interface brief)on a varaible
Edit the content of the variable (to keep the IP only)
Send the variable again to the router withinin another command
I created a socket but i dont seem to get any luck sending commands to the router. I'm not even sure if I'm logging in.
Here is my code:
use IO::Socket;
use strinct;
use warnings;
$iaddr = gethostbyname("192.168.1.237");
$ssh_port = 22;
$sin = sockaddr_in($ssh_port, $iaddr);
socket(DEV, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
connect(DEV, $sin) || die "Can't connect to EN4000: $!\n";
print DEV "user\n";
print DEV "password\n";
print DEV "echo test >> /etc/config/networkTest \n";
I run it, check the file /etc/config/networkTest but no modification is made
Have a look at Net::SSH::W32Perl
Here's a quick example;
#!/usr/bin/env perl
use strict;
use warnings;
use Net::SSH::W32Perl;
my $host='example.com';
my $user='john';
my $pass="pass";
# Connect
my $ssh = Net::SSH::W32Perl->new($host);
$ssh->login($user, $pass);
# Run command
my $cmd = q(echo test >> /etc/config/networkTest);
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
There is a small perl script using Net:SSH2 module
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH2;
my $ssh2 = Net::SSH2->new();
$ssh2->connect('testhost') or die $!;
if ($ssh2->auth_publickey('admin', 'id_rsa.pub', 'id_rsa')) {
print "Connected\n";
} else {
print "failed";
}
my $sess = $ssh2->channel() or die "Unable to create chan\n";
$sess->blocking(1);
$sess->shell();
print $sess "sudo /root/remote.pl\n";
print "$_\n" while <$sess>;
$sess->send_eof;
$sess->close;
After executing this, sometimes I see output of /root/remote.pl and sometimes I don't. I believe that the problem is in output buffering, but I don't how to solve this.
If this script is used on a linux system, why not using perl back-tilt to use linux commands instead of using a module?
Example:
#!/usr/bin/perl
use strict;
use warnings;
# Command
my $cmd = "ssh login:password#host; sudo /root/remote.pl; exit";
# Execute the command
`$cmd`
PS: if the remote.pl script has some print, you should see them.
I am trying to run a Perl script that takes user, pass, ip arguments and uses that to check the version of a network switch through ssh. However when i run it on our server i keep getting:
Math::BigInt: couldn't load specified math lib(s), fallback to Math::BigInt::FastCalc at /usr/lib/perl5/site_perl/5.10.0/Crypt/DH.pm line 6
It the proceeds to hang for a bit then return with no output. What is causing this to fail and how can i get around it? I do not have access to install extra modules to the server.
EDIT: I have checked the currently installed modules and Net::SSH:Perl, Math::BigInt::FastCalc, and Math::Pari are all installed, so i have no idea why it is having problems loading those modules.
Here is my script for reference:
#!/usr/bin/perl
# Outputs the name of the Flash File (which denotes the software version) of the switch
#Input: User Pass Host
open(FH, ">>unparsed.txt");
open (FH2, ">>versions.txt");
use strict;
use Net::SSH::Perl;
my $user = $ARGV[0];
my $pass = $ARGV[1];
my $host = $ARGV[2]; #Hostname given as command line argument
my $cmd = "show version";
my $version;
print("\n");
print($ARGV[2]);
print("\n");
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass); # login to switch
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
printf FH ($stdout); #output all test to file
close(FH);
open(FH, "unparsed.txt");
while(<FH>){ #look through file for flash filename
if($_ =~ /System image file is "(.*)"/){
$version = $1;
}
}
print ($version); #output flash filename
print ("\n");
printf FH2 ($ARGV[2]);
printf FH2 ("\n");
printf FH2 ($version);
printf FH2 ("\n");
close(FH2);
close(FH);
Crypt::DH loads Math::BigInt with:
use Math::BigInt lib => "GMP,Pari";
Therefore, you need either GMP or Pari on your system.
Your distribution's package manager may already provide a means to install them.
Have you tried using Net::SSH?
I prefer Net::SSH, and Net::SFTP::Foreign, math libs usally give me troubles when I try to install them on older systems (specially with the mess that some sysadmin do with paths on Unix systems). Most systems either use OpenSSH, or some fork of it (like SunSSH on Solaris), so it's less likely that you you'll have any sort of trouble using those distributions.
Try using Net::OpenSSH instead of Net::SSH::Perl.
Im trying to run a perl script which involves ssh to a remote server and while executing this code, it throws an error like
Can't locate Net/SSH/Perl.pm in INC <#INC contains:C:/Perl/site/lib c:\perl\lib at line5.
I open ppm graphical UI and installed NET-SSH, area= site
but still not able to execute this script
Here is the script
use strict;
use warnings;
use Net::SSH::Perl;
my $ip=12.14.142.22;
my $user = "qwerty";
my $pass = "termide";
my $ssh = Net::SSH::Perl->new($ip, use_pty => 1);
$ssh->login($user, $pass);
While Foo::Bar often includes a Foo::Bar::Baz module, that isn't the case here.
Net::SSH and Net::SSH::Perl are different distributions. You need to install Net::SSH::Perl.
You should install Net::SSH::Perl, not Net::SSH. The error message is clean about that :)
Net::SSH2 is another SSH client that works on Windows and far easier to install. A PPM is available from here.