Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于从服务器获取最新文件的unix ftp脚本_Unix_Ftp - Fatal编程技术网

用于从服务器获取最新文件的unix ftp脚本

用于从服务器获取最新文件的unix ftp脚本,unix,ftp,Unix,Ftp,我有一个unix脚本,可以通过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 #/垃圾箱/垃圾箱 HOST='1.1.1.1' USER='USER' PA

我有一个unix脚本,可以通过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
#/垃圾箱/垃圾箱
HOST='1.1.1.1'
USER='USER'
PASSWD='pass'
文件='1234'

ftp-n$HOST这实际上是在将ftp客户端推得比应该推的更远,但这是可能的

请注意,在您的系统上,LS_FILE_偏移量可能不同,如果偏移量错误,这将根本不起作用

#!/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
#/垃圾箱/垃圾箱
HOST='1.1.1.1'
USER='USER'
PASSWD='pass'
目录='.LogbookPlus'
文件\u到\u获取=1
LS_FILE_OFFSET=57#检查目录列表以查看文件名的起始位置
rm-f目录列表
#从按修改日期排序的目录中获取列表

ftp-n$HOST>directory_listing这实际上是在将ftp客户端推得比应该推的更远,但这是可能的

请注意,在您的系统上,LS_FILE_偏移量可能不同,如果偏移量错误,这将根本不起作用

#!/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
#/垃圾箱/垃圾箱
HOST='1.1.1.1'
USER='USER'
PASSWD='pass'
目录='.LogbookPlus'
文件\u到\u获取=1
LS_FILE_OFFSET=57#检查目录列表以查看文件名的起始位置
rm-f目录列表
#从按修改日期排序的目录中获取列表

ftp-n$HOST>directory_listing这实际上是在将ftp客户端推得比应该推的更远,但这是可能的

请注意,在您的系统上,LS_FILE_偏移量可能不同,如果偏移量错误,这将根本不起作用

#!/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
#/垃圾箱/垃圾箱
HOST='1.1.1.1'
USER='USER'
PASSWD='pass'
目录='.LogbookPlus'
文件\u到\u获取=1
LS_FILE_OFFSET=57#检查目录列表以查看文件名的起始位置
rm-f目录列表
#从按修改日期排序的目录中获取列表

ftp-n$HOST>directory_listing这实际上是在将ftp客户端推得比应该推的更远,但这是可能的

请注意,在您的系统上,LS_FILE_偏移量可能不同,如果偏移量错误,这将根本不起作用

#!/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
#/垃圾箱/垃圾箱
HOST='1.1.1.1'
USER='USER'
PASSWD='pass'
目录='.LogbookPlus'
文件\u到\u获取=1
LS_FILE_OFFSET=57#检查目录列表以查看文件名的起始位置
rm-f目录列表
#从按修改日期排序的目录中获取列表

ftp-n$HOST>directory_listing您应该明确提供有关您正在使用的系统的更多信息,例如,并非每个ftp服务器都支持@JesseParker使用的
ls-t
。我利用这个机会,把我用了一段时间的一些想法写进一个脚本,用
awk
来描述肮脏的行为。如您所见,了解您的客户机使用的unix的风格将是有益的。我已经在Debian Wheezy GNU/Linux和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
#/垃圾箱/垃圾箱
#用法:[……]
#
#将文件从预配置的ftp服务器提取到当前目录。
#最大文件数为
#仅考虑修改时间比给定日期更新的文件。
#这个日期是根据本地的“date”命令给出的,这是非常不同的
#在BSD和GNU系统上,例如:
# 
#GNU:
#昨天
#去年
#1970年1月1日
#
#BSD:
#-v-1d#昨天(现在减去1天)
#-v-1y#去年(现在减去1年)
#-f%b%e%C%y 1970年1月1日#格式:月日世纪年
#
#脚本尝试自动检测日期系统YMMV。
#
#错误:
#不喜欢文件名中的引号(“),可能更多。
#
#此文件中不应包含凭据,但可能包含凭据
#在“.netrc”中,而不使用“ftp-n”。
#
#还有很多。
#
HOST='1.1.1.1'
USER='USER'
PASSWD='pass'
DIR='.LogbookPlus'
#用于数字比较的日期格式。如果支持,可以简单地为+%s。
日期\u FMT=+%C%y%m%d%H%m%S
#日期字符串的服务器区域设置。
LC\U SRV\U日期=C
#来自BSD系统和GNU coreutils的“date”命令
#完全不同。请在此处测试相应的系统:
如果LC_ALL=C date-j-f“%b%e%C%y”1970年1月1日“$date\u FMT>/dev/null 2>&1;则
系统类型=BSD
elif LC_ALL=C date-d“1970年1月1日”$date\u FMT>/dev/null 2>&1;然后
SYS_TYPE=GNUish
其他的
echo“sh:不知道如何约会;-)很抱歉"
出口1;
fi
#要获取的最大文件数(首先是最新文件)
MAX_NUM=$(${1:-1}+0))#确保argv[1]被视为一个数字
转移
#文件的最大年限。仅考虑更新的文件。
如果[GNUish=“$SYS_TYPE”];则
最大年龄=$(日期“$date\U FMT”-d“${*:-昨天}”)
elif[BSDish=“$SYS_TYPE”];然后
最大年龄=$(日期-j“${*:--v-1d}”“$date\U FMT”)
fi
#创建临时文件
TMP_文件=$(mktemp)
陷阱“rm-f”$TMP_文件”退出INT TERM HUP
ftp-i-n$HOST“tmp_文件
}
$perm~/^[^-]/{#跳过非常规文件
下一个
}
{
如果(“BSDish”==系统类型){
如果($yeartime~/[0-9][0-9][0-9][0-9]/){
ts_fmt=“\%b%e%C%y”
}否则如果($yeartime~/[0-9][0-9:[0-9][0-9]/){
ts_fmt=“\%b%e%H:%M”
}否则{
打印“既没有年份也没有时间:”$8
出口1
}
}else{#在BEGIN中测试:必须是“GNUish”
ts_fmt=“”
}
cmd=date\u cmd“ts\u fmt”\“$month”$day“$yeartime”\“date\u fmt”
cmd | getline时间戳
关闭(cmd)
如果(时间戳>最大年龄){
#清除除文件名以外的所有内容
$perm=$links=$user=$group=$size=$month=$day=$yeartime=“”
文件[文件名称]=$0
文件[file_cnt,“time”]=时间戳
++文件
}
}
结束{
对于(i=0;i=0){
$1=”“#删除时间戳
gsub(/^/,“”)#带前导空格
打印“获取”$0
}
打印“退出”
}
' \
|ftp-v-i-n$HOST
报价用户$USER
报价通过$PASSWD
cd“$DIR”
迪尔。
退出
结束\u FTP\u脚本

你肯定应该提供更多关于你正在使用的系统的信息,例如,不是每个ftp服务器都支持@JesseParker使用的
ls-t
。我利用了这个机会,提出了一些我使用mysel的想法