# 作業 - 研究程式功能 ## 程式內容  ## 輸出結果 cat /tmp/sinfo  ## 分析 ### 一. 環境變數 **`1. gw=$(route -n | grep -e "^0.0.0.0 ")`**  --- **``2. export GWIF=${gw##* }``** * 將上面過濾出來的 用 {gw##*} 從頭開始刪除到最後  **符合的為 ens33** --- **``3. ips=${ifconfig $GWIF | grep 'inet' }``** * 將上面的$GWIF=ens33帶入 得到結果為  --- **``4. export IP=$(echo $ips | cut -d' ' -f2 | cut -d':' -f2)``** * inet 120.96.143.190 netmask 255.255.255.128 broadcast 120.96.143.255 cut -d : 參數指定欄位分隔字元 -f2 : 擷取第二欄位 會取得 : 120.96.143.190  --- **``5. export NETID=${IP%.*}``** * {IP%.*} 從尾開始刪,刪到 . 為止 得到為 120.96.143 --- **``6. export GW=$(route -n | grep -e '^0.0.0.0' | tr -s \ - | cut -d ' ' -f2)``**  * tr -s 把連續重複的字元以單獨一個字元表示 cut -d ' ' -f2 : 參數指定欄位分隔字元,擷取第二欄位 會得到 : 120.96.143.254 --- ### 二. 程式功能 功能為 讀取電腦系統的硬體 & 網路資訊 **1.記憶體** ``` m=$(free -mh | grep Mem: | tr -s ' ' | cut -d' ' -f2) ``` * free:指令會顯示內存的使用情況,包括實體內存,虛擬的交換文件內存,共享內存區段,以及系統核心使用的緩衝區等。 * -m : 以MB為單位顯示內存使用情況。 * -h : 以合適的單位顯示內存使用情況,最大為三位數,自動計算對應的單位值。單位有: B = bytes K = kilos M = megas G = gigas T = teras https://www.runoob.com/linux/linux-comm-free.html * tr -s : 把連續重複的字元以單獨一個字元表示 * cut -d : 參數指定欄位分隔字元 * -f2 參數指定欲擷取的欄位。第二欄位 --- **2.硬碟** ``` m=$(df -h | grep /dev/sda) ds=$(echo $m | cut -d ' ' -f2) ``` * df -h : 指令加上 -h 選項可以印出人類易讀的格式,會以幾G、幾M的形式印出 https://shengyu7697.github.io/linux-df/ * cut -d : 參數指定欄位分隔字元 * -f2 參數指定欲擷取的欄位。第二欄位 --- **3.CPU** ``` cname=$(cat /proc/cpuinfo | grep 'model name' | head -n 1 | cut -d ':' -f2) cnumber=$(cat /proc/cpuinfo | grep 'model name' | wc -l) ``` * head -n 1 : 從cpuinfo中搜尋找 'model name' 關鍵字,看該檔案前1條訊息( N 為數字) https://ithelp.ithome.com.tw/articles/10236105 * cut -d : 參數指定欄位分隔字元 * -f2 參數指定欲擷取的欄位。第二欄位 --- **4. ip** ``` export IP=$(echo $ips | cut -d' ' -f2 | cut -d':' -f2) ``` --- **5. gateway** ``` gw=$(route -n | grep -e "^0.0.0.0 ") export GWIF=${gw##* } ``` ## 三. 輸出內容 **1 .記憶體、CPU型號、硬碟大小** ``` echo "Memory : ${m}M" >> /tmp/sinfo echo "CPU : $cname (core: $cnumber)" >> /tmp/sinfo echo "Disk : $ds" >> /tmp/sinfo ``` * 將輸出的內容,導入/tmp/sinfo中 --- **2. IP & Gatewat & 伺服器名稱** ``` echo "[Network]" >> /tmp/sinfo echo "IP : $IP" >> /tmp/sinfo echo "Gateway : $GW" >> /tmp/sinfo cat /etc/resolv.conf | grep 'nameserver' | head -n 1 >> /tmp/sinfo ``` --- * cat /etc/resolv.conf : DNS 客戶機配置文件 * nameserver : 名稱伺服器 **3. PING看看網路是否有通** ``` /bin/ping -c 1 www.hinet.net [ "$?" == "0" ] && echo "Internet OK" >> /tmp/sinfo ``` * [ "$?" == "0" ] && echo "Internet OK" 若ping www.hinet.net這個網站有通則會寫 "Internet OK" ###### tags: `作業`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up