因此,对于作为工作一部分的项目,我需要获取 Linux 更新的大小。
我使用以下命令:
aptitude search -SF '%p' --disable-columns '~U'
因此,我获得了需要更新的数据包的名称,但我只需要脚本的大小。对于 Ubuntu、Debian,我可以使用apt
或,对于 Centos、RedHat aptitude
,我也可以做同样的事情。yum
我在命令中搜索了一些内容man
,但找不到我想要的内容。有人能帮我吗?我以为这很简单,但也许我错过了什么。
答案1
您可以通过以下步骤实现此目的:
首先,apt-show-versions
使用以下命令在机器上安装包:
sudo apt-get install -y apt-show-versions
使用以下命令您可以获取可升级的软件包列表:
apt-show-versions -u
我们可以利用这个工具来apt-cache
恢复包的大小,让我们看看如何做到这一点:
for package in `/usr/bin/apt-show-versions -u | tr ' ' / | cut -f1,6 -d'/' | tr / =` ; do echo -n $package ; sudo apt-cache show $package | grep '^Size:' | cut -d: -f2 ; done | sort -k2 -n
上述命令将为您提供可升级包的列表及其按字节升序排列的大小。
如果您想了解每个步骤执行的操作,下面是上述命令的快速分解:
# Gives you a list of upgradable packages with their new available versions in format package_name=version
/usr/bin/apt-show-versions -u | tr ' ' / | cut -f1,6 -d'/' | tr / =
# Get the size (in bytes) of each package that is upgradable
sudo apt-cache show $package | grep '^Size:'
# Display the output in ascending order
sort -k2 -n
上述命令的输出示例如下:
galoget@hackem:~$ for package in `/usr/bin/apt-show-versions -u | tr ' ' / | cut -f1,6 -d'/' | tr / =` ; do echo -n $package ; sudo apt-cache show $package | grep '
^Size:' | cut -d: -f2 ; done | sort -k2 -n
initramfs-tools:all=0.136ubuntu6.6 9248
systemd-sysv:amd64=245.4-4ubuntu3.7 10280
software-properties-common:all=0.98.9.5 10616
initramfs-tools-bin:amd64=0.136ubuntu6.6 10904
update-manager-core:all=1:20.04.10.7 11260
python-apt-common:all=2.0.0ubuntu0.20.04.5 17052
linux-base:all=4.5ubuntu3.6 17780
libasound2-data:all=1.2.2-2.1ubuntu2.4 20040
ubuntu-release-upgrader-core:all=1:20.04.33 23812
python3-software-properties:all=0.98.9.5 25124
alsa-ucm-conf:all=1.2.2-1ubuntu0.8 26484
systemd-timesyncd:amd64=245.4-4ubuntu3.7 28104
libprocps8:amd64=2:3.3.16-1ubuntu2.2 33016
libnetplan0:amd64=0.102-0ubuntu1~20.04.2 34608
libpam-runtime:all=1.3.1-5ubuntu4.2 37276
python3-update-manager:all=1:20.04.10.7 38176
libpam-modules-bin:amd64=1.3.1-5ubuntu4.2 41180
initramfs-tools-core:all=0.136ubuntu6.6 47744
libxmlb1:amd64=0.1.15-2ubuntu1~20.04.1 50520
libpam0g:amd64=1.3.1-5ubuntu4.2 55372
libudev1:amd64=245.4-4ubuntu3.7 77604
libnss-systemd:amd64=245.4-4ubuntu3.7 96096
python3-distupgrade:all=1:20.04.33 104100
netplan.io:amd64=0.102-0ubuntu1~20.04.2 117380
update-notifier-common:all=3.192.30.8 132392
python3-apt:amd64=2.0.0ubuntu0.20.04.5 154164
libpam-systemd:amd64=245.4-4ubuntu3.7 185812
apt-utils:amd64=2.0.6 216244
procps:amd64=2:3.3.16-1ubuntu2.2 232168
sosreport:amd64=4.1-1ubuntu0.20.04.2 245052
libpam-modules:amd64=1.3.1-5ubuntu4.2 260364
libsystemd0:amd64=245.4-4ubuntu3.7 270248
open-iscsi:amd64=2.0.874-7.1ubuntu6.2 283160
tmux:amd64=3.0a-2ubuntu0.3 292044
libasound2:amd64=1.2.2-2.1ubuntu2.4 334416
cloud-init:all=21.2-3-g899bfaa9-0ubuntu2~20.04.1 456404
open-vm-tools:amd64=2:11.2.5-2ubuntu1~ubuntu20.04.1 603812
openssl:amd64=1.1.1f-1ubuntu2.4 620068
libapt-pkg6.0:amd64=2.0.6 835288
ubuntu-advantage-tools:amd64=27.1~20.04.1 837368
apt:amd64=2.0.6 1295960
libssl1.1:amd64=1.1.1f-1ubuntu2.4 1319180
udev:amd64=245.4-4ubuntu3.7 1366316
systemd:amd64=245.4-4ubuntu3.7 3805892
snapd:amd64=2.49.2+20.04 30555628
如果您只想要包的大小并将它们添加以获取以字节为单位的总大小,则可以使用以下命令:
galoget@ip-172-31-35-106:~$ for package in `/usr/bin/apt-show-versions -u | tr ' ' / | cut -f1,6 -d'/' | tr / =` ; do sudo apt-cache show $package | grep '^Size: ' | cut -d " " -f2 ; done | sort -k2 -n | awk '{ SUM += $1} END { print SUM }'
45245756
在我的示例中,更新的总大小将是45245756
字节,即 45.3 兆字节,可以通过运行以下命令进行验证:
galoget@hackem:~$ sudo apt upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
distro-info
The following packages will be upgraded:
alsa-ucm-conf apt apt-utils cloud-init initramfs-tools initramfs-tools-bin initramfs-tools-core libapt-pkg6.0 libasound2 libasound2-data libnetplan0 libnss-systemd
libpam-modules libpam-modules-bin libpam-runtime libpam-systemd libpam0g libprocps8 libssl1.1 libsystemd0 libudev1 libxmlb1 linux-base netplan.io open-iscsi
open-vm-tools openssl procps python-apt-common python3-apt python3-distupgrade python3-software-properties python3-update-manager snapd software-properties-common
sosreport systemd systemd-sysv systemd-timesyncd tmux ubuntu-advantage-tools ubuntu-release-upgrader-core udev update-manager-core update-notifier-common
45 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 45.3 MB of archives.
After this operation, 11.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
如您所见,尺寸匹配,因此命令完美运行。
答案2
因此,我们和朋友一起更改了一些内容。提醒:我发送了一个 python 脚本来获取 apt 和 yum 更新的大小。
以下是 Python 代码:
def get_upgradable() :
if os.path.isfile("/usr/bin/apt"):
command = "apt list --upgradable 2>/dev/null | cut -d'/' -f1"
elif os.path.isfile("/usr/bin/yum"):
#command = "yum check-update 2>/dev/null | grep \".x86_64\" | cut -d' ' -f1"
command="yum check-update | awk '/\S+\s+[0-9]\S+\s+\S+/ {print $1 }'"
else :
raise PkgManager("Not found package manager")
return None
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, encoding='utf8')
packages_name = []
for out in process.stdout :
packages_name.append(out[:-1])
return packages_name
def get_yum_size(*args, return_somme=True) :
command = "yum info {pkg} | egrep \"(Taille|Size)\""
factor = {"k" : 1000,
"M" : 1000000,
"G" : 1000000000}
somme = 0
sizes = []
for pkg in args :
lines = subprocess.Popen(command.format(pkg=pkg),
shell=True,
stdout=subprocess.PIPE,
encoding='utf8',
).stdout.readlines()
infos = lines[-1][:-1].split(' ')
size = float(infos[-2])*factor[infos[-1]]
sizes.append(size)
somme += size
if return_somme : return somme
return sizes
def get_apt_size(*args, return_somme=True) :
command = "apt-cache --no-all-versions show {pkg} | grep \"^Size\" | cut -d' ' -f2"
sizes = []
somme = 0
for pkg in args :
size = int(subprocess.Popen(command.format(pkg=pkg),
shell=True,
stdout=subprocess.PIPE,
encoding='utf8'
).stdout.read())
sizes.append(size)
somme += size
if return_somme : return somme
return sizes
def get_size(*args, **kwargs) :
if os.path.isfile("/usr/bin/apt"):
return get_apt_size(*args, **kwargs)
elif os.path.isfile("/usr/bin/yum"):
return get_yum_size(*args, **kwargs)
else :
raise PkgManager("Not found package manager")
然后在主代码中调用它:
get_size(*get_upgradable())
如果你尝试,输出将是:9780000000
这就是所有更新的大小(以字节为单位)。