通过使用ubuntu命令,我有一个特定的输出格式,我想要JSON格式,但我想要使用bash实现。
下面我提到了JSON格式的例子
{
"kernel_version": "5.15.0-46-generic",
"operating_system": "Ubuntu 20.04.4 LTS (Focal Fossa)",
"os_architecture": "64 Bit OS",
"internal_ip_address": [
"6.6.6.6",
"7.7.7.7",
"8.8.8.8"
],
"external_ip_address": "9.9.9.9",
"total_number_of_cpu": "8",
"internet": "Connected",
"uptime": "up 5 hours, 37 minutes",
"hostname": "ubuntu",
"users": "dummy",
"date_and_time": "Thursday 10 January 1995 03:15:03 PM IST",
"time_zone": "Asia/Kolkata",
"memory": {
"total_RAM": {
"size1": "7.6",
"format1": "GB"
},
"used_RAM": {
"size2": "4.0",
"format2": "GB"
},
"cache_memory": {
"size3": "3.3",
"format3": "GB"
},
"total_swap_memory": {
"size4": "4.2",
"format4": "GB"
},
"free_swap_memory": {
"size5": "4.2",
"format5": "GB"
},
"total_used_swap_memory": {
"size6": "33",
"format6": "MB"
},
"RAM_usage_percentage": "42.86%",
"sec_storage_size": {
"size7": "38",
"format7": "GB"
},
"available_sec_storage": {
"size8": "21",
"format8": "GB"
},
"used_sec_storage": {
"size9": "15",
"format9": "GB"
},
"sec_storage_used_percentage": "43%"
},
"cpu": {
"cpu_load": "0.48%"
},
"top_cpu_consuming_proc": [
{
"user": "val1",
"pid": "0000",
"cpu": "22",
"command": "dummy1"
},
{
"user": "val2+",
"pid": "111",
"cpu": "33",
"command": "dummy2"
},
{
"user": "val3",
"pid": "2222",
"cpu": "44",
"command": "dummy3"
}
]
}
我已经尝试了以下bash代码:
#!/bin/bash
echo {"kernel_version": "$(uname -r)",
"operating_system": "$([ -f /etc/os-release ] && echo $(egrep -w "NAME|VERSION" /etc/os-release|awk -F= '{ print $2 }'|sed 's/"//g') $tecreset|| cat /etc/system-release printf"n")",
"os_architecture": "$(arch | grep x86_64 &> /dev/null && printf "64 Bit OSn" $tecreset|| printf " 32 Bit ")",
"internal_ip_address": $(hostname -I | awk '
BEGIN { ORS = ""; print "[" }
{ printf "%s"%s", "%s", "%s"",
separator, $1, $2, $3, $11
separator = ", "
}
END { print "]" }';),
"external_ip_address": "$(curl -s ipecho.net/plain;echo)",
"total_number_of_cpu": "$(grep processor /proc/cpuinfo | wc -l)",
"internet": "$(ping -c 1 google.com &> /dev/null && echo -e "Connected" || echo "Disconnected")",
"uptime": "$(uptime -p)",
"hostname": "$(hostname)",
"users": "$(users)",
"date_and_time": "$(date)",
"time_zone": "$(cat /etc/timezone)",
"memory": {"total_RAM": $(jo -a $(free -h | awk 'NR==2{printf "%sn", $2 }' | sed 's/Gi/GB/i' | awk '{ gsub(/([[:alpha:]]+|[[:digit:].-]+|[^[:alnum:].-]+)/,"&n",$0) ; printf $0}')), "used_RAM": "$(free -h | awk 'NR==2{printf "%sn", $3 }' | sed 's/Gi/GB/i')", "cache_memory": "$(free -h | awk 'NR==2{printf "%sn", $6 }' | sed 's/Gi/GB/i')", "total_swap_memory": "$(free -h | awk 'NR==3{printf "%sn", $2 }' | sed 's/Gi/GB/i')", "free_swap_memory": "$(free -h | awk 'NR==3{printf "%sn", $4 }' | sed 's/Gi/GB/i')", "total_used_swap_memory": "$(free -h | awk 'NR==3{printf "%sn", $3 }' | sed 's/Gi/GB/i')", "RAM_usage_percentage": "$(free -g | awk 'NR==2{printf "%.2f%%n", $3*100/$2 }')", "sec_storage_size": "$(df -h / | awk 'NR==2{printf "%sn", $2 }' | sed 's/G/GB/i')", "available_sec_storage": "$(df -h / | awk 'NR==2{printf "%sn", $4 }' | sed 's/G/GB/i')", "used_sec_storage": "$(df -h / | awk 'NR==2{printf "%sn", $3 }' | sed 's/G/GB/i')", "sec_storage_used_percentage": "$(df -h / | awk 'NR==2{printf "%sn", $5 }')"},
"cpu": {"cpu_load": "$(top -bn1 | grep load | awk '{printf "%.2f%%n", $(NF-2)}')"}}
我可以得到结果,但我无法将其用于JSON。我必须完成bash脚本。任何人都可以请你用合适的例子告诉我这个解决方案。
使用heredoc作为模板。下面是一个示例,它为您提供了所需的前几个字段:
#!/bin/sh
jq . << EOF
{
"kernel_version": "$(uname -r)",
"operating_system": "$(awk '$1 == "NAME"{printf "%s ", $3 } $1 == "VERSION" {print $3}' FS=[="] /etc/os-release)",
"os_architecture": "$(arch)",
"internal_ip_address": [
$(hostname -I | sed -e 's/^/"/' -e 's/ *$/",/' -e '$s/,$//')
]
}
EOF