shell檢測eth0
Ⅰ 編寫shell腳本獲取本機的網路地址
問題有問題,本機的ip地址是:192.168.100.2/255.255.255.0,網路地址應該是192.168.100.0/255.255.255.0。默認的網關地址才是192.168.100.1/255.255.255.0。
一個超笨的辦法:
在腳本里寫入:
netid=`ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1 | cut -d "." -f 1-3`
echo $netid.0
就可以顯示一個IP地址了,這個IP地址就是你本機所在網路的網路地址。當然,這是以掩碼是24位且你的網卡是eth0為前提的。注意第二個cut -d 後面的引號裡面有空格。
Ⅱ linux 網卡eth0檢測時沒有IP地址,怎麼回事
我想你的網卡IP地址的獲取方式是DHCP方式,你可以設置成為static方式,方法是:執行setup命令,選擇network設置這一項,來設置靜態IP地址獲取方式,並設置IP地址。
然後,再啟動一下network服務:
service network restart
再用ifconfig命令查看一下IP地址是不是有了呢?
Ⅲ shell獲取eth0 地址
This will give you all IPv4 interfaces, including the loopback 127.0.0.1:
This will only show eth0:
And this way you can get IPv6 addresses:
Only eth0 IPv6:
附加:
Getting the external IP
Ⅳ 用shell怎麼在linux下修改eth0的ip地址
!#/bin/bash
ifconfig eth0 x.x.x.x netmask x.x.x.x up #立即生效
ipconf = "/etc/sysconfig/network-scripts/ifcfg-eth0"
echo "DEVICE=eth0" > $ipconf
echo "ONBOOT=yes" > $ipconf
echo "BOOTPROTO=static" > $ipconf
echo "IPADDR=192.168.0.117" > $ipconf
echo "NETMASK=255.255.255.0" > $ipconf
echo "GATEWAY=192.168.0.254" > $ipconf
echo "service network restart" > $ipconf
Ⅳ 【Linux】在shell腳本中獲取當前主機的主機名以及IP地址
在命令行模式下,獲取當前主機名稱:
在命令行模式下,獲取IP地址:
在shell腳本中獲取當前主機的主機名以及IP地址:
說明:
`` 表示執行該語句,並返回執行後的結果;
grep -A1 "eth0" 表示找出帶有「eth0」的行,輸出中除顯示該行外,還顯示之後的一行;
awk 則是進行分割。
Ⅵ linux下怎麼檢測網線連接狀態
首先讓我們通過xshell登錄一台linux伺服器,查看一下當前這台機器有幾塊網卡:ifconfig -a;我們這台機器有兩塊網卡分別是eth0和eth1
然後讓我們模擬一下環境:拔掉eth1的網線,再次執行:ifconfig -a這條命令,對比一下eth0和eth1的區別,發現eht1不再running。沒錯這個欄位就是看網卡是否有連線的,或者網線是否有問題的。
3
接著讓我們把eth1的網線插上,再執行一遍:ifconfig -a;對比一下eth0和eth1,發現此時eth1已經running了。
4
簡單吧!不知道小編這篇文章對您是否有所幫助呢?
Ⅶ 如何通過shell獲取本機的網卡名稱
這個就是shell編程技術了哦
能否看看wo 的網名的 啊?幫忙解決一下這個問題哦
Ⅷ Linux shell修改網卡名為eth0
伺服器初始化的時候將網卡名字改成eth0
網卡名字千奇百怪
有默認的eth0,有ens33,有enp164932等
下面方法可以取出網卡名稱
方法1
方法2
方法3
綜合下來就是:
記得重啟
改了改了
公司突然用伺服器做LINUX伺服器,不搭載ESXi了,這個時候,網卡有3-4個.上面這個就不行了..
要用這個
完成體
記得重啟
如果想順便禁用一下ipv6
當然要重啟生效
感謝 陳大佬 的大力支持
https://blog.csdn.net/u010383467/article/details/118213446?spm=1001.2014.3001.5501
Ⅸ shell腳本上
| 對於初學者而言,因為沒有實戰經驗,寫不出來 Shell 腳本 很正常,如果工作了幾年的運維老年還是寫不出來,那就是沒主動找需求,缺乏練習,缺乏經驗。針對以上問題,總結了30個生產環境中經典的 Shell 腳本 ,通過這些需求案例,希望能幫助大家提升Shell編寫思路,掌握編寫技巧。 |
先了解下編寫Shell過程中注意事項:
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">開頭加解釋器:#!/bin/bash
語法縮進,使用四個空格;多加註釋說明。
命名建議規則:變數名大寫、局部變數小寫,函數名小寫,名字體現出實際作用。
默認變數是全局的,在函數中變數local指定為局部變數,避免污染其他作用域。
有兩個 命令 能幫助我調試腳本:set -e 遇到執行非0時退出腳本,set-x 列印執行過程。
寫腳本一定先測試再到生產上。
</pre>
1、獲取隨機字元串或數字
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">獲取隨機8位字元串:
方法1:
471b94f2
方法2:
vg3BEg==
方法3:
ed9e032c
獲取隨機8位數字:
方法1:
23648321
方法2:
38571131
方法3:
69024815
cksum:列印CRC效驗和統計位元組
</pre>
2、定義一個顏色輸出字元串函數
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:
function echo_color() {
if [ 2 33[0m"
elif [ 2 33[0m"
fi
}
方法2:
function echo_color() {
case 2[0m"
;;
red)
echo -e "[31;40m$2[0m"
;;
*)
echo "Example: echo_color red string"
esac
}
使用方法:echo_color green "test"
function關鍵字定義一個函數,可加或不加。
</pre>
3、批量創建用戶
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
DATE= 1 == "green" ]; then
echo -e "[32;40m 1 == "red" ]; then
echo -e "[31;40m$2[0m"
fi
}
if [ -s USER_FILE {DATE}.bak
echo_color green " {USER_FILE}- USER_FILE
echo "----------------" >> USER &>/dev/null; then
PASS= RANDOM |md5sum |cut -c 1-8)
useradd PASS |passwd --stdin USER USER_FILE
echo " USER User already exists!"
fi
done
</pre>
4、檢查軟體包是否安裝
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
if rpm -q sysstat &>/dev/null; then
echo "sysstat is already installed."
else
echo "sysstat is not installed!"
fi
</pre>
5、檢查服務狀態
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
PORT_C= (ps -ef |grep ntpd |grep -vc grep)
if [ PS_C -eq 0 ]; then
echo "內容" | mail -s "主題" [email protected]
fi
</pre>
6、檢查主機存活狀態
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:將錯誤IP放到數組裡面判斷是否ping失敗三次
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in NUM -le 3 ]; do
if ping -c 1 IP Ping is successful."
break
else
# echo " NUM"
FAIL_COUNT[ IP
let NUM++
fi
done
if [ {FAIL_COUNT[1]} Ping is failure!"
unset FAIL_COUNT[*]
fi
done
方法2:將錯誤次數放到FAIL_COUNT變數裡面判斷是否ping失敗三次
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in IP >/dev/null; then
echo " IP Ping is failure FAIL_COUNT -eq 3 ]; then
echo "$IP Ping is failure!"
fi
done
方法3:利用for循環將ping通就跳出循環繼續,如果不跳出就會走到列印ping失敗
ping_success_status() {
if ping -c 1 IP Ping is successful."
continue
fi
}
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in IP Ping is failure!"
done
</pre>
7、監控CPU、內存和硬碟利用率
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)CPU
藉助vmstat工具來分析CPU統計信息。
DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (( SY))
if [ DATE
Host: USE
" | mail -s "CPU Monitor" $MAIL
fi
2)內存
DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (free -m |awk /Mem/{print (free -m |awk /Mem/{print 6- (( USE))
if [ DATE
Host: TOTAL,Use= FREE
" | mail -s "Memory Monitor" $MAIL
fi
3)硬碟
DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (fdisk -l |awk -F [: ]+ BEGIN{OFS="="}/^Disk /dev/{printf "%s=%sG,", 3} )
PART_USE= 1,int( 6} )
for i in (echo (echo (echo USE -gt 80 ]; then
echo "
Date: IP
Total: PART= MOUNT)
" | mail -s "Disk Monitor" $MAIL
fi
done
</pre>
8、批量主機磁碟利用率監控
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">前提監控端和被監控端SSH免交互登錄或者密鑰登錄。
寫一個配置文件保存被監控主機SSH連接信息,文件內容格式:IP User Port
HOST_INFO=host.info
for IP in 1} (awk -v ip= 1{print HOST_INFO)
PORT= IP ip== 3} PORT IP df -h > (awk BEGIN{OFS="="}/^/dev/{print 5)} USE_RATE_LIST; do
PART_NAME= {USE_RATE#*=}
if [ PART_NAME Partition usage $USE_RATE%!"
fi
done
done
</pre>
9、檢查網站可用性
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)檢查URL可用性
方法1:
check_url() {
HTTP_CODE= 1)
if [ 1 Access failure!"
fi
}
方法2:
check_url() {
if ! wget -T 10 --tries=1 --spider $1 >/dev/null 2>&1; then
}
使用方法:check_url www..com
2)判斷三次URL可用性
思路與上面檢查主機存活狀態一樣。
方法1:利用循環技巧,如果成功就跳出當前循環,否則執行到最後一行
check_url() {
HTTP_CODE= 1)
if [ URL_LIST; do
check_url URL
check_url URL Access failure!"
done
方法2:錯誤次數保存到變數
URL_LIST=" www..com www.agasgf.com "
for URL in (curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" HTTP_CODE -ne 200 ]; then
let FAIL_COUNT++
else
break
fi
done
if [ URL Access failure!"
fi
done
方法3:錯誤次數保存到數組
URL_LIST=" www..com www.agasgf.com "
for URL in NUM -le 3 ]; do
HTTP_CODE= URL)
if [ NUM]= NUM下標, {#FAIL_COUNT[ ]} -eq 3 ]; then
echo "Warning: $URL Access failure!"
unset FAIL_COUNT[ ] #清空數組
fi
done
</pre>
10、檢查MySQL主從同步狀態
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
USER=bak
PASSWD=123456
IO_SQL_STATUS= USER -p 0} ) #gsub去除冒號後面的空格
for i in {i%:*}
THREAD_STATUS= THREAD_STATUS" != "Yes" ]; then
echo "Error: MySQL Master-Slave THREAD_STATUS!"
fi
done
</pre>
動手練一練,讓你的Shell功底上升一個段位!
Ⅹ linux下如何查看某個網卡當前使用的IP。
1、連接上相應的linux主機,進入到等待輸入shell指令的linux命令行狀態下。