원격 위치에있는 파일 이름 목록을 얻고 싶습니다.
내 Perl 스크립트에서 아래 스 니펫을 사용하고 있습니다.
my $command = "sftp -q -o${transferAuthMode}=yes -oPort=$sftpPort ${remoteUsername}\@${remoteHost} 2>\&1 <<EOF\n" .
"cd \"${remotePath}\"\n" .
"ls -l \n" .
"quit\n" .
"EOF\n";
my @files = `$command`;
원격 위치의 파일 수가 많으면 (> 500) 모든 파일 이름이 @files
.
수동으로 SFTP를 수행하고 파일을 나열하면 모든 파일이 나열되지만 스크립트를 통해 동일하게 표시되지 않습니다. 매번 @files
크기가 달라집니다. 파일이 많은 경우에만 발생합니다.
그 이유를 찾을 수 없습니다. 도와 주 시겠어요?
이는 추가 패키지 모듈 없이도 가능합니다. CentOS 7 Server (Windows VM)에서 테스트했습니다.
내 원격 호스트 세부 정보 : 원격 호스트 디렉토리에 ~ 2000 개의 파일이 있습니다. CentOS 6.8 서버.
%_gaurav@[remotehost]:/home/gaurav/files/test> ls -lrth|head -3;echo;ls -lrth|tail -2
total 7.9M
-rw-rw-r--. 1 gaurav gaurav 35 Feb 16 23:51 File-0.txt
-rw-rw-r--. 1 gaurav gaurav 35 Feb 16 23:51 File-1.txt
-rw-rw-r--. 1 gaurav gaurav 38 Feb 16 23:51 File-1998.txt
-rw-rw-r--. 1 gaurav gaurav 38 Feb 16 23:51 File-1999.txt
%_gaurav@[remotehost]: /home/gaurav/files/test>
LocalHost의 스크립트 출력 : 명령을 실행하고 있다는 점에 유의하십시오 o${transferAuthMode}=yes
. 아래에서 볼 수 있듯이 스크립트는 500 개 이상의 결과를 배열에 모두 수집 할 수 있습니다.
결과를 표시하기 위해 배열의 전체 항목, 특정 인덱스 번호를 prnting하고 있지만 주석 처리되지 않은 Dumper 줄로 시도하여 전체 결과를 확인합니다.
%_STATION@gaurav * /root/ga/study/pl> ./scp.pl
Read 2003 lines from SCP command.
ArrayIndex: 2,3,1999,2000 contain:
[-rw-rw-r-- 0 501 501 36B Feb 16 23:51 File-58.txt]
[-rw-rw-r-- 0 501 501 37B Feb 16 23:51 File-129.txt]
[-rw-rw-r-- 0 501 501 38B Feb 16 23:51 File-1759.txt]
[-rw-rw-r-- 0 501 501 38B Feb 16 23:51 File-1810.txt]
%_STATION@gaurav * /root/ga/study/pl>
스크립트 및 작업 :
#!/usr/bin/perl
use strict ;
use warnings ;
use Data::Dumper ;
my $sftp_port=22 ;
my ($user, $host) = ("gaurav","192.168.246.137") ;
my $remote_path = '/home/gaurav/files/test' ;
my @result ; # To store result
my $command = "sftp -q -oPort=$sftp_port ${user}\@${host} 2>\&1 <<EOF\n"."cd $remote_path\nls -lrth\nquit\nEOF" ;
# open the command as a file handle, read output and store it.
open FH, "$command |" or die "Something went wrong!!\n" ;
while (<FH>) {
tr/(?\r|\f|\n)//d ; # Removing any new line, carriage return or form feed.
push(@result,"\[$_\]") ;
}
close FH ;
#print Dumper @result ;
# Just for printing a little bit of results from
# the array. Following lines can be deleted.
my $total = scalar @result ;
print "Read $total lines from SCP command.\n" ;
print "\nArrayIndex: 2,3,1999,2000 contain:\n
$result[2]
$result[3]
$result[1999]
$result[2000]
" ;
다른 방법 : 쉘 스크립트를 만들고 perl 스크립트에서 호출하여 출력을 읽어이 문제를 해결할 수도 있습니다. 아래와 같이 펄 스크립트와 최종 출력에 의해 호출되는 쉘 스크립트입니다. 이것은 perl에서 직접 명령을 작성 / 형식화 할 시간이 많지 않을 때 빠른 기술로 사용할 수 있습니다. 이전 스크립트에서도 qx 스타일 (아래 참조)을 사용할 수 있습니다 .
쉘 스크립트 "scp.sh"
%_STATION@gaurav * /root/ga/study/pl> cat scp.sh
#!/bin/bash
sftp -oPort=${1} ${2}@${3} 2>&1 <<EOF
cd ${4}
ls -l
quit
EOF
Perl 스크립트 "2scp.pl"
%_STATION@gaurav * /root/ga/study/pl> cat 2scp.pl
#!/usr/bin/perl
use strict ;
use warnings ;
use Data::Dumper ;
my $sftp_port=22 ;
my ($user, $host) = ("gaurav","192.168.246.137") ;
my $remote_path = '/home/gaurav/files/test' ;
# Passing arguements to shell script using concatination.
my $command = './scp.sh '." $sftp_port $user $host $remote_path" ;
my @result = qx{$command} ; # Runs the command and stores the result.
my $total = scalar @result ;
print "Read $total lines from SCP command.\n" ;
# End.
산출:
%_STATION@gaurav * /root/ga/study/pl> ./2scp.pl
Read 2004 lines from SCP command.
%_STATION@gaurav * /root/ga/study/pl>
그것을 시도하고 알려주십시오. 감사.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다