AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 554042
Accepted
Santosh Garole
Santosh Garole
Asked: 2019-11-26 05:12:24 +0800 CST2019-11-26 05:12:24 +0800 CST 2019-11-26 05:12:24 +0800 CST

我们如何检查操作系统版本和要在 Linux 服务器上应用的安全补丁的数量?

  • 772

我编写了一个 bash 脚本,它检查操作系统版本并根据操作系统分布计算可用的安全补丁。

这里的查询是脚本不适用于 Ubuntu OS。脚本如下:

#!/bin/bash


# Detecting OS Distribution
if [ -f /etc/os-release ]; then
  . /etc/os-release
  OS=$PRETTY_NAME

elif [ -f /etc/redhat-release ]; then

   REDHAT=$(cat /etc/redhat-release)

else
   echo " OS is not supported "
fi

#Check how many packages for security and available to update
if [[ $OS == *"Red Hat"* ]] || [[ $OS == *"Amazon Linux"* ]] || [[ $OS == 
    *"CentOS"*  ]] || [[ $REDHAT == *"Red Hat"*  ]] ; then

    SECPKGCNT=$(sudo yum list-security --security | awk 'NR>2 {print last} 
{last=$0}' | wc -l)
    echo "${OS},${REDHAT},${SECPKGCNT}"

elif [[ $OS == *"Ubuntu"* ]] ;then

    SECPKGCOUNT=$(sudo apt list --upgradable 2>/dev/null | grep "\-security" 
    | wc -l)
    echo "${OS},${SECPKGCOUNT}"
   else
  echo " No security packages to update"
   fi

ubuntu机器中的O/p:

root@ip-XXXX:~# sh -x getos.sh
+ [ -f /etc/os-release ]
+ . /etc/os-release
+ NAME=Ubuntu
+ VERSION=14.04.5 LTS, Trusty Tahr
+ ID=ubuntu
+ ID_LIKE=debian
+ PRETTY_NAME=Ubuntu 14.04.5 LTS
+ VERSION_ID=14.04
+ HOME_URL=http://www.ubuntu.com/
+ SUPPORT_URL=http://help.ubuntu.com/
+ BUG_REPORT_URL=http://bugs.launchpad.net/ubuntu/
+ OS=Ubuntu 14.04.5 LTS
+ [[ Ubuntu 14.04.5 LTS = *Red Hat* ]]
getos.sh: 29: getos.sh: [[: not found
+ [[ Ubuntu 14.04.5 LTS = *Amazon Linux* ]]
getos.sh: 29: getos.sh: [[: not found
+ [[ Ubuntu 14.04.5 LTS = *CentOS* ]]
getos.sh: 29: getos.sh: [[: not found
+ [[ = *Red Hat* ]]
getos.sh: 29: getos.sh: [[: not found
+ [ Ubuntu 14.04.5 LTS = *Ubuntu* ]
getos.sh: 34: [: Ubuntu: unexpected operator
+ echo  No security packages to update
No security packages to update

在上面的 ubuntu 系统中,脚本没有按预期工作,我的意思是它应该打印操作系统版本和安全补丁的数量。

bash shell-script
  • 1 1 个回答
  • 1158 Views

1 个回答

  • Voted
  1. Best Answer
    terdon
    2019-11-26T06:01:53+08:002019-11-26T06:01:53+08:00

    你有两个主要问题。首先,您正在使用 运行脚本sh,而不是使用 .bash不支持的bashbash 功能(构造) 。所以你需要运行或者只是它是可执行的。[[shbash getos.sh./getos.sh

    接下来,您不能随意中断命令,您需要在控制运算符上中断。所以这将失败:

    SECPKGCOUNT=$(sudo apt list --upgradable 2>/dev/null | grep "\-security" 
    | wc -l)
    

    虽然这会起作用:

    SECPKGCOUNT=$(sudo apt list --upgradable 2>/dev/null | grep "\-security" | 
    wc -l)
    

    然后,几个小问题。你的第一个if ... elif .. else意味着如果/etc/os-release存在,你永远不会做剩下的。因此,您永远不会遇到同时设置OS和的情况REDHAT。但是,您似乎期待两者,而且我认为没有理由/etc/os-release会从 Red Hat 系统中丢失。所以我认为你可能想要分开if而不是if ... elif.

    最后,您的一些命令(theawk和 the grep | wc)可以简化,您正在创建实际上不需要的变量,并且可以通过以下方式使整个事情变得更简单:

    #!/bin/bash
    
    if [ -f /etc/os-release ]; then
      . /etc/os-release
    fi
    if [ -f /etc/redhat-release ]; then
      REDHAT=$(cat /etc/redhat-release)
    fi
    
    if [[ $PRETTY_NAME == *"Red Hat"* ]] || [[ $PRETTY_NAME == *"Amazon Linux"* ]] ||
       [[ $PRETTY_NAME ==  *"CentPRETTY_NAME"* ]] || [[ $REDHAT == *"Red Hat"*  ]]; then
      SECPKGCNT=$(sudo yum list-security --security | awk 'NR>2 {k++}END{print k}')
      PRETTY_NAME="$PRETTY_NAME,$REDHAT";
    elif [[ "$PRETTY_NAME" == *"Ubuntu"* ]] ;then
      SECPKGCNT=$(sudo apt list --upgradable 2>/dev/null | grep -c "\-security")
    else
      echo " OS is not supported "
      exit
    fi
    
    echo "$PRETTY_NAME,$SECPKGCNT"
    
    • 2

相关问题

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • MySQL Select with function IN () with bash array

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve