For loop 使用文件中的行作为变量,馈送到程序-自动化wget
我编写这个脚本是为了从我的不同服务器上实现wget的自动化,并将结果输出到日志文件中,然后通过电子邮件发送给我。我分享这一点的部分原因是为了让其他人能够自己使用解决方案 我已将该文件放置在cron.weekly中,因此它每周向我发送电子邮件 顺便说一句,我是一个绝对的新手,这是我从这个网站学习写的第一个脚本 脚本如下: 我也从我这边做了一些小评论For loop 使用文件中的行作为变量,馈送到程序-自动化wget,for-loop,automation,cron,pipe,wget,For Loop,Automation,Cron,Pipe,Wget,我编写这个脚本是为了从我的不同服务器上实现wget的自动化,并将结果输出到日志文件中,然后通过电子邮件发送给我。我分享这一点的部分原因是为了让其他人能够自己使用解决方案 我已将该文件放置在cron.weekly中,因此它每周向我发送电子邮件 顺便说一句,我是一个绝对的新手,这是我从这个网站学习写的第一个脚本 脚本如下: 我也从我这边做了一些小评论 #!/bin/sh #wget -verbose -tries -timeout -dontcreatedirectories -dontsavean
#!/bin/sh
#wget -verbose -tries -timeout -dontcreatedirectories -dontsaveanything http://x output text to file
# automating apt-get update and upgrade so it runs weekly
apt-get update
apt-get upgrade -y
# this is where the logs will be stored
cd /var/log
# create a tmp file and if it is left behind from earlier results, empty it out
echo -e "\n" > wget.tmp
# these are the header texts for the results
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "WGET SCHEDULES" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
# this will make note of the time the tests are started
date "+START OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
# up to 10 servers to wget from
echo "Server #1" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
wget -v -r -t 2 -T 7 -nd -O /dev/null http://servername/file 2>> wget.tmp
grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
rm -f wget.tmp
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
。
持续到
理想情况下,我不想将自己限制在x个服务器上,但只要文件servers.list中有文本(通过文件名中包含“.”,我就可以将其存储在cron.weekly中,并且不会对其进行处理)。因此,对于每个服务器,写入服务器#,并处理wget、输出到文件、grep到日志文件。最好格式化servers.list文件
hostname filename store/to
.
.
hostname filename store/to
例如:
msn.robots.ua 100mb.test /dev/null
justme.cov ratings.xlsx /srv/storage/latest
邮件看起来像
主题:Servername Wget Results v.2.0 Sendmail 09/09/12----周日04:59:21(MSK)::第36周
工作组时间表
2012年9月9日星期日04:52:00开始WGETS计划 服务器#1 连接到xxx.xxxxxx.com | ip.ip.ip.ip |:80。。。有联系的。 长度:104857600(100M)[文本/纯文本] 10235万。。。。。。。。。。100%573万美元 102400K 100%0.00=25s 2012-09-09 04:52:27(4.07 MB/s)-`/dev/null'已保存[104857600/104857600] .
2012年9月9日星期日04:59:21 WGETS计划结束
因此,my/var/log最终将wordpress-wget-SUMMARY-sep24_2012.log等文件存储为示例文件名。问题是维护它正在成为一个问题,服务器会发生变化,所以我只想更新一个位置的服务器列表,让它们自己处理所有的问题。在执行之前,它将下载新的servers.list,其余的都完成了 邮件正在工作日志文件的输出正在工作,一切正常,只需稍加调整
泰!泰!Ty用于阅读。如果我正确地复制了你的问题,你想替换这样一个事实:你只能使用10台服务器,并且想要从一个文件中提取数据,而不必在脚本中修复它(如果有任何更改)。基于这种重复,我建议如下: 在server.list文件中,我将在代码截取后解释为什么要对其进行分隔。因此,您的server.list文件如下所示:
msn.robots.ua|100mb.test|/dev/null
justme.cov|ratings.xlsx|/srv/storage/latest
使用此设置,您可以在中间的BASH脚本中放置一个for循环,其中的复制代码如下:
for conf in $(cat server.list); do
server=`echo $conf | cut -d"|" -f1;`
filename=`echo $conf | cut -d"|" -f2;`
storeto=`echo $conf | cut -d"|" -f3;`
... the rest of your code goes here using the above variables ...
done
如果在文件中使用空格作为分隔符,那么每次迭代中的$conf将只是每个空格分隔的值,而不是获取行并将其拆分
使用awk可能有一种更优雅的方法来实现这一点,但这会起作用。因此,现在的结束脚本如下所示
#!/bin/sh
#wget -verbose -tries -timeout -dontcreatedirectories -dontsaveanything http://x output text to file
# update and upgrade packages and packages list
apt-get update
apt-get upgrade -y
# go to location where logs will be stored and servers list is updated from list stored on master server
cd /var/log
rm -f server.list
wget http://yourserver/server.list
# create a tmp file and if it is left behind from earlier results, empty it out
echo -e "\n" > wget.tmp
# these are the header texts for the results
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "WGET SCHEDULES" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
# this will make note of the time the tests are started
date "+START OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo " " >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
### for loop to read from server.list file like this http://msn.robots.ua|100mb.test|/dev/null
wgetcount=0
for conf in $(cat server.list); do
wgetcount=$((wgetcount+1))
server=`echo $conf | cut -d"|" -f1;`
filename=`echo $conf | cut -d"|" -f2;`
storeto=`echo $conf | cut -d"|" -f3;`
echo "Server # "$wgetcount >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
wget -v -r -t 2 -T 7 -nd -O $storeto $server/$filename 2>> wget.tmp
grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
rm -f wget.tmp
echo " " >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
done
# record the end time of tests
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
date "+END OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
#mail results to yourself
echo "Subject: $(hostname -s| tr a-z A-Z)" Wget Results v.2.0 Sendmail "$(date +%x)" ---- "$(date "+%A")" "$(date "+%X") ("$(date "+%Z")") ::: Week "$(date "+%V")" """ | cat - $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log | /usr/lib/sendmail -t youremailaddress
结果相同,可更新,代码更简洁;))谢谢彼得太好了!它的作品,我增加了一点增量后,做了一些阅读:)非常感谢!由于某种原因,我不能做+1,因为我的代表太低了。。但是你的建议起了作用。
#!/bin/sh
#wget -verbose -tries -timeout -dontcreatedirectories -dontsaveanything http://x output text to file
# update and upgrade packages and packages list
apt-get update
apt-get upgrade -y
# go to location where logs will be stored and servers list is updated from list stored on master server
cd /var/log
rm -f server.list
wget http://yourserver/server.list
# create a tmp file and if it is left behind from earlier results, empty it out
echo -e "\n" > wget.tmp
# these are the header texts for the results
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "WGET SCHEDULES" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
# this will make note of the time the tests are started
date "+START OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
echo " " >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
### for loop to read from server.list file like this http://msn.robots.ua|100mb.test|/dev/null
wgetcount=0
for conf in $(cat server.list); do
wgetcount=$((wgetcount+1))
server=`echo $conf | cut -d"|" -f1;`
filename=`echo $conf | cut -d"|" -f2;`
storeto=`echo $conf | cut -d"|" -f3;`
echo "Server # "$wgetcount >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
wget -v -r -t 2 -T 7 -nd -O $storeto $server/$filename 2>> wget.tmp
grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
rm -f wget.tmp
echo " " >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
done
# record the end time of tests
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
date "+END OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log
#mail results to yourself
echo "Subject: $(hostname -s| tr a-z A-Z)" Wget Results v.2.0 Sendmail "$(date +%x)" ---- "$(date "+%A")" "$(date "+%X") ("$(date "+%Z")") ::: Week "$(date "+%V")" """ | cat - $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log | /usr/lib/sendmail -t youremailaddress