2014-11-21 2 views
0

에서 최신 파일을 얻기 위해 나는 ftp를 이런 식으로 뭔가 보이는 통해 파일을받을 수있는 유닉스 스크립트가 : 특정 파일을 얻는 대신유닉스 FTP 스크립트는 서버

#!/bin/sh 
HOST='1.1.1.1' 
USER='user' 
PASSWD='pass' 
FILE='1234' 

ftp -n $HOST <<END_SCRIPT 
quote USER $USER 
quote PASS $PASSWD 
cd .LogbookPlus 
get $FILE 
quit 
END_SCRIPT 
exit 0 

을, 나는 마지막 수정 된 파일을 얻으려면 폴더 또는 지난 24 시간 동안 생성 된 모든 파일. ftp를 통해 가능합니까?

답변

2

이것은 실제로 푸시되어야하는 것보다 더 많은 FTP 클라이언트를 밀고 있지만 가능합니다.

LS_FILE_OFFSET은 시스템에 따라 다를 수 있으며 오프셋이 잘못된 경우에는 작동하지 않습니다.

#!/bin/sh 

HOST='1.1.1.1' 
USER='user' 
PASSWD='pass' 
DIRECTORY='.LogbookPlus' 
FILES_TO_GET=1 
LS_FILE_OFFSET=57 # Check directory_listing to see where filename begins 

rm -f directory_listing 

# get listing from directory sorted by modification date 
ftp -n $HOST > directory_listing <<fin 
quote USER $USER 
quote PASS $PASSWD 
cd $DIRECTORY 
ls -t 
quit 
fin 

# parse the filenames from the directory listing 
files_to_get=`cut -c $LS_FILE_OFFSET- < directory_listing | head -$FILES_TO_GET` 

# make a set of get commands from the filename(s) 
cmd="" 
for f in $files_to_get; do 
cmd="${cmd}get $f 
" 
done 

# go back and get the file(s) 
ftp -n $HOST <<fin 
quote USER $USER 
quote PASS $PASSWD 
cd $DIRECTORY 
$cmd 
quit 
fin 

exit 0 
+0

정말 감사합니다. 나는 이것을 시도했지만이 부분은 에러를 준다 : $ files_to_get에 f에 대해 ; do cmd = "$ {cmd} get $ f – clayton33

+0

오류 란 무엇입니까? 어떤 셸을 사용하고 있습니까? –

1

사용중인 시스템에 대한 자세한 정보를 제공해야합니다. 모든 ftp 서버은 @JesseParker에서 사용하는 ls -t을 지원합니다. 나는 그 기회를 이용하여 얼마 동안 자신이 사용했던 아이디어를 awk을 더러운 행위에 사용하는 스크립트에 넣었다. 보시다시피 유닉스의 어떤 맛이 있는지 알고 계시다면 고객을 사용하는 것이 좋습니다. 데비안 위지 (Debian Wheezy) GNU/리눅스와 FreeBSD 9.2에서이 스크립트를 테스트 해 보았습니다.

#!/bin/sh 


# usage: <this_script> <num_files> <date...> [ <...of...> <...max....> <...age...> ... ] 
# 
# Fetches files from preconfigured ftp server to current directory. 
# Maximum number of files is <num_files> 
# Only files that have a newer modification time than given date are considered. 
# This date is given according to the local 'date' command, which is very different 
# on BSD and GNU systems, e.g.: 
# 
# GNU: 
# yesterday 
# last year 
# Jan 01 1970 
# 
# BSD: 
# -v-1d      # yesterday (now minus 1 day) 
# -v-1y      # last year (now minus 1 year) 
# -f %b %e %C%y Jan 01 1970 # format: month day century year 
# 
# Script tries to autodetect date system, YMMV. 
# 
# BUGS: 
# Does not like quotation marks (") in file names, maybe much more. 
# 
# Should not have credentials inside this file, but maybe have them 
# in '.netrc' and not use 'ftp -n'. 
# 
# Plenty more. 
# 

HOST='1.1.1.1' 
USER='user' 
PASSWD='pass' 
DIR='.LogbookPlus' 


# Date format for numerical comparison. Can be simply +%s if supported. 
DATE_FMT=+%C%y%m%d%H%M%S 


# The server's locale for date strings. 
LC_SRV_DATE=C 

