我打算创建并运行一个能检测最后一次用户登录的ansible剧本(或.sh脚本),然后,如果登录日期早于30天,请继续关闭服务器,否则请停止该剧本。
我对ansible的了解也是基础和脚本编写。 :(
我将在我的RHEL服务器6.x和7.x上执行此操作
last | grep test
test pts/1 127.0.0.1 Mon Sep 9 10:53 - 10:53 (00:00)
保存此输出,如果日期早于30天,请关闭服务器。我该如何将此输出设置为下一个命令的变量?
if [ "$(date +%D)" != "30" ];then
shutdown -h now
fi
答案 0 :(得分:0)
您应该尝试自己解决这个问题,但是您可以从这里开始:
Dim srcPdf As iText.Kernel.Pdf.PdfDocument = New iText.Kernel.Pdf.PdfDocument(New iText.Kernel.Pdf.PdfReader(srcFile))
Dim destPDF As New iText.Kernel.Pdf.PdfDocument(New iText.Kernel.Pdf.PdfWriter(destFile))
For contador = 1 To srcPdf.GetNumberOfPages
Dim srcPage = srcPdf.GetPage(contador)
Dim rotacion As iText.Kernel.Pdf.PdfNumber = srcPage.GetPdfObject().GetAsNumber(iText.Kernel.Pdf.PdfName.Rotate)
If IsNothing(rotacion) OrElse rotacion.IntValue = 0 Then
srcPdf.CopyPagesTo(contador, contador, destPDF)
Continue For
End If
Dim destPage As iText.Kernel.Pdf.PdfPage = destPDF.AddNewPage(New iText.Kernel.Geom.PageSize(srcPage.GetPageSizeWithRotation))
If rotacion.IntValue = 180 Then
destPage.GetPdfObject().Put(iText.Kernel.Pdf.PdfName.Rotate, New iText.Kernel.Pdf.PdfNumber(180))
Else
destPage.GetPdfObject().Put(iText.Kernel.Pdf.PdfName.Rotate, New iText.Kernel.Pdf.PdfNumber(rotacion.IntValue + 180))
End If
destPage.SetIgnorePageRotationForContent(True)
Dim canvas As New iText.Kernel.Pdf.Canvas.PdfCanvas(destPage)
Dim pageCopy As iText.Kernel.Pdf.Xobject.PdfFormXObject = srcPage.CopyAsFormXObject(destPDF)
canvas.AddXObject(pageCopy, 0, 0)
destPage.GetPdfObject().Remove(iText.Kernel.Pdf.PdfName.Rotate)
Next
destPDF.Close()
srcPdf.Close()
我不会给您最后一点(如何检查user_last_login是否超过30天)。
提示(Ansible中有一些函数可以处理时间(基于Python):
- name: Check user logged in (30 days)
shell: last | grep test
register: user_last_login
ignore_errors: True
- name: Shut down server if no user login
shell: shutdown -h now
become: yes
when: user_last_login
答案 1 :(得分:0)
这就是我解决问题的方式
---
- name: Check last login
hosts: REDHAT
tasks:
- name: Last logged user
shell: last | grep myusername
register: output
ignore_errors: yes
- name: shutdown server
shell: shutdown -h now
become: yes
when: (output.stdout|int - ansible_date_time.epoch|int) < 2592000