Perl脚本学习经验(四)--Perl中sftp的使用

使用sftp,需要使用Except模块,该模块需要下载安装在perl目录下,可以上http://www.cpan.org/上下载对应的安装包;
1. 用root用户登录环境;
2. cd /usr/lib/perl5/
3. 上传包IO-Tty-1.10.tar.gz 和 Expect-1.21.tar.gz到perl5目录下;
4. 安装IO-Tty-1.10
 tar -zxvf IO-Tty-1.10.tar.gz
 cd IO-Tty-1.10/
 perl Makefile.PL
 make
 make test
 make install
5. 安装Expect-1.21
 tar -zxvf Expect-1.21.tar.gz
 cd Expect-1.21/
 perl Makefile.PL
 make
 make test
 make install

使用use Expect;
Demo:
    my $ssh = Expect->new;
    my $timeout = 10;
    $ssh->raw_pty => 1;
    $ssh->spawn("sftp $User\@$Server") or die "spawn failed!\n";
    $ssh->expect($timeout,[qr/continue connecting (yes\/no)?/,sub{$ssh->send("yes\n");exp_continue;}],
                          [qr/Password:/,sub{$ssh->send("$Password\n");exp_continue;}],
                          [qr/sftp>/,sub{$ssh->send("cd config\n");}]);
                         
    $ssh->expect($timeout,[qr/sftp>/,sub{$ssh->send("put $g_local_file $g_file\n");}]);
    $ssh->expect(86400,"sftp>") or die "not see sftp tip for exit!";
    $ssh->send("exit\n");
    $ssh->soft_close();

 

备注:

 1. 第一次使用sftp的时候,没有记录对应ip,所以会有以下提示:

Connecting to 192.168.1.1...
The authenticity of host 192.168.1.1(192.168.1.1)' can't be established.
RSA key fingerprint is 86:39:3b:c9:4b:90:df:e9:39:1d:3c:fb:56:26:2f:28.
Are you sure you want to continue connecting (yes/no)?

所以在脚本中需要发送yes

 

  2.exp_continue 匹配到就执行send中的命令,匹配不到就继续执行下面的命令;

     如果不加exp_continue,顺序执行脚本命令;

转载于:https://www.cnblogs.com/huochangjun/p/3365484.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值