LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2013, 07:30 PM   #1
grzeslaw
Member
 
Registered: Nov 2008
Posts: 111

Rep: Reputation: 24
Arrow Problem with Net::SCP::Expect in pell script


Hello members!

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.

Regards,
 
Old 03-08-2013, 07:18 AM   #2
grzeslaw
Member
 
Registered: Nov 2008
Posts: 111

Original Poster
Rep: Reputation: 24
Solved:

I add one option to the Net::SCP::Expect options, and it starts working:
Code:
auto_yes=>'1'
So full line looks like that:
Code:
my $scpe = Net::SCP::Expect->new(preserve=>'1',auto_yes=>'1', host=>"$host", user=>'nagios', password=>'mysecretpass');
 
Old 03-08-2013, 04:58 PM   #3
grzeslaw
Member
 
Registered: Nov 2008
Posts: 111

Original Poster
Rep: Reputation: 24
Exclamation

No.. unfurtunately it is not solved :/

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.

OS which I run the script: CentOS 6.3

Perl system libraries:
Code:
perl-Class-MethodMaker-2.16-4.el6.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
perl-ExtUtils-MakeMaker-6.55-127.el6.x86_64
perl-IO-Compress-Zlib-2.020-127.el6.x86_64
perl-Pod-Simple-3.13-127.el6.x86_64
perl-SNMP_Session-1.12-4.el6.noarch
perl-IO-Socket-SSL-1.31-2.el6.noarch
perl-Error-0.17015-4.el6.noarch
perl-XML-Parser-2.36-7.el6.x86_64
perl-Math-BigInt-GMP-1.24-1.el6.rf.x86_64
perl-ExtUtils-ParseXS-2.2003.0-127.el6.x86_64
perl-Compress-Zlib-2.020-127.el6.x86_64
perl-Test-Simple-0.92-127.el6.x86_64
perl-Archive-Zip-1.30-2.el6.noarch
perl-HTML-Tagset-3.20-4.el6.noarch
perl-libs-5.10.1-127.el6.x86_64
perl-5.10.1-127.el6.x86_64
perl-perl5lib-1.02-1.el6.rf.noarch
perl-Git-1.7.1-2.el6_0.1.noarch
perl-CPAN-1.9402-127.el6.x86_64
perl-Test-Harness-3.17-127.el6.x86_64
perl-Socket6-0.23-3.el6.x86_64
perl-IO-Socket-INET6-2.56-4.el6.noarch
perl-URI-1.40-2.el6.noarch
perl-CGI-3.51-127.el6.x86_64
perl-HTML-Parser-3.64-2.el6.x86_64
perl-Pod-Escapes-1.04-127.el6.x86_64
perl-Module-Pluggable-3.90-127.el6.x86_64
perl-Net-SSLeay-1.35-9.el6.x86_64
perl-Digest-SHA-5.47-127.el6.x86_64
perl-DBI-1.609-4.el6.x86_64
perl-Compress-Raw-Zlib-2.020-127.el6.x86_64
perl-devel-5.10.1-127.el6.x86_64
perl-IO-Compress-Base-2.020-127.el6.x86_64
perl-libwww-perl-5.833-2.el6.noarch
perl-version-0.77-127.el6.x86_64
perl-Net-LibIDN-0.12-3.el6.x86_64

