比较 /etc/shadow 文件中的过期日期和今天的日期,找出已过期的账户。

huangapple go评论63阅读模式
英文:

compare the expiration date in /etc/shadow to today's date and figure out which accounts have expired

问题

root@node033:~# vi exppass
root@node033:~# bash exppass
exppass: line 7: syntax error: unexpected end of file
root@node033:~# cat exppass
cat /etc/shadow |
while IFS=":" read col1 col2 col3 col4 col5 col6 col8;
do
echo $col3 $col8 
if [ $expire -lt $today ];
then

我正在尝试使用日期和时间进行比较来删除过期密码。
调整您的脚本以实现类似以下操作:

if [ $expire -lt $today ]; then        
#删除密码

我认为上面的`$expire`等同于您正在读取的列之一,并且您可以通过类似`today=$(date +%s)`的方式获取今天的日期。
英文:
root@node033:~# vi exppass
root@node033:~# bash exppass
exppass: line 7: syntax error: unexpected end of file
root@node033:~# cat exppass
cat /etc/shadow |
while IFS=":" read col1 col2 col3 col4 col5 col6 col8;
do
echo $col3 $col8 
if [ $expire -lt $today ];
then

I am trying to remove the expiration passwd using to compare the date and time.
adjust your script to do something along the lines:

if [ $expire -lt $today ]; then        
#delete the password

I think $expire above is equivalent to one of the columns you're reading. and you can get today's date by doing something like today=$(date +%s)

答案1

得分: 0

如果你愿意使用awkbash来解决问题,而不是在循环中进行复杂的操作,可以使用以下命令:

awk -F: -v today=$(( $( date "+%s" ) / 86400 ))  '$8!=""{print $1, today-$8}' /etc/shadow

解释:

  • -F: 定义了输入字段分隔符为冒号。
  • -v today=$(( $( date "+%s" ) / 86400 )) 表达了今天的日期距离纪元以来的天数(这是/etc/shadow中使用的日期格式),并将其赋给了awk变量today

现在是awk的逻辑部分:

  • $8!="" 如果第8个字段(账户到期日期)不为空,则 {print $1, today-$8} 打印用户名和今天与到期日期之间的天数差。如果日期在过去,你会得到一个正值,如果在未来,会得到一个负值。

编辑

根据上面的混乱内容,看起来你想检查密码和账户到期情况:

以下命令可以实现这一点:

awk -F: -v today=$(( $( date "+%s" ) / 86400 ))  '$3!="" || $8!=""{printf "%s\tpw: %s\tacc: %s\n", $1, today - $3, today-$8}' /etc/shadow

现在第2个字段显示密码的年龄,第3个字段显示账户到期日期。如果这还不是你想要的,请重新表述你的问题,以反映你实际想要的内容。

英文:

If you're happy w/ an awk & bash solution rather than doing the hard lifting in a loop:

awk -F: -v today=$(( $( date "+%s" ) / 86400 ))  '$8!=""{print $1, today-$8}' /etc/shadow

Explanation:
-F: defines the input field separator to be a colon.

-v today=$(( $( date "+%s" ) / 86400 )) expresses today's date in days since epoch (which is the format used in /etc/shadow) and assigns it to an awk variable called today.

Now for the awk logic:

$8!="" if the 8th field (Account Expiry) isn't unset, {print $1, today-$8} print the username and the difference between today and the expiry date in days. If the date lies in the past, you get a positive value, if it's in the future, a negative one.

Edit:

Looking at the jumble above it appears that you're trying to check for both password and account expiry:

This should do:

awk -F: -v today=$(( $( date "+%s" ) / 86400 ))  '$3!="" || $8!=""{printf "%s\tpw: %s\tacc: %s\n", $1,today - $3, today-$8}' /etc/shadow

The 2nd field now shows the age of the password, the 3rd the account expiration. If this still isn't what you're after you'll need to sit down and rephrase your question so it reflects what you're actually after.

huangapple
  • 本文由 发表于 2023年2月18日 02:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定