본문 바로가기
예제코드
2012.12.23 12:21

Perl Expect ssh and scp example script

조회 수 11671 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

http://programming-in-linux.blogspot.sg/2008/09/remote-scp-and-ssh-using-expect.html


Very often I need a install script that will do the job remotely, or copy files automatically or any other operation I have to do over an ssh connection. 
There are a few alternatives in order to do this and expect is providing a solution to this problem.

Most people know the expect from the Tcl package and its C interface that exists in the same package. Writing a C utility is possible but often too rigid in case I need just a script that I can evolve and adapt easily throughout a project. 

There is the pexpect also for Python and paramiko library as well and of course there is the Expect.pm package for Perl language. 
All of them have advantages and disadvantages depending on the purpose and of course depending on the taste you have for a specific language. 
Here I post an example of an ssh script I often use as the initial template to develop more elaborate scripts. 

This example uses the Expect.pm which you will need to download and install. 
You can download the extension from here :

http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod


The script will remotely copy a file using scp and then display the list from the remote site. 

Here is the script :

#!/usr/bin/perl 

use warnings;
use Expect;

sub expect_prompt(){
my $ssh_exp = shift;
my $timeout = shift;
my $prompt = shift;
$ssh_exp->expect($timeout, '-re' , $prompt) or die "expect error" . $ssh_exp->exp_error() . "\n";
my $out = $ssh_exp->before();
return $out;
}
sub remote_copy (){

my $scp = shift;
my $ruser = shift;
my $password = shift;
my $server = shift;
my $localpath = shift;
my $timeout = shift;
my $prompt = '\$\s*';
my $spawn_ok;

my $scp_exp = new Expect;
$scp_exp->spawn("$scp $localpath $ruser\@$server\:\.");# or die "Cannot spawn ssh: $!\n";

$scp_exp->expect(5, [qr'\(yes/no\)\s*' , sub {my $exph = shift; print $exph "yes\r" ;exp_continue; }],
[qr'word:\s*' , sub {my $exph = shift; print $exph "$password\r";exp_continue; }],
[EOF => sub {die "Error: Could not login!\n"; }],
[timeout => sub {die "Error: Could not login!\n"; }],
'-re', '\$');

# $scp_exp->expect($timeout, '-re' ,'word:\s*') or die "nok";
# print $scp_exp "$password\r";
# $scp_exp->expect($timeout, '-re' , $prompt) or return "scp finished!\n";
$scp_exp->soft_close();
#
# Check md5sum @todo
#
}
sub remote_install (){

my $ssh = shift;
my $ruser = shift;
my $password = shift;
my $server = shift;
my $lfile = shift;
my $timeout = shift;
my $prompt = '\$\s*';
my $root_prompt = '\#\s*';
my $spawn_ok;

my $ssh_exp = new Expect;
$ssh_exp->spawn("$ssh $ruser\@$server") or die "Cannot spawn ssh: $!\n";
$ssh_exp->expect($timeout, '-re' ,'word:\s*') or die "nok";
print $ssh_exp "$password\r";
&expect_prompt($ssh_exp, $timeout, $prompt);

print $ssh_exp "ls\r";
my $list = &expect_prompt($ssh_exp, $timeout, $prompt);
print "\n******** Remote FILE LIST *******\n $list \n"; 
print "*********************************\n\n"; 
$ssh_exp->hard_close(); 
}

#--------------------------------------------------------------
# Main 
#--------------------------------------------------------------

my $scp = "/usr/bin/scp";
my $ssh = "/usr/bin/ssh";
my $ruser = "auser";
my $rpass = "mypassword";
my $rserver = "10.10.10.1";
my $lfile = "/home/username/myfile.txt";
my $timeout = 10;

&remote_copy ($scp, $ruser, $rpass, $rserver, $lfile, $timeout);
&remote_install ($ssh, $ruser, $rpass, $rserver, $lfile, $timeout);




Enjoy!


Title
List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 기본정보 Regular Expression Reference Hojung 2012.12.03 19900
공지 기본정보 모듈 설치방법 Hojung 2012.10.31 16978
공지 기본정보 Activestate 설치 + PATH 설정 + 모듈 설치 Hojung 2010.04.19 35115
64 예제코드 Perl script to push samples to Virustotal Hojung 2014.02.20 5390
63 예제코드 TS 디렉토리에서 techsupport 찾아 모델별로 mv 명령 출력 (File::Find) Hojung 2013.01.22 7852
62 예제코드 How can I read in an entire file all at once?: Hojung 2013.01.14 8487
» 예제코드 Perl Expect ssh and scp example script Hojung 2012.12.23 11671
60 기본정보 How to install HTML::HTMLDoc from Mac Hojung 2012.11.19 7631
59 기본정보 How to install module (모듈 설치방법 3가지) Hojung 2012.11.19 7373
58 기본정보 How to uninstall module from CPAN Hojung 2012.11.19 7574
57 예제코드 turning seconds into days, hours, minutes and seconds Hojung 2012.11.12 7322
56 예제코드 How to print Unix timestamp Hojung 2012.11.12 9162
55 기본정보 MAC에서 HTML::HTMLDoc 설치하기 Hojung 2012.10.31 7893
54 기본정보 Mac에서 PDF::FromHTML 모듈 설치하기 Hojung 2012.10.31 7588
53 기본정보 CPAN: Automatically install dependencies without confirmation Hojung 2012.10.31 8428
52 예제코드 WWW::Mechanize::Firefox file Hojung 2012.10.30 9509
51 예제코드 Get number of elements in an array and a hash Hojung 2012.06.04 6909
50 예제코드 Cross-platform cross-environment RPC server creation Hojung 2012.05.30 7267
49 예제코드 인수가 이메일형식인지 확인 Hojung 2012.03.11 7535
48 예제코드 How to keep session in Perl CGI Hojung 2012.03.07 7486
47 기본정보 CGI::Session::Tutorial Hojung 2012.03.06 7733
Board Pagination ‹ Prev 1 2 3 4 Next ›
/ 4

Designed by sketchbooks.co.kr / sketchbook5 board skin

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5