耳朵(十一)——Linux綜合命令測試
#1.顯示當前主機系統資訊,包括主機名,IPv4地址,作業系統版本,核心版本,CPU型號,記憶體大小,硬碟大 小,儲存指令碼 為sys_info.sh
#!/bin/bash
info1=hostname
#查詢主機名
echo "localhost name : //g’#查詢ipv4的地址 echo "IPV4 iddress is : $info2" echo "-----------------------------------------------------------------------------" info3=
cat /etc/centos-release#作業系統的版本 echo "os info id : $info3 " echo "-----------------------------------------------------------------------------" info4=
#核心版本 echo "kernel is: $info4" echo "-----------------------------------------------------------------------------" info5=
cat /proc/cpuinfo | head -n5 | tail -n1 | sed 's/^.*?/g’#cpu型號 echo "cup info : $info5" echo "-----------------------------------------------------------------------------" info6=
free -h | grep “Mem” | cut -d " " -f12#記憶體 echo "Memory info : $info6" echo "-----------------------------------------------------------------------------" info7=
fdisk -l | grep “Disk /dev” | cut -d, -f1` #磁碟大小
echo “$info7”
#2.將/etc/目錄備份到/tmp下,並以此格式儲存bak_etc_yyyy-mm-dd,儲存為指令碼bak_etc.sh
#!/bin/bash
cp -r /etc /tmp/bak_etc_date +%F
#3.顯示當前硬碟分割槽中空間利用率大的值。
#!/bin/bash
d=df | sed -r 's/([ ])+/;/g' | cut -d";" -f5 | sort -n | tail -n1
echo “disk max is: $d”
#4.顯示正連線本主機的每個遠端主機的IPv4地址和連線數,並按連線數從大到小排序,儲存指令碼為link.sh
#!/bin/bash
info=netstat -nt | sed -r 's/([ ])+/;/g' | cut -d";" -f5 | cut -d":" -f1 | sort -nr | uniq -c
echo “ipv4 $ info”
#5.計算/etc/passwd檔案中的第5個使用者和第15使用者的ID之和,儲存指令碼為sum_id.sh
#!/bin/bash
uid1= cat /etc/passwd | cut -d":" -f 3 | head -n5 | tail -n1
uid2=cat /etc/passwd | cut -d":" -f 3 | head -n15 | tail -n1
echo “expr uid1 + uid2”
#6.統計/etc, /var, /usr目錄中共有多少檔案,儲存指令碼為sum.sh
#!/bin/bash
dir1=ls -l /etc/ | wc -l
dir2=ls -l /var/ | wc -l
dir3=ls -l /usr/ | wc -l
echo “/etc一共有dir2個檔案,/usr一共有$dir3個檔案”
#7.接受一個主機的IPv4地址做為引數,測試是否可連通。如果能ping通,則輸出“該IP地址可訪問”;如果不可 ping通,則輸出“該IP地址不可訪問”,儲存指令碼為ping.sh
#!/bin/bash ipaddr=’(<([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))>.){3}<([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))>’ read -p “please inupt ipv4 addr:” ipv4 if [[ $ipv4 =~ $ipaddr ]] then ping $ipv4 -c 4 $$ echo “可以正常訪問” || echo “不能正常訪問” else echo “請輸入正確網址” exit fi