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,顺序执行脚本命令;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律