因此,我有一个bash
脚本,它在服务器上输出详细信息。问题是我需要输出为JSON
。什么是最好的方式去做这件事?下面是bash脚本:从Bash脚本输出JSON
# Get hostname
hostname=`hostname -A` 2> /dev/null
# Get distro
distro=`python -c 'import platform ; print platform.linux_distribution()[0] + " " + platform.linux_distribution()[1]'` 2> /dev/null
# Get uptime
if [ -f "/proc/uptime" ]; then
uptime=`cat /proc/uptime`
uptime=${uptime%%.*}
seconds=$((uptime%60))
minutes=$((uptime/60%60))
hours=$((uptime/60/60%24))
days=$((uptime/60/60/24))
uptime="$days days, $hours hours, $minutes minutes, $seconds seconds"
else
uptime=""
fi
echo $hostname
echo $distro
echo $uptime
所以我想输出是一样的东西:
{"hostname":"server.domain.com", "distro":"CentOS 6.3", "uptime":"5 days, 22 hours, 1 minutes, 41 seconds"}
感谢。
是否有一个原因,你不想从Python做到这一切?我的意思是,无论如何你都在使用它,Python可以很容易地确定主机名,读取正常运行时间并生成你想要的JSON。 – willglynn