使用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 installssh
使用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();spa
备注:ip
1. 第一次使用sftp的时候,没有记录对应ip,因此会有如下提示:ci
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)?get
因此在脚本中须要发送yesit
2.exp_continue 匹配到就执行send中的命令,匹配不到就继续执行下面的命令;test
若是不加exp_continue,顺序执行脚本命令;登录