我有一个脚本,它从不同的服务器提取数据,并从外部文件读取其详细信息。它读取文件,详细输出显示所有匹配的文件,但它仅从远程主机获取一个文件。以下是我的脚本:
while IFS=','; read region sdp ip1 ip2 ip3 user1 pass1 user2 pass2 user3 pass3
do
in=/var/opt/fds/statistics/
out=/pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
file=\*"PSC-TrafficHandler_8.1_A_"\*"_System."$date\*".stat"
mkdir -p /pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
ftp -in $ip1<<END_SCRIPT
quote USER $user1
quote PASS $pass1
bin
prompt off
lcd /pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
cd /var/opt/fds/statistics/
binary
mget *PSC-TrafficHandler_8.1_A_*_System.$date*.stat
bye
END_SCRIPT
done < /root/SDP_BHC/bin/Credentials.csv
以下是输出:
IP: 10.XXX.XX.XX
Interactive mode on.
Local directory now /pmautomation/PM/RawFiles/Data/BHCA/20150802/EAST/WB_SDP49
mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0000.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0100.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0200.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0300.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0400.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0500.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0600.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0700.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0800.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_0900.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1000.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1100.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1200.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1300.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1400.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1500.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1600.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1700.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1800.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_1900.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_2000.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_2100.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_2200.stat? mget WDSDP49B_PSC-TrafficHandler_8.1_A_2_System.20150802_2300.stat?
为什么我的 mget 命令无法获取所有文件并且只能从所有匹配文件中获取一个文件?
答案1
我从杰夫那里得到了一些指示。不知何故,提示关闭不起作用,我收到提示以获取匹配的文件。我尝试将“y”放在mget
命令下方并且它有效。以下是更新后的代码:
while IFS=','; read region sdp ip1 ip2 ip3 user1 pass1 user2 pass2 user3 pass3
do
in=/var/opt/fds/statistics/
out=/pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
file=\*"PSC-TrafficHandler_8.1_A_"\*"_System."$date\*".stat"
mkdir -p /pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
ftp -in $ip1<<END_SCRIPT
quote USER $user1
quote PASS $pass1
bin
prompt off
lcd /pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
cd /var/opt/fds/statistics/
binary
mget *PSC-TrafficHandler_8.1_A_*_System.$date*.stat
y
y
y
y
bye
END_SCRIPT
done < /root/SDP_BHC/bin/Credentials.csv
这确实是很奇怪的行为,但它对我有用。幸运的是,我知道远程主机中的文件数量,并输入相同数量的“y”使其工作。
答案2
您是否尝试过从“此处”文档末尾删除选项卡?由于脚本没有检测到结束,这可能是您的问题。
while IFS=','; read region sdp ip1 ip2 ip3 user1 pass1 user2 pass2 user3 pass3
do
in=/var/opt/fds/statistics/
out=/pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
file=\*"PSC-TrafficHandler_8.1_A_"\*"_System."$date\*".stat"
mkdir -p /pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
ftp -in $ip1<<END_SCRIPT
quote USER $user1
quote PASS $pass1
bin
prompt off
lcd /pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
cd /var/opt/fds/statistics/
binary
mget *PSC-TrafficHandler_8.1_A_*_System.$date*.stat
bye
END_SCRIPT <----------
done < /root/SDP_BHC/bin/Credentials.csv
答案3
使用wget
。
while IFS=','; read region sdp ip1 ip2 ip3 user1 pass1 user2 pass2 user3 pass3
do
in=/var/opt/fds/statistics/
out=/pmautomation/PM/RawFiles/Data/BHCA/$date/$region/$sdp/
file=\*"PSC-TrafficHandler_8.1_A_"\*"_System."$date\*".stat"
mkdir -p $out
cd $out
wget --ftp-user=$user1 --ftp-password=$pass1 ftp://$ip1/$in/$file
done < /root/SDP_BHC/bin/Credentials.csv