首页 > Web开发 > 详细

Net::SCP::Expect 模块详解

时间:2015-08-12 17:09:11      阅读:357      评论:0      收藏:0      [点我收藏+]

示例:

 

Example 1 - uses login method, longhand scp:
        my $scpe = Net::SCP::Expect->new;
        $scpe->login(‘user name‘, ‘password‘);
        $scpe->scp(‘file‘,‘host:/some/dir‘);
Example 2 - uses constructor, shorthand scp:
        my $scpe = Net::SCP::Expect->new(host=>‘host‘, user=>‘user‘, password=>‘xxxx‘);
        $scpe->scp(‘file‘,‘/some/dir‘); # ‘file‘ copied to ‘host‘ at ‘/some/dir‘
Example 3 - copying from remote machine to local host
        my $scpe = Net::SCP::Expect->new(user=>‘user‘,password=>‘xxxx‘);
        $scpe->scp(‘host:/some/dir/filename‘,‘newfilename‘);
Example 4 - uses login method, longhand scp, IPv6 compatible:
        my $scpe = Net::SCP::Expect->new;
        $scpe->login(‘user name‘, ‘password‘);
        $scpe->scp(‘file‘,‘[ipv6-host]:/some/dir‘); # <-- Important: scp() with explicit IPv6 host in to or from address must use square brackets
       See the scp() method for more information on valid syntax

上面展示了一些跟远程端交互的方法。

将远程服务器的文件拷贝到本机上。

示例1;测试代码如下:

#!/usr/bin/perl
use v5.10;
use warnings;
use Net::SCP::Expect;
#定义远程服务器的ip 登陆用户 和密码;
my $server=‘192.168.0.128‘;
my $user=‘root‘;
my $pass=‘root12300.‘;
#创建一个连接远程服务器的对象;
my $scp=Net::SCP::Expect->new(
                host    =>$server,
                user    =>$user,
                password=>$pass
);
#第一次登陆时,用来进行交互
$scp->auto_yes(1);
$scp->scp(":/test/src/App-cpanminus-1.7039.tar.gz","/test/") or die $scp->error_handler;
say "文件拷贝完成..ok";
[root@TrackerServer Net]# ls
test.pl
[root@TrackerServer Net]# 
[root@TrackerServer Net]# ls /test/ -l
total 0
[root@TrackerServer Net]# ./test.pl 
文件拷贝完成..ok
[root@TrackerServer Net]# ls /test/ -l
total 312
-rw-r--r-- 1 root root 316814 Aug 12 15:55 App-cpanminus-1.7039.tar.gz
[root@TrackerServer Net]#

将本机的文件拷贝到远程机器上

示例2:测试代码如下(只需要将scp方法中的文件位置变换下就行了):

#!/usr/bin/perl
use v5.10;
use warnings;
use Net::SCP::Expect;
#定义远程服务器的ip 登陆用户 和密码;
my $server=‘192.168.0.128‘;
my $user=‘root‘;
my $pass=‘root12300.‘;
#创建一个连接远程服务器的对象;
my $scp=Net::SCP::Expect->new(
host=>$server,
user=>$user,
password=>$pass
);
#第一次登陆时,用来进行交互
$scp->auto_yes(1);
$scp->scp("/test/App-cpanminus-1.7039.tar.gz",":/test/") or die $scp->error_handler;
say "文件拷贝完成..ok";



 scp()

 

     Copies the file from source to destination.  If no host is specified, 
     you will be using ’scp’ as an expensive form of ’cp’.
       There are several valid ways to use this method
       Local to Remote
       scp(source, user@host:destination);
       scp(source, user@[ipv6-host]:destination);  # Same as previous, with IPv6 host
       scp(source, host:destination); # User already defined
       scp(source, [ipv6-host]:destination); # Same as previous, with IPv6 host
       scp(source, :destination); # User and host already defined
       scp(source, destination); # Same as previous
       scp(source); # Same as previous; destination will use base name of source
       Remote to Local
       scp(user@host:source, destination);
       scp(user@[ipv6-host]:source, destination); # Same as previous, with IPv6 host
       scp(host:source, destination);
       scp([ipv6-host]:source, destination); # Same as previous, with IPv6 host
       scp(:source, destination);

























Net::SCP::Expect 模块详解

原文:http://my.oschina.net/u/2420214/blog/491236

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!