我的任务是编写一个可以审核 mysql 登录的 bash 脚本。
如果您激活general_log
中的选项/etc/my.cnf
,您可以注册 mysql 执行的所有活动,并将其写入(在我的例子中)/var/lib/mysql/localhost.log
。
如果我cat
这样做,我会得到这一行:
2018-11-18T12:39:46.622298Z 5 Connect Access denied for user 'root'@'localhost' (using password: YES)
因此,考虑到这一点,我制作了这个 grep 语法(将变量更改为实际数字并且它有效!):
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied
我还制作了一个 grep 指令来计算这些登录的次数。
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied
它们都可以工作,而且据我所知,它们在技术上是正确的(经过拼写检查!)。
但我需要将它们烘焙到脚本中,以便我可以从用户那里获取变量并将它们放入正确的脚本中,但到目前为止我无法使它们工作:当bash
尝试阅读时,我收到了错误,您可以在下面看到第一个变量。
到目前为止的代码:
#!/bin/bash
echo "Year input: "
read -r year
if [[ $year -gt 2020 ]];
then
echo "Incorrect year input."
exit 1
else
echo "Month input: "
read -r month
if [[ $month -gt 12 ]];
then
echo "Incorrect month input."
exit 1
else
echo "Day input: "
read -r day
if [[ $day -gt 31 ]];
then
echo "Incorrect day input."
exit 1
else
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1 " times."
echo ''
grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$year"-"$month"-"$day" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1 " times."
fi
fi
fi
exit 0
我得到的错误(原文:下面的图片1):
[root@localhost ~]# sh /medla/sf_compartida/two.sh
Year input:
2018
': not a valid identifiersh: line 3: read: `year
/media/sf_compartida/two.s: line 34: syntax error: unexpected end of file
[root@localhost ~]#
编辑
新代码:
#!/bin/bash
read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d")
then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]
then
echo "invalid year" >&2
exit 1
else
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -v denied
logbien=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -cv denied)
echo "Correct logins: "echo "$logbien" | wc -1" times."
echo ''
grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep denied
logmal=$(grep "$date" /var/lib/mysql/localhost.log | grep Connect | grep -c denied)
echo "Incorrect logins: "echo "$logmal" | wc -1" times."
fi
我得到的错误(原文:下面的图片2):
[root@localhost ~]# sh /media/sf_compartida/two2.sh
Enter the date (YYYY-mm-dd): 2018-11-20
': not a valid identifier.sh: line 2: read: `date
/media/sf compartida/two2.sh: line 9: syntax error in conditional expression
'media/sf_compartida/two2.sh: line 9: syntax error near `]]
'media/sf_compartida/two2.sh: line 9: `if [[ $year -gt 2020 ]]
[root@localhost ~]#
答案1
这不是一个答案,而是一些代码审查:我会提供这个来改进您的日期输入:让用户立即输入日期,并使用命令date
来验证和规范用户的输入。
read -r -p "Enter the date (YYYY-mm-dd): " date
if ! date=$(date -d "$date" "+%Y-%m-%d"); then
echo "Error: invalid date" >&2
exit 1
fi
year=${date%%-*}
if [[ $year -gt 2020 ]]; then echo "invalid year" >&2; exit 1; fi
# then: grep "$date" logfile ...