文章 通过shell脚本获取主机信息
Post
Cancel

通过shell脚本获取主机信息

在Windows系统下面,我们通常可以通过查看计算机的属性可以获取到该主机的基本信息,也可以通过一些第三方软件获取主机的基本参数。在Linux系统下,我们可以通过编写shell脚本获取主机的基本参数。shell脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
echo "Current User:$USER"
count=`find /etc/sysconfig/network-scripts/ -name ifcfg-eth* |wc -l`
i=0
while [ $i -lt $count ]
do
ip=`ifconfig eth$i |grep "inet addr"|awk -F[:" "]+ '{print $4}'`
echo "Host IP: `echo $ip`"
i=$(($i+1))
done
echo "Kernel:`uname -r`"
echo "Hostname:$HOSTNAME"
echo "Current Time:`date +'%Y-%m-%d %H:%M:%S'` "
os=`/usr/bin/lsb_release -a |grep "Des"|sed 's@^.*on:@@g'`
echo "Host OS:`echo $os`"
cpu=`/bin/cat /proc/cpuinfo|grep "name"|uniq -c|sed 's@^.*:@@g'`
echo "CPU:`echo $cpu`"

脚本运行结果如下:

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# sh host_info.sh
Current User:root
Host IP: 10.224.252.123
Host IP: 192.168.1.13
Host IP: 172.16.10.24
Kernel:2.6.18-348.el5
Hostname:localhost.localdomain
Current Time:2014-01-04 11:14:36
Host OS:CentOS release 5.9 (Final)
CPU:Intel(R) Pentium(R) CPU G860 @ 3.00GHz
This post is licensed under CC BY 4.0 by the author.

Shell脚本编程30分钟入门

一般缓存更新策略