获取服务器基本信息脚本

对KVM虚拟化友好,Hyperv不友好,其余虚拟化未知~

#!/usr/bin/env python3
import subprocess
import json

def exec_cmd(cmd):
    return subprocess.getoutput(cmd)
prod=exec_cmd('dmidecode -q -t 1 ')
hostname=exec_cmd('uname -n')
kernel=exec_cmd('uname -r')
os=exec_cmd('cat /etc/redhat-release')
cpu_type = exec_cmd("grep 'model name' /proc/cpuinfo | uniq ").split(':')[1].strip()
cpu_pyc = exec_cmd("grep 'physical id' /proc/cpuinfo | sort -u | wc -l")
cpu_cores_each =exec_cmd( "grep 'cpu cores' /proc/cpuinfo | uniq").split(':')[1]
mem=exec_cmd('dmidecode -q -t 17 ')
prod_dic = {
    "Manufacturer": "manufacturer",
    "Product Name": "pod_name",
    "Serial Number": "sn",  
}
mem_dic={
    'Size': 'capacity',
    'Locator': 'slot',
    'Type': 'model',
    'Speed': 'speed',
    'Manufacturer': 'manufacturer',
    'Serial Number': 'sn',
}
base_dic={
    "hostname":hostname,
    "kernel":kernel,
    "os":os,
    "cpu_type":cpu_type,
    "cpu_pyc":cpu_pyc,
    "cpu_cores_each":cpu_cores_each
}
for line in prod.splitlines():
    line=line.strip()
    try:
        k,v=line.split(':')
        if k in prod_dic:
            base_dic[prod_dic.get(k)] = v
    except ValueError as e:
        print(e)
for line in mem.splitlines():
    line=line.strip()
    try:
        k,v=line.split(':')
        if k in mem_dic:
            base_dic[mem_dic.get(k)] = v
    except ValueError as e:
        print(e)
base_dic=json.dumps(base_dic,indent=4)
print(base_dic)


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