And Perl compiled from CPAN:
Code:
/root/.cpan/build/ExtUtils-CBuilder-0.280205-UeVBqh
/root/.cpan/build/Crypt-DSA-1.17-ACg843
/root/.cpan/build/Locale-Maketext-Simple-0.21-n5UnGn
/root/.cpan/build/HTTP-Message-6.06-_fACLj
/root/.cpan/build/Test-Requires-0.06-6EdjXk
/root/.cpan/build/Net-SSH-Perl-1.35-n6Mzyc
/root/.cpan/build/Net-SCP-Expect-0.16
/root/.cpan/build/Class-Loader-2.03-t5VEvP
/root/.cpan/build/Class-Accessor-0.34-FI8j4H
/root/.cpan/build/IPC-Cmd-0.78-q9BxWD
/root/.cpan/build/Expect-1.21-xMgDgk
/root/.cpan/build/Math-BigInt-Pari-1.17-ol0DpY
/root/.cpan/build/Encode-Locale-1.03-eNGVfB
/root/.cpan/build/IO-Tty-1.10-2HhM3y
/root/.cpan/build/HTTP-Date-6.02-GlkeJJ
/root/.cpan/build/Module-Load-Conditional-0.54-xx5qoL
/root/.cpan/build/Math-BigInt-1.997-83f7oE
/root/.cpan/build/Crypt-Random-1.25-o_fE3N
/root/.cpan/build/Data-Dump-1.21-QNCUEd
/root/.cpan/build/Module-Runtime-0.013-34e3Rv
/root/.cpan/build/Probe-Perl-0.01-n8mxpy
/root/.cpan/build/IO-Compress-2.060-E9Fp74
/root/.cpan/build/Task-Weaken-1.04-QvKNOG
/root/.cpan/build/IO-HTML-0.04-vNJN9p
/root/.cpan/build/SOAP-Lite-0.715-7P2W6V
/root/.cpan/build/Params-Validate-1.07-zlTPyG
/root/.cpan/build/Convert-PEM-0.08-MrelrJ
/root/.cpan/build/Data-Buffer-0.04-_oojqm
/root/.cpan/build/XML-NamespaceSupport-1.11-Ldy1mi
/root/.cpan/build/Module-Load-0.22-lZZANC
/root/.cpan/build/Nagios-Plugin-0.36-H4nG5F
/root/.cpan/build/Class-Inspector-1.28-SVTF4G
/root/.cpan/build/Math-GMP-2.06-3Olzad
/root/.cpan/build/Compress-Raw-Bzip2-2.060-oYdBpU
/root/.cpan/build/Tie-EncryptedHash-1.24-ESf4mD
/root/.cpan/build/HTTP-Cookies-6.01-2UkLYa
/root/.cpan/build/Digest-MD2-2.03-cfa71T
/root/.cpan/build/Math-BigInt-1.997-Pv7Vgl
/root/.cpan/build/Module-Implementation-0.06-mrfhxL
/root/.cpan/build/Math-Base85-0.2-O75Pw_
/root/.cpan/build/LWP-Protocol-https-6.03-L_OdQ_
/root/.cpan/build/Digest-HMAC-1.03-x5Mbh6
/root/.cpan/build/Module-Build-0.4003-jpIlHQ
/root/.cpan/build/Test-Exception-0.31-TAA0Zs
/root/.cpan/build/Digest-SHA1-2.13-wBJkXz
/root/.cpan/build/WWW-RobotRules-6.02-DGEV8F
/root/.cpan/build/Convert-ASN1-0.26-UMojbm
/root/.cpan/build/Carp-1.26-PpWfZz
/root/.cpan/build/XML-SAX-0.99-rfFgZ_
/root/.cpan/build/Module-CoreList-2.79-FaOm6A
/root/.cpan/build/Crypt-RSA-1.99-1wXpIw
/root/.cpan/build/Net-SCP-0.08
/root/.cpan/build/Crypt-Blowfish-2.12-qbzl9C
/root/.cpan/build/LWP-MediaTypes-6.02-SOwjTU
/root/.cpan/build/XML-SAX-Base-1.08-VeI26H
/root/.cpan/build/Crypt-DES_EDE3-0.01-LXQUBH
/root/.cpan/build/Compress-Raw-Zlib-2.060-ip2mHy
/root/.cpan/build/Crypt-IDEA-1.08-NyBqeB
/root/.cpan/build/Crypt-Primes-0.50-oK8Mzw
/root/.cpan/build/Cache-Memcached-1.30-KF7flP
/root/.cpan/build/File-Which-1.09-Gf4gQ5
/root/.cpan/build/Carp-Clan-6.04-wYwu8v
/root/.cpan/build/String-CRC32-1.4-cbGVJ0
/root/.cpan/build/Term-ReadPassword-0.11-7cL7Ov
/root/.cpan/build/Convert-ASCII-Armour-1.4-mJrx1L
/root/.cpan/build/HTTP-Daemon-6.01-C9IGlO
/root/.cpan/build/libwww-perl-6.04-JOaCr7
/root/.cpan/build/Crypt-CBC-2.32-_kZjUm
/root/.cpan/build/Math-Pari-2.01080605-pQQQsw
/root/.cpan/build/Math-BigInt-Pari-1.17-7QGcuF
/root/.cpan/build/Sort-Versions-1.5-r0Hukj
/root/.cpan/build/Nagios-Plugins-Memcached-0.02-jh6NuF
/root/.cpan/build/Time-HiRes-1.9725-Bq6g8r
/root/.cpan/build/HTTP-Negotiate-6.01-5LORq6
/root/.cpan/build/Test-Fatal-0.010-xORzZx
/root/.cpan/build/CPAN-1.9800-blqlIS
/root/.cpan/build/Digest-BubbleBabble-0.02-zBoosK
/root/.cpan/build/Sub-Uplevel-0.24-vf20cg
/root/.cpan/build/Math-Calc-Units-1.07-vF6DPv
/root/.cpan/build/Perl-OSType-1.002-W8AGG_
/root/.cpan/build/Net-IPv4Addr-0.10-dixZ8L
/root/.cpan/build/Crypt-DES-2.05-b6PbDS
/root/.cpan/build/Params-Check-0.36-l2u3iS
/root/.cpan/build/Net-IPv6Addr-0.2-xEEHE1
/root/.cpan/build/IPC-Run3-0.045-nH75TO
/root/.cpan/build/Module-Metadata-1.000011-0bJHos
/root/.cpan/build/File-Listing-6.04-x5NeW6
/root/.cpan/build/Test-Script-1.07-nd16MV
/root/.cpan/build/File-Slurp-9999.19-_pxR19
/root/.cpan/build/Config-Tiny-2.14-kf4Elm
/root/.cpan/build/Class-ErrorHandler-0.01-e1MToL
/root/.cpan/build/Crypt-DH-0.07-393038
/root/.cpan/build/XML-LibXML-2.0014-GXgN9_
/root/.cpan/build/Class-MethodMaker-2.18-ZcgPsE

Remote systems are Debian 6.0.5

Last edited by grzeslaw; 03-08-2013 at 05:08 PM.
 
Old 03-11-2013, 09:02 AM   #4
grzeslaw
Member
 
Registered: Nov 2008
Posts: 111

Original Poster
Rep: Reputation: 24
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 $!;
Hope it could help somebody in the future
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 01:57 AM
[SOLVED] SCP with wildcard in Expect s7upify Linux - General 5 07-18-2011 12:12 PM
Expect script, scp and find command unihiekka Programming 1 10-17-2008 03:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration