定子1个shell脚本,想要自动统计访问服务器的源IP地址信息,并将源IP地址自动打印到日志文件中,手动执行时可以正常打印出源IP地址,当使用crontab执行时,一直不打印源IP地址信息,只有msgLog里的日志打印成功了。
脚本内容如下:
#!/bin/bash
msgLog()
{
NowTime="`date '+%Y-%m-%d %H:%M:%S'`"
echo "[$NowTime]: $1" | tee -a "$2"
}
#过滤出192.168.2.102:1100
msgLog "Get the Source IP to access 192.168.2.102:1100......" "/tmp/lilei/192.168.2.102_1100.log"
ss -ant | awk '$4 ~ /192.168.2.102:1100/ {print $5}' | cut -d ":" -f 1 | sort | uniq | grep -v "*" >> /tmp/lilei/192.168.2.102_1100.log
相似问题