我希望修改Linux登录脚本,以便当用户登录时,会通知他们accountExpires在ActiveDirectory上的日期,并随着日期的临近(60、30、15、5、1天等)发出越来越严重的警告。
有两件事让我悲伤。我希望ldapsearch只输出"accountExpires:######“,我需要将它从100纳秒间隔-自-1/1/1601 64位格式转换为日月-年份格式。
帮助!
发布于 2017-02-27 15:27:03
好吧,我经过了很多试验和错误的…后才发现的
#!/bin/bash
accountExpires=$(ldapsearch -d 0 -LLL -v -b "DC=<yourdomain>,DC=<yourdomainextension>" sAMAccountName=$(whoami) 2> /dev/null | grep -oP 'accountExpires: \K.*' | tail -n 1)
if [ accountExpires = "9223372036854775807" ]; then
echo "Account never expires."
else
timeInSeconds=$((( accountExpires / 10000000 )))
interval1601to1970=$((( 0 - $(date --date=1601-01-01 +%s) )))
timeSince1970=$((( $timeInSeconds - $interval1601to1970 )))
echo "Your account expires on: $(date --date @"$timeSince1970")"
fi发布于 2022-10-11 02:38:48
@JuanJimenez
使用脚本进行测试:
$ ./get-expired-date.sh 133169184000000000
Your account expires on: Fri Dec 30 23:52:48 WIB 2022使用powershell进行测试:
> cat .\getdate.ps1
[datetime]::fromfiletime(133169184000000000)
> .\getdate.ps1
31 December, 2022 7:00:00 AM与powershell相比,大约有7个小时的差异。总之谢谢你的剧本。
编辑:添加划时代的差异到您的脚本修复问题。
#!/bin/bash
accountExpires=$(ldapsearch -d 0 -LLL -v -b "DC=<yourdomain>,DC=<yourdomainextension>" sAMAccountName=$(whoami) 2> /dev/null | grep -oP 'accountExpires: \K.*' | tail -n 1)
#accountExpires=$1
if [ accountExpires = "9223372036854775807" ]; then
echo "Account never expires."
else
timeInSeconds=$((( accountExpires / 10000000 )))
interval1601to1970=$((( 0 - $(date --date=1601-01-01 +%s) )))
correctionEpoch=25632
timeSince1970=$((( $timeInSeconds - $interval1601to1970 + $correctionEpoch)))
echo "Your account expires on: $(date --date @"$timeSince1970")"
fihttps://stackoverflow.com/questions/42487242
复制相似问题