ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am very new to perl, and what I am trying to do in my script, is to copy, the file, to remote hosts, without prompting a password. I found some interesting cpan library NET::SCP::Expect which allows that kind of things.. On all servers, I have the same password for user "nagios"
First of all, I will show you the code, which is working perfectly but only if host is specified in script:
Code:
#!/bin/perl
use Net::SCP::Expect;
use warnings;
use strict;
my $scpe = Net::SCP::Expect->new(preserve=>'1', host=>"host-smtp-01", user=>'nagios', password=>'mysecretpass');
$scpe->scp('f12.txt','/usr/local/nagios/')
Now, I am trying to made a loop, and copy the file to multiply servers (which are specified in file /tmp/hosts1). Below the code:
Code:
#!/usr/bin/perl
use Net::SCP::Expect;
use warnings;
use strict;
open(my $fh, '<', '/tmp/hosts1') or die $!;
while (my $host = <$fh>){
my $scpe = Net::SCP::Expect->new(preserve=>'1', host=>"$host", user=>'nagios', password=>'mysecretpass');
$scpe->scp('f12.txt','/usr/local/nagios/');
}
close($fh) or die $!;
Unfortunately I get some wired error, that host is not added to known_hosts file.. Why? It's strange, because I am able to connect to all of hosts, and they are in known_hosts file..
Code:
[root@host-ma-01 perl]# perl scp-without-key.pl
Problem performing scp: The authenticity of host '10.230.17.50
(10.230.17.50)' can't be established.
RSA key fingerprint is 62:a1:9c:6a:53:2d:c7:bc:9a:8e:5b:d6:8a:c7:c6:dc.
Are you sure you want to continue connecting (yes/no)? at scp-without-key.pl line 17.
[root@host-ma-01 perl]# cat /tmp/hosts1
10.230.17.50
10.230.17.40
[root@host-ma-01 perl]# grep 10.230.17.50 /root/.ssh/known_hosts
host-smtp-01,10.230.17.50 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZYdIWh6ebv0Pn2F5Byk8F3tcyqmFs4OmiWNLbX1ihvXmJaTQWAvYVdGKf7gwQX3SkLCe833oG6sKcraM+Y3HNZ7xalKKuYNlzOpPhbr0eH13SDgD2oel53DWg8GcAGjVDpOywRKsS/OqVuDqy8//C49YXSB+MRbTPH1Hxtz1tYczv0e0V0co5uJYjCkqiQDSIFsbhAjmbZSDAUVvg2QciPO7bSOso6IWHRqI8AnTLmGteDBE7MOiwRekjEyB5tqZwUnyudC0+AWtPkecAf9h0GSRL08f/w4piXx7BB47qpllNEQzJ7SXUM9Um+V9Wn8nz6m5kx2iBG535eNfBbWIp
[root@host-ma-01 perl]#
[root@host-ma-01 perl]# ssh 10.230.17.50 -l nagios
nagios@10.230.17.50's password:
Last login: Thu Mar 7 17:12:16 2013 from 209.202.132.8
nagios@host-smtp-01:~$
What I miss on the script? I will be thankfull for any help in this issue.
This massage is seen on random hosts.. So maybe sth is wrong with that library?
Code:
Problem performing scp: Executing: program /usr/bin/ssh host 10.230.20.25
, user nagios, command scp -v -p -t /usr/local/nagios/libexec/
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
The authenticity of host '10.230.20.25
(10.230.20.25)' can't be established.
RSA key fingerprint is 5f:77:03:5d:06:6d:bd:7b:41:87:b8:8d:85:7a:dd:0d.
Are you sure you want to continue connecting (yes/no)? at scp-without-key.pl line 12.
Problem solved, but adding to /root/.ssh/config, one option:
Code:
Host *
StrictHostKeyChecking no
Next, there were some scp errors (exit with code 256) - problem was with permission on remote hosts..
And the last one: when scp hangs and exit, that was connection issue, so I add "timeout=>'25'," what fix the problem.
Finally the scp code looks like that:
Code:
#!/usr/bin/perl
use Net::SCP::Expect;
use warnings;
use strict;
open(my $fh, '<', '/tmp/hosts-debian') or die $!;
while (my $host = <$fh>){
my $scpe = Net::SCP::Expect->new(timeout=>'25', auto_quote=>'0', preserve=>'1', verbose=>'1', auto_yes=>'1', host=>"$host", user=>'MYUSERNAME', password=>'MYSECRETPASSWD');
$scpe->scp('MYFILE.TXT', '/REMOTE/DIR/LOCATION');
}
close($fh) or die $!;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.