我编写了一个 bash 脚本来使用 telnet 发送电子邮件。我将它安装在运行busyBox(具有灰壳)的TS-7260 上。
Bash 和 Ash 之间有些不同,我无法弄清楚为什么以下内容不起作用。这与我将回声传输到 telnet 的方式有关。这是脚本:
#!/bin/ash
# Snag all the error messages from a given date, open a telnet connection to an outgoing mail server, stick the logs in an email, and send it.
# Tue Jul 2 14:06:12 EDT 2013
# TMB
# Tue Jul 9 17:12:29 EDT 2013
# Grepping the whole error file for WARNING and the piping it to a grep for the date took about four minutes to complete on the gateway. This will only get longer and the file will only get bigger as time goes by.
# Using tail to get the last 5000 lines, I get about three days of errors (2000 of them are from one day, though)
# Getting 5000 lines, then searching them by WARNING and then DATE took 15 seconds on the gateway.
yesterdayDate=$(./getYesterday)
warningLogs=$(tail -5000 /mnt/sd/blah.txt | grep WARNING | grep "$yesterdayDate")
sleep 30
{
sleep 5
echo "ehlo blah.com"
sleep 5
echo "auth plain blah"
sleep 5
echo "mail from: blah@blah.com"
sleep 5
echo "rcpt to: me@blah.com"
sleep 5
echo "data"
sleep 5
echo "Hi!"
sleep 1
echo "Here are all the warnings and faults from yesterday:"
sleep 1
echo "$yesterdayDate"
sleep 1
echo "NOTE: All times are UTC."
sleep 1
echo ""
sleep 1
echo "$warningLogs"
sleep 10
echo ""
sleep 1
echo "Good luck,"
sleep 1
echo "The Robot"
sleep 5
echo "."
sleep 20
echo "quit"
sleep 5
} | telnet blah.com port
exit
我也尝试在管道之前使用普通括号。我已经阅读了 ash 的手册页,但仍在做一些愚蠢的事情。我怀疑这是某种正在进行的子流程业务。
顺便说一句,这在 bash 中效果很好。
提前致谢!
注意——我将脚本简化为:
echo "quit" | telnet blah.com port
它完全符合您在 bash 中的期望,但我看不到在 ash 中发生任何事情。将 echo 替换为“sleep 10”表示 sleep 作为进程运行,但不是 telnet。