我正在编写一个 python 脚本来获取带有日期的 apt-get 命令历史记录。传递 IP 地址和密码作为参数,然后通过 SSH 连接到主机。在命令列表中,我们有一个 Linux 命令列表,并使用 for 循环执行这些命令。有没有命令可以获取最近的 linux 更新日期?我尝试了“history 5 | tail | grep 'apt-get update'”,但 python 无法打印此命令的输出。
import paramiko
import sys
hostname = sys.argv[1]
username = "root"
password = sys.argv[2]
commands = [
"pwd",
"history 5 | tail | grep 'apt-get update'",
"ls -l /var/lib/apt/periodic/update-success-stamp | awk '{print $6" "$7" "$8}'",
"tail -n 25 /var/log/apt/history.log"
]
# initialize the SSH client
client = paramiko.SSHClient()
# add to known hosts
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=hostname, username=username, password=password)
except:
print("[!] Cannot connect to the SSH Server")
exit()
# execute the commands
for command in commands:
print("="*50, command, "="*50)
stdin, stdout, stderr = client.exec_command(command)
print(stdout.read().decode())
err = stderr.read().decode()
if err:
print(err)
del stdin, stdout, stderr
答案1
bash
历史记录通常不会记录命令的时间戳(根据我的经验,设置变量HISTTIMEFORMAT
不会做任何有用的事情)。您可以检查时间戳
/var/log/dpkg.log
(这是找出上次安装/升级/删除软件包的快速方法)或检查内容/var/log/apt/history.log
(您可以对其进行解析以获取有关执行了哪些操作以及何时执行的更多详细信息) )。
/var/log/apt/history.log
例如,我的系统之一的最后两条记录是:
Start-Date: 2022-11-09 19:51:36
Commandline: apt -m -u dist-upgrade
Install: libjxl0.7:amd64 (0.7.0-5+b1, automatic), libhwy1:amd64 (1.0.2-4, automatic), libicu72:amd64 (72.1-2, automatic)
Upgrade: libvulkan1:amd64 (1.3.224.0-1, 1.3.231.1-1), libvulkan1:i386 (1.3.224.0-1, 1.3.231.1-1), libglib2.0-dev-bin:amd64 (2.74.1-1, 2.74.1-2), udev:amd64 (252-3, 252.1-1), source-highlight:amd64 (3.1.9-4.2, 3.1.9-4.2+b1), libglx-mesa0:amd64 (22.2.2-1, 22.2.3-1), libglx-mesa0:i386 (22.2.2-1, 22.2.3-1), systemd-container:amd64 (252-3, 252.1-1), libpciaccess-dev:amd64 (0.17-1, 0.17-2), libglib2.0-bin:amd64 (2.74.1-1, 2.74.1-2), vulkan-tools:amd64 (1.3.224.0+dfsg1-1, 1.3.231.1+dfsg1-1), libglib2.0-dev:amd64 (2.74.1-1, 2.74.1-2), libglib2.0-doc:amd64 (2.74.1-1, 2.74.1-2), pristine-tar:amd64 (1.49, 1.50), ffmpeg-doc:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libpam-systemd:amd64 (252-3, 252.1-1), libboost-iostreams1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), libgbm1:amd64 (22.2.2-1, 22.2.3-1), libgbm1:i386 (22.2.2-1, 22.2.3-1), libpciaccess0:amd64 (0.17-1, 0.17-2), libpciaccess0:i386 (0.17-1, 0.17-2), systemsettings:amd64 (4:5.26.2-1, 4:5.26.3-1), kwrited:amd64 (4:5.26.2-1, 4:5.26.3-1), liboxygenstyleconfig5-5:amd64 (4:5.26.2-1, 4:5.26.3-1), libdrm-nouveau2:amd64 (2.4.113-2, 2.4.114-1), libdrm-nouveau2:i386 (2.4.113-2, 2.4.114-1), libsystemd0:amd64 (252-3, 252.1-1), libsystemd0:i386 (252-3, 252.1-1), libavdevice59:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), plasma-discover-common:amd64 (5.26.2-1, 5.26.3-1), ffmpeg:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), systemd-journal-remote:amd64 (252-3, 252.1-1), kde-style-oxygen-qt5:amd64 (4:5.26.2-1, 4:5.26.3-1), libudev-dev:amd64 (252-3, 252.1-1), libboost-filesystem1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), libboost-thread1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), libglib2.0-data:amd64 (2.74.1-1, 2.74.1-2), kactivitymanagerd:amd64 (5.26.2-1, 5.26.3-1), systemd:amd64 (252-3, 252.1-1), libudev1:amd64 (252-3, 252.1-1), libudev1:i386 (252-3, 252.1-1), libpostproc56:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libgl1-mesa-dev:amd64 (22.2.2-1, 22.2.3-1), libgl1-mesa-dri:amd64 (22.2.2-1, 22.2.3-1), libgl1-mesa-dri:i386 (22.2.2-1, 22.2.3-1), libosmesa6:amd64 (22.2.2-1, 22.2.3-1), libosmesa6:i386 (22.2.2-1, 22.2.3-1), libdrm-common:amd64 (2.4.113-2, 2.4.114-1), libboost-locale1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), libswscale-dev:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libboost-python1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), ksshaskpass:amd64 (4:5.26.2-1, 4:5.26.3-1), libavcodec59:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libavutil57:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), liboxygenstyle5-5:amd64 (4:5.26.2-1, 4:5.26.3-1), libswscale6:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libavogadro2-1:amd64 (1.97.0-2, 1.97.0-3), dh-elpa-helper:amd64 (2.0.14, 2.0.15), kmenuedit:amd64 (4:5.26.2-1, 4:5.26.3-1), libicu-dev:amd64 (71.1-3, 72.1-2), libavutil-dev:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libglapi-mesa:amd64 (22.2.2-1, 22.2.3-1), libglapi-mesa:i386 (22.2.2-1, 22.2.3-1), libboost-wave1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), libglib2.0-0:amd64 (2.74.1-1, 2.74.1-2), libglib2.0-0:i386 (2.74.1-1, 2.74.1-2), libboost-program-options1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), btrfs-progs:amd64 (6.0-1, 6.0.1-1), libdrm-amdgpu1:amd64 (2.4.113-2, 2.4.114-1), libdrm-amdgpu1:i386 (2.4.113-2, 2.4.114-1), libdrm-radeon1:amd64 (2.4.113-2, 2.4.114-1), libdrm-radeon1:i386 (2.4.113-2, 2.4.114-1), libdrm2:amd64 (2.4.113-2, 2.4.114-1), libdrm2:i386 (2.4.113-2, 2.4.114-1), libsystemd-shared:amd64 (252-3, 252.1-1), libswresample4:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libboost-regex1.74.0:amd64 (1.74.0-17, 1.74.0-17+b1), mupdf:amd64 (1.20.3+ds1-1+b1, 1.21.0+ds1-1), systemd-sysv:amd64 (252-3, 252.1-1), libavformat59:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libegl-mesa0:amd64 (22.2.2-1, 22.2.3-1), libegl-mesa0:i386 (22.2.2-1, 22.2.3-1), mupdf-tools:amd64 (1.20.3+ds1-1+b1, 1.21.0+ds1-1), dwarves:amd64 (1.24-2, 1.24-3), mesa-vdpau-drivers:amd64 (22.2.2-1, 22.2.3-1), libavcodec-dev:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), bluedevil:amd64 (4:5.26.2-1, 4:5.26.3.1-1), pahole:amd64 (1.24-2, 1.24-3), libdrm-intel1:amd64 (2.4.113-2, 2.4.114-1), libdrm-intel1:i386 (2.4.113-2, 2.4.114-1), libkworkspace5-5:amd64 (4:5.26.2-1, 4:5.26.3-1), libsource-highlight4v5:amd64 (3.1.9-4.2, 3.1.9-4.2+b1), icu-devtools:amd64 (71.1-3, 72.1-2), libswresample-dev:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2), libavfilter8:amd64 (10:5.1.2-dmo1, 10:5.1.2-dmo2)
Remove: libflightcrew0v5:amd64 (0.9.3+dfsg-1+b4), libaqsis1:amd64 (1.8.2+git20210815-1+b1), flightcrew:amd64 (0.9.3+dfsg-1+b4), aqsis:amd64 (1.8.2+git20210815-1+b1)
End-Date: 2022-11-09 19:54:06
Start-Date: 2022-11-09 19:59:28
Commandline: apt-get -u install linux-image-amd64 linux-headers-amd64
Install: linux-image-6.0.0-3-amd64:amd64 (6.0.7-1, automatic), linux-headers-6.0.0-3-amd64:amd64 (6.0.7-1, automatic), linux-headers-6.0.0-3-common:amd64 (6.0.7-1, automatic)
Upgrade: linux-headers-amd64:amd64 (6.0.6-2, 6.0.7-1), linux-image-amd64:amd64 (6.0.6-2, 6.0.7-1)
End-Date: 2022-11-09 20:07:29
这些记录方便地采用“段落”格式(即用空行分隔),因此可以轻松地使用awk -v RS=""
或进行处理perl -00
。
注意:apt历史日志仅记录apt和朋友所做的事情。它不记录任何dpkg
单独完成的事情。然而,dpkg.log 文件记录了 dpkg 本身所做的任何事情和apt/apt-get/aptitude 或任何其他最终执行 dpkg 来安装/升级/删除软件包的工具所做的任何事情。
换句话说,dpkg -i /path/to/package.deb
ordpkg --purge packagename
会被记录在 中/var/log/dpkg.log
,但不会被记录在 中(因为它根本 /var/log/apt/history.log
不使用)。或者将被记录在两者中(因为两者都用于执行安装/删除/等操作)。apt
apt-get install package
apt purge package
apt
apt-get
dpkg
答案2
我没有,所以我将使用:paramiko
给出相邻的答案。或者只是通过ting或来抓取 bash 历史文件。是一个内置的 shell,与其他可执行文件不同,它以交互方式运行,并且显示命令执行历史记录而不是时间戳。修辞评论:这看起来像是可以使用 shell 脚本运行的东西。subprocess
subprocess.call("bash -i -c 'history -r; history'", shell=True)
cat
~/.bash_history
~/.history
history
PATH
其他一些研究链接: