---
title: "用Net::SSH::Perl和Net::SCP::Expect写部署脚本"
author: "luolonghao"
date: "2010-11-21T20:48:49Z"
tags: []
categories: ["操作系统"]
slug: "net-ssh-perl-net-scp-expect"
draft: false
lastmod: "2010-12-31T00:44:03Z"
id: 819080
url: "https://luolonghao.iteye.com/blog/819080"
---

一般写shell脚本多台服务器之间进行操作要打通密码，就是公私钥认证方式。如果用Perl的Net::SSH::Perl和Net::SCP::Expect库不需要创建公私钥，直接用原有ssh帐户就可以实现部署脚本的自动化。

安装方法：

```bash

cpan -i Net::SSH::Perl
cpan -i Net::SCP::Expect

```

注：Net::SSH::Perl依赖gmp，我在mac os上刚开始安装gmp5会报错，换gmp4就没问题。

Net::SSH::Perl：

```perl

#查看192.168.1.102服务器的/tmp目录下的文件
my $ssh = Net::SSH::Perl->new('192.168.1.102');
$ssh->login('user', 'password');
$ssh->cmd('ls -al /tmp/');

```

更多文档：http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod

Net::SCP::Expect:

```perl

#将test.tar.gz文件复制到192.168.1.102服务器的/tmp目录下
my $scpe = Net::SCP::Expect->new;
$scpe->login('user', ’password‘);
$scpe->scp("/tmp/test.tar.gz", '192.168.1.102:/tmp/');

```

更多文档：http://search.cpan.org/~turnstep/Net-SSH-Perl-1.34/lib/Net/SSH/Perl.pm