Linux内部查看系统信息
$INTERNET192忽略
[root@ mysql-master ~]# ip a|grep eth0|grep inet|cut -d' ' -f6|cut -d'/' -f1
10.0.0.21 [root@ mysql-master ~]# ifconfig eth0 |grep 'inet addr'|cut -d':' -f2|cut -d' ' -f1
10.0.0.21
[root@localhost ~]# ifconfig |grep -w inet |awk '{print $2}'|sed -n '1p'
日常巡检完善脚本:
#!/bin/bash function system(){
echo "#########################系统信息#########################"
OS_TYPE=`uname`
OS_VER=`cat /etc/redhat-release`
OS_KER=`uname -a|awk '{print $3}'`
OS_TIME=`date +%F_%T`
OS_RUN_TIME=`uptime |awk '{print $3}'|awk -F, '{print $1}'`
OS_LAST_REBOOT_TIME=`who -b|awk '{print $2,$3}'`
OS_HOSTNAME=`hostname` echo " 系统类型:$OS_TYPE"
echo " 系统版本:$OS_VER"
echo " 系统内核:$OS_KER"
echo " 当前时间:$OS_TIME"
echo " 运行时间:$OS_RUN_TIME"
echo "最后重启时间:$OS_LAST_REBOOT_TIME"
echo " 本机名称:$OS_HOSTNAME"
}
function network(){ echo "#########################网络信息#########################"
INTERNET=(`ifconfig|grep ens|awk -F: '{print $1}'`) # centos6 ifconfig | grep Bcast | awk '{print $2}' | awk -F : '{print $2}'
for((i=;i<`echo ${#INTERNET[*]}`;i++))
do
OS_IP=`ifconfig ${INTERNET[$i]}|head -|grep inet|awk '{print $2}'`
echo " 本机IP:${INTERNET[$i]}:$OS_IP"
done
curl -I http://www.baidu.com &>/dev/null # 测试
if [ $? -eq ]
then echo " 访问外网:成功"
else
echo " 访问外网:失败"
fi
} function hardware(){ echo "#########################硬件信息#########################"
CPUID=`grep "physical id" /proc/cpuinfo |sort|uniq|wc -l`
CPUCORES=`grep "cores" /proc/cpuinfo|sort|uniq|awk -F: '{print $2}'`
CPUMODE=`grep "model name" /proc/cpuinfo|sort|uniq|awk -F: '{print $2}'` echo " CPU数量: $CPUID"
echo " CPU核心:$CPUCORES"
echo " CPU型号:$CPUMODE" MEMTOTAL=`free -m|grep Mem|awk '{print $2}'`
MEMFREE=`free -m|grep Mem|awk '{print $7}'` echo " 内存总容量: ${MEMTOTAL}MB"
echo "剩余内存容量: ${MEMFREE}MB" disksize=
swapsize=`free|grep Swap|awk {'print $2'}`
partitionsize=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk {'print $3'}`)
for ((i=;i<`echo ${#partitionsize[*]}`;i++))
do
disksize=`expr $disksize + ${partitionsize[$i]}`
done
((disktotal=\($disksize+$swapsize\)//)) echo " 磁盘总容量: ${disktotal}GB" diskfree=
swapfree=`free|grep Swap|awk '{print $4}'`
partitionfree=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk '{print $5}'`)
for ((i=;i<`echo ${#partitionfree[*]}`;i++))
do
diskfree=`expr $diskfree + ${partitionfree[$i]}`
done ((freetotal=\($diskfree+$swapfree\)//)) echo "剩余磁盘容量:${freetotal}GB"
} function secure(){
echo "#########################安全信息#########################" countuser=(`last|grep "still logged in"|awk '{print $1}'|sort|uniq`)
for ((i=;i<`echo ${#countuser[*]}`;i++))
do echo "当前登录用户:${countuser[$i]}"
done md5sum -c --quiet /opt/passwd.db &>/dev/null
if [ $? -eq ]
then echo " 用户异常:否"
else echo " 用户异常:是"
fi
} function chksys(){
system
network
hardware
secure
}
chksys
总结:
uname: 系统类型
cat /etc/redhat-relese: 系统版本
uname -a: 系统内核
date +%F_%T: 当前时间
uptime: 运行开机时间
who -b: 最后重启时间
hostname: 本机名称
curl -I http://www.baidu.com &>/dev/null
echo $? 如果返回值为0,则说明访问成功,返回为1,则访问外网失败
grep "physical id" /proc/cpuinfo CPU数量
grep "cores" /proc/cpuinfo CPU核心
model name" /proc/cpuinfo CPU型号
free -m|grep Mem 内存容量
df -T 磁盘容量
last 最后登录用户
last|grep "still logged in" 同上