# The 'date' command from BSD systems and that from the GNU coreutils 
# are completely different. Test for the appropriate system here: 

if LC_ALL=C date -j -f "%b %e %C%y" "Jan 01 1970" $DATE_FMT > /dev/null 2>&1 ; then 
    SYS_TYPE=BSDish 
elif LC_ALL=C date -d "Jan 01 1970" $DATE_FMT > /dev/null 2>&1 ; then 
    SYS_TYPE=GNUish 
else 
    echo "sh: don't know how to date ;-) sorry!" 
    exit 1; 
fi 

# Max. number of files to get (newest files first) 
MAX_NUM=$((${1:-1} + 0)) # ensure argv[1] is treated as a number 
shift 

# Max. age of files. Only files newer that this will be considered. 
if [ GNUish = "$SYS_TYPE" ] ; then 
    MAX_AGE=$(date "$DATE_FMT" -d "${*:-yesterday}") 
elif [ BSDish = "$SYS_TYPE" ] ; then 
    MAX_AGE=$(date -j "${*:--v-1d}" "$DATE_FMT") 
fi 


# create temporary file 
TMP_FILE=$(mktemp) 
trap 'rm -f "$TMP_FILE"' EXIT INT TERM HUP 


ftp -i -n $HOST <<END_FTP_SCRIPT | \ 
awk -v max_age="$MAX_AGE" \ 
    -v max_num="$MAX_NUM" \ 
    -v date_fmt="$DATE_FMT" \ 
    -v date_loc="$LC_SRV_DATE" \ 
    -v sys_type="$SYS_TYPE" \ 
    -v tmp_file="$TMP_FILE" ' 
BEGIN { 
    # columns in the 'dir' output from the ftp server: 
    # drwx------ 1 user  group  4096 Apr 8 2009 Mail 
    # -rw------- 1 user  group  13052 Nov 20 02:07 .bash_history 
    perm=1; links=2; user=3; group=4; size=5; month=6; day=7; yeartime=8; # name=$9..$NF 

    if ("BSDish" == sys_type) { 
     date_cmd="LC_ALL=" date_loc " date -j -f" 
    } else if ("GNUish" == sys_type) { 
     date_cmd="LC_ALL=" date_loc " date -d" 
    } else { 
     print "awk: don'\''t know how to date ;-) sorry!" > "/dev/stderr" 
     exit 1; 
    } 

    files[""] = "" 
    file_cnt = 0 
    out_cmd = "sort -rn | head -n " max_num " > " tmp_file 
} 

$perm ~ /^[^-]/ { # skip non-regular files 
    next 
} 

{ 
    if ("BSDish" == sys_type) { 
     if ($yeartime ~ /[0-9][0-9][0-9][0-9]/) { 
      ts_fmt = "\"%b %e %C%y\"" 
     } else if ($yeartime ~ /[0-9][0-9:[0-9][0-9]/) { 
      ts_fmt = "\"%b %e %H:%M\"" 
     } else { 
      print "has neither year nor time: " $8 
      exit 1 
     } 
    } else { # tested in BEGIN: must be "GNUish" 
     ts_fmt = "" 
    } 
    cmd = date_cmd " " ts_fmt " \"" $month " " $day " " $yeartime "\" " date_fmt 
    cmd | getline timestamp 
    close(cmd) 
    if (timestamp > max_age) { 
     # clear everything but the file name 
     $perm=$links=$user=$group=$size=$month=$day=$yeartime="" 
     files[ file_cnt,"name" ] = $0 
     files[ file_cnt,"time" ] = timestamp 
     ++file_cnt 
    } 
} 

END { 
    for(i=0; i<file_cnt; ++i) { 
     print files[ i,"time" ] "\t" files[ i,"name" ] \ 
     | out_cmd 
    } 
    close(out_cmd) 

    print "quote USER '$USER'\nquote PASS '$PASSWD'\ncd \"'$DIR'\"" 
    i = 0 
    while((getline < tmp_file) > 0) { 
     $1 = "" # drop timestamp 
     gsub(/^ /,"") # strip leading space 
     print "get \"" $0 "\"" 
    } 
    print "quit" 
} 
' \ 
| ftp -v -i -n $HOST 
quote USER $USER 
quote PASS $PASSWD 
cd "$DIR" 
dir . 
quit 
END_FTP_SCRIPT