![格式化命令的输出](https://linux22.com/image/150262/%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%91%BD%E4%BB%A4%E7%9A%84%E8%BE%93%E5%87%BA.png)
我目前正在尝试使用 Plink 在 Linux 服务器上运行一些命令来监视其上的一些内容,例如磁盘空间、内存、CPU 使用情况。
我有我想要使用的命令,但我想将输出格式化为更容易阅读的内容。
这是我在批处理文件中使用的命令。
FOR /F "" %%G IN (C:\Users\username\Desktop\ip_list.txt) DO "C:\Program Files\PuTTY\plink.exe" -ssh -batch username@%%G -pw password "hostname; df | fgrep '/dev/'; free -h; top -bn2 | grep 'Cpu(s)';"
这是我得到的输出
基本上,我想在各个命令输出之间添加一些行,以使其更易于阅读。如果不将输出写入文本文件,这可能吗?
谢谢
答案1
这可以通过添加来实现回声“”在命令中间需要空格的地方。
这里有一些例子。
- 在中间添加新行。
例子:
df | fgrep '/dev/'; echo ""; free -h
输出
tmpfs 16334344 55772 16278572 1% /dev/shm
total used free shared buff/cache available
Mem: 31G 4.0G 21G 346M 6.0G 26G
Swap: 15G 2.3M 15G
- 添加命令的详细信息。
受到推崇的
例子:
echo "==================" ; echo "This is output of df"; echo "==================" ;df | grep '/dev/shm' ; echo ""; echo "==================" ; echo "This is output of Free"; echo "==================";free -h
输出:
==================
This is output of df
==================
tmpfs 16334344 55772 16278572 1% /dev/shm
==================
This is output of Free
==================
total used free shared buff/cache available
Mem: 31G 4.0G 21G 359M 6.0G 26G
Swap: 15G 2.3M 15G
答案2
Instead of displaying whole df command output i Wrote below script to find only if disk space crosses threshold (here i assuming threshold as 90%) and also included current memory usage percentage details
Here is the code
df -Ph| awk 'NR>1'| sed "s/%//g"| awk '($(NF-1) >90){print "Disk space utilized is" " "$(NF-1)"%" " for partition" " "$NF}'|sed '1i Below are partition details of hosts where disk utilized crossed 90%\n'| sed '$s/.*/&\n=============================================================================/g';free | awk '/Mem/{print $0}'| awk '{print $3/$2*100}'| sed '1i below are current memory usage percentage of host'
Sample output
Below are partition details of hosts where disk utilized crossed 90%
Disk space utilized is 100% for partition /snap/gnome-system-monitor/51
Disk space utilized is 100% for partition /snap/gtk-common-themes/1122
Disk space utilized is 100% for partition /snap/core/6531
Disk space utilized is 100% for partition /snap/gnome-calculator/260
Disk space utilized is 100% for partition /snap/gnome-logs/37
Disk space utilized is 100% for partition /snap/gtk-common-themes/818
=============================================================================
below are current memory usage percentage of host
39.2084