shell常用運維腳本
㈠ linux shell有哪些 類型
2.4腳本語言的種類
2.4.1 Shell腳本語言的種類
Shell腳本語言是弱類型語言(無需定義變數的類型就可使用),在Unix/Linux中主要有兩大類Shell,一類是Bourne shell,另一類是C shell。
1. Bourne shell
Bourne shell又包括Bourne shell (sh)、Korn shell (ksh)、Bourne Again Shell(bash)三種類型。
Bourne shell (sh)由AT&T的Steve Bourne開發,是標準的UNIX Shell,很多UNIX系統都配有sh。
Korn shell (ksh)由David Korn開發,是Bourne shell(sh)的超集合,並且添加了csh引入的新功能,是目前很多UNIX系統標准配置的Shell,在這些系統上的/bin/sh往往是指向/bin/ksh的符號鏈接。
Bourne Again Shell即bash由GNU項目組開發,主要目標是與POSIX標準保持一致,同時兼顧對sh的兼容,bash從csh和ksh借鑒了很多功能,是各種Linux發行版默認配置的Shell,在Linux系統上的/bin/sh往往是指向/bin/bash的符號鏈接。雖然如此,bash和sh還是有很多的不同,一方面,bash擴展了一些命令和參數,另一方面,bash並不完全和sh兼容,它們有些行為並不一致,但大多數企業運維的情況下區別不大,特殊場景可以使用bash替代sh。
2. C shell
C shell又包括csh、tcsh兩種類型。
csh由Berkeley大學開發,隨BSD UNIX發布,它的流程式控制制語句很像C語言,支持很多Bourne Shell所不支持的功能,例如:作業控制、別名、系統算術、命令歷史、命令行編輯等。
tcsh是csh的增強版,加入了命令補全等功能,在FreeBSD、Mac OS X等系統上替代了csh。
以上介紹的這些Shell中,較為通用的是標準的Bourne shell (sh)和C shell (csh)。其中Bourne shell (sh)已經被Bourne Again shell (bash) 取代。
可通過以下命令查看CentOS7系統的Shell支持情況。
[root@oldboy ~]# cat /etc/shells
/bin/sh #<==這是Linux里常用的Shell,指向/bin/bash。
/bin/bash #<==這是Linux里常用的Shell,也是默認使用的Shell。
/usr/bin/sh
/usr/bin/bash
Linux系統中的主流Shell是bash,bash是由Bourne Shell(sh)發展而來的,但bash還包含了csh和ksh的特色,但大多數腳本都可以不加修改地在sh上運行,如果你使用sh後發現結果和預期有差異,那麼可以嘗試使用bash替代sh。
內容來源:《跟老男孩學linux運維:Shell高級編程實戰》第02章_Shell腳本初步入門
㈡ 什麼是Shell腳本Shell腳本在Linux運維工作中的地位!
Shell腳本是實現Linux/Unix系統管理及自動化運維必備的重要工具,也是每一個運維工程師需要熟練掌握的知識,它可以幫我們提升工作效率,還可以減少不必要的重復性工作。但很多人對shell並不了解,分不清什麼是shell,什麼是shell腳本,接下來我們通過這篇文章為大家介紹一下。
什麼是shell?
Shell指命令解釋器。它的功能是解釋執行用戶輸入的命令和程序,用戶每輸入一條命令,Shell就解釋執行一條。這種從鍵盤上一輸入命令就能立即得到響應的對話模式稱為交互模式。
什麼是Shell腳本?
當執行命令或程序語句是通過程序文件而不是命令行,那這個程序被稱為Shell腳本。如果Shell腳本內置很多命令、語句及循環控制,然後一次性執行完畢,這種通過文件執行腳本的方式稱為非交互方式。用戶可以在Shell腳本中輸入一系列命令及命令語句組合。這些命令、變數和流程式控制制語句等有機地結合在一起,就形成一個功能強大的Shell腳本。
Shell腳本在Linux運維工作中的地位
Shell腳本語言很適合用於處理純文本類型的數據,而Linux系統中幾乎所有的配置文件、日誌文件(如NFS、Rsync、Httpd、Nginx、LVS、MySQL等),以及絕大多數的啟動文件都是純文本類型的文件。因此,學好shell腳本語言,就可以利用它在Linux系統中發揮巨大的作用。
㈢ 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功底上升一個段位!
㈣ python 運維常用腳本
Python 批量遍歷目錄文件,並修改訪問時間
import os
path = "D:/UASM64/include/"
dirs = os.listdir(path)
temp=[];
for file in dirs:
temp.append(os.path.join(path, file))
for x in temp:
os.utime(x, (1577808000, 1577808000))
Python 實現的自動化伺服器管理
import sys
import os
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def ssh_cmd(user,passwd,port,userfile,cmd):
def ssh_put(user,passwd,source,target):
while True:
try:
shell=str(input("[Shell] # "))
if (shell == ""):
continue
elif (shell == "exit"):
exit()
elif (shell == "put"):
ssh_put("root","123123","./a.py","/root/a.py")
elif (shell =="cron"):
temp=input("輸入一個計劃任務: ")
temp1="(crontab -l; echo "+ temp + ") |crontab"
ssh_cmd("root","123123","22","./user_ip.conf",temp1)
elif (shell == "uncron"):
temp=input("輸入要刪除的計劃任務: ")
temp1="crontab -l | grep -v " "+ temp + "|crontab"
ssh_cmd("root","123123","22","./user_ip.conf",temp1)
else:
ssh_cmd("lyshark","123123","22","./user_ip.conf",shell)
遍歷目錄和文件
import os
def list_all_files(rootdir):
import os
_files = []
list = os.listdir(rootdir) #列出文件夾下所有的目錄與文件
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isdir(path):
_files.extend(list_all_files(path))
if os.path.isfile(path):
_files.append(path)
return _files
a=list_all_files("C:/Users/LyShark/Desktop/a")
print(a)
python檢測指定埠狀態
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk.settimeout(1)
for ip in range(0,254):
try:
sk.connect(("192.168.1."+str(ip),443))
print("192.168.1.%d server open
"%ip)
except Exception:
print("192.168.1.%d server not open"%ip)
sk.close()
python實現批量執行CMD命令
import sys
import os
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print("------------------------------>
")
print("使用說明,在當前目錄創建ip.txt寫入ip地址")
print("------------------------------>
")
user=input("輸入用戶名:")
passwd=input("輸入密碼:")
port=input("輸入埠:")
cmd=input("輸入執行的命令:")
file = open("./ip.txt", "r")
line = file.readlines()
for i in range(len(line)):
print("對IP: %s 執行"%line[i].strip('
'))
python3-實現釘釘報警
import requests
import sys
import json
dingding_url = ' https://oapi.dingtalk.com/robot/send?access_token='
data = {"msgtype": "markdown","markdown": {"title": "監控","text": "apche異常"}}
headers = {'Content-Type':'application/json;charset=UTF-8'}
send_data = json.mps(data).encode('utf-8')
requests.post(url=dingding_url,data=send_data,headers=headers)
import psutil
import requests
import time
import os
import json
monitor_name = set(['httpd','cobblerd']) # 用戶指定監控的服務進程名稱
proc_dict = {}
proc_name = set() # 系統檢測的進程名稱
monitor_map = {
'httpd': 'systemctl restart httpd',
'cobblerd': 'systemctl restart cobblerd' # 系統在進程down掉後,自動重啟
}
dingding_url = ' https://oapi.dingtalk.com/robot/send?access_token='
while True:
for proc in psutil.process_iter(attrs=['pid','name']):
proc_dict[proc.info['pid']] = proc.info['name']
proc_name.add(proc.info['name'])
判斷指定埠是否開放
import socket
port_number = [135,443,80]
for index in port_number:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((飗.0.0.1', index))
if result == 0:
print("Port %d is open" % index)
else:
print("Port %d is not open" % index)
sock.close()
判斷指定埠並且實現釘釘輪詢報警
import requests
import sys
import json
import socket
import time
def dingding(title,text):
dingding_url = ' https://oapi.dingtalk.com/robot/send?access_token='
data = {"msgtype": "markdown","markdown": {"title": title,"text": text}}
headers = {'Content-Type':'application/json;charset=UTF-8'}
send_data = json.mps(data).encode('utf-8')
requests.post(url=dingding_url,data=send_data,headers=headers)
def net_scan():
port_number = [80,135,443]
for index in port_number:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((飗.0.0.1', index))
if result == 0:
print("Port %d is open" % index)
else:
return index
sock.close()
while True:
dingding("Warning",net_scan())
time.sleep(60)
python-實現SSH批量CMD執行命令
import sys
import os
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def ssh_cmd(user,passwd,port,userfile,cmd):
file = open(userfile, "r")
line = file.readlines()
for i in range(len(line)):
print("對IP: %s 執行"%line[i].strip('
'))
ssh.connect(hostname=line[i].strip('
'),port=port,username=user,password=passwd)
cmd=cmd
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
ssh_cmd("lyshark","123","22","./ip.txt","free -h |grep 'Mem:' |awk '{print $3}'")
用python寫一個列舉當前目錄以及所有子目錄下的文件,並列印出絕對路徑
import sys
import os
for root,dirs,files in os.walk("C://"):
for name in files:
print(os.path.join(root,name))
os.walk()
按照這樣的日期格式(xxxx-xx-xx)每日生成一個文件,例如今天生成的文件為2013-09-23.log, 並且把磁碟的使用情況寫到到這個文件中。
import os
import sys
import time
new_time = time.strftime("%Y-%m-%d")
disk_status = os.popen("df -h").readlines()
str1 = ''.join(disk_status)
f = open(new_time+'.log','w')
f.write("%s"%str1)
f.flush()
f.close()
統計出每個IP的訪問量有多少?(從日誌文件中查找)
import sys
list = []
f = open("/var/log/httpd/access_log","r")
str1 = f.readlines()
f.close()
for i in str1:
ip=i.split()[0]
list.append(ip)
list_num=set(list)
for j in list_num:
num=list.count(j)
print("%s -----> %s" %(num,j))
寫個程序,接受用戶輸入數字,並進行校驗,非數字給出錯誤提示,然後重新等待用戶輸入。
import tab
import sys
while True:
try:
num=int(input("輸入數字:").strip())
for x in range(2,num+1):
for y in range(2,x):
if x % y == 0:
break
else:
print(x)
except ValueError:
print("您輸入的不是數字")
except KeyboardInterrupt:
sys.exit("
")
ps 可以查看進程的內存佔用大小,寫一個腳本計算一下所有進程所佔用內存大小的和。
import sys
import os
list=[]
sum=0
str1=os.popen("ps aux","r").readlines()
for i in str1:
str2=i.split()
new_rss=str2[5]
list.append(new_rss)
for i in list[1:-1]:
num=int(i)
sum=sum+num
print("%s ---> %s"%(list[0],sum))
關於Python 命令行參數argv
import sys
if len(sys.argv) < 2:
print ("沒有輸入任何參數")
sys.exit()
if sys.argv[1].startswith("-"):
option = sys.argv[1][1:]
利用random生成6位數字加字母隨機驗證碼
import sys
import random
rand=[]
for x in range(6):
y=random.randrange(0,5)
if y == 2 or y == 4:
num=random.randrange(0,9)
rand.append(str(num))
else:
temp=random.randrange(65,91)
c=chr(temp)
rand.append(c)
result="".join(rand)
print(result)
自動化-使用pexpect非交互登陸系統
import pexpect
import sys
ssh = pexpect.spawn('ssh [email protected]')
fout = file('sshlog.txt', 'w')
ssh.logfile = fout
ssh.expect("[email protected]'s password:")
ssh.sendline("密碼")
ssh.expect('#')
ssh.sendline('ls /home')
ssh.expect('#')
Python-取系統時間
import sys
import time
time_str = time.strftime("日期:%Y-%m-%d",time.localtime())
print(time_str)
time_str= time.strftime("時間:%H:%M",time.localtime())
print(time_str)
psutil-獲取內存使用情況
import sys
import os
import psutil
memory_convent = 1024 * 1024
mem =psutil.virtual_memory()
print("內存容量為:"+str(mem.total/(memory_convent))+"MB
")
print("已使用內存:"+str(mem.used/(memory_convent))+"MB
")
print("可用內存:"+str(mem.total/(memory_convent)-mem.used/(1024*1024))+"MB
")
print("buffer容量:"+str(mem.buffers/( memory_convent ))+"MB
")
print("cache容量:"+str(mem.cached/(memory_convent))+"MB
")
Python-通過SNMP協議監控CPU
注意:被監控的機器上需要支持snmp協議 yum install -y net-snmp*
import os
def getAllitems(host, oid):
sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid + '|grep Raw|grep Cpu|grep -v Kernel').read().split('
')[:-1]
return sn1
def getDate(host):
items = getAllitems(host, '.1.3.6.1.4.1.2021.11')
if name == ' main ':
Python-通過SNMP協議監控系統負載
注意:被監控的機器上需要支持snmp協議 yum install -y net-snmp*
import os
import sys
def getAllitems(host, oid):
sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('
')
return sn1
def getload(host,loid):
load_oids = Ƈ.3.6.1.4.1.2021.10.1.3.' + str(loid)
return getAllitems(host,load_oids)[0].split(':')[3]
if name == ' main ':
Python-通過SNMP協議監控內存
注意:被監控的機器上需要支持snmp協議 yum install -y net-snmp*
import os
def getAllitems(host, oid):
def getSwapTotal(host):
def getSwapUsed(host):
def getMemTotal(host):
def getMemUsed(host):
if name == ' main ':
Python-通過SNMP協議監控磁碟
注意:被監控的機器上需要支持snmp協議 yum install -y net-snmp*
import re
import os
def getAllitems(host,oid):
def getDate(source,newitem):
def getRealDate(item1,item2,listname):
def caculateDiskUsedRate(host):
if name == ' main ':
Python-通過SNMP協議監控網卡流量
注意:被監控的機器上需要支持snmp協議 yum install -y net-snmp*
import re
import os
def getAllitems(host,oid):
sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('
')[:-1]
return sn1
def getDevices(host):
device_mib = getAllitems(host,'RFC1213-MIB::ifDescr')
device_list = []
def getDate(host,oid):
date_mib = getAllitems(host,oid)[1:]
date = []
if name == ' main ':
Python-實現多級菜單
import os
import sys
ps="[None]->"
ip=["192.168.1.1","192.168.1.2","192.168.1.3"]
flage=1
while True:
ps="[None]->"
temp=input(ps)
if (temp=="test"):
print("test page !!!!")
elif(temp=="user"):
while (flage == 1):
ps="[User]->"
temp1=input(ps)
if(temp1 =="exit"):
flage=0
break
elif(temp1=="show"):
for i in range(len(ip)):
print(i)
Python實現一個沒用的東西
import sys
ps="[root@localhost]# "
ip=["192.168.1.1","192.168.1.2","192.168.1.3"]
while True:
temp=input(ps)
temp1=temp.split()
檢查各個進程讀寫的磁碟IO
import sys
import os
import time
import signal
import re
class DiskIO:
def init (self, pname=None, pid=None, reads=0, writes=0):
self.pname = pname
self.pid = pid
self.reads = 0
self.writes = 0
def main():
argc = len(sys.argv)
if argc != 1:
print ("usage: please run this script like [./lyshark.py]")
sys.exit(0)
if os.getuid() != 0:
print ("Error: This script must be run as root")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
os.system('echo 1 > /proc/sys/vm/block_mp')
print ("TASK PID READ WRITE")
while True:
os.system('dmesg -c > /tmp/diskio.log')
l = []
f = open('/tmp/diskio.log', 'r')
line = f.readline()
while line:
m = re.match(
'^(S+)(d+)(d+): (READ|WRITE) block (d+) on (S+)', line)
if m != None:
if not l:
l.append(DiskIO(m.group(1), m.group(2)))
line = f.readline()
continue
found = False
for item in l:
if item.pid == m.group(2):
found = True
if m.group(3) == "READ":
item.reads = item.reads + 1
elif m.group(3) == "WRITE":
item.writes = item.writes + 1
if not found:
l.append(DiskIO(m.group(1), m.group(2)))
line = f.readline()
time.sleep(1)
for item in l:
print ("%-10s %10s %10d %10d" %
(item.pname, item.pid, item.reads, item.writes))
def signal_handler(signal, frame):
os.system('echo 0 > /proc/sys/vm/block_mp')
sys.exit(0)
if name ==" main ":
main()
利用Pexpect實現自動非交互登陸linux
import pexpect
import sys
ssh = pexpect.spawn('ssh [email protected]')
fout = file('sshlog.log', 'w')
ssh.logfile = fout
ssh.expect("[email protected]'s password:")
ssh.sendline("密碼")
ssh.expect('#')
ssh.sendline('ls /home')
ssh.expect('#')
利用psutil模塊獲取系統的各種統計信息
import sys
import psutil
import time
import os
time_str = time.strftime( "%Y-%m-%d", time.localtime( ) )
file_name = "./" + time_str + ".log"
if os.path.exists ( file_name ) == False :
os.mknod( file_name )
handle = open ( file_name , "w" )
else :
handle = open ( file_name , "a" )
if len( sys.argv ) == 1 :
print_type = 1
else :
print_type = 2
def isset ( list_arr , name ) :
if name in list_arr :
return True
else :
return False
print_str = "";
if ( print_type == 1 ) or isset( sys.argv,"mem" ) :
memory_convent = 1024 * 1024
mem = psutil.virtual_memory()
print_str += " 內存狀態如下:
"
print_str = print_str + " 系統的內存容量為: "+str( mem.total/( memory_convent ) ) + " MB
"
print_str = print_str + " 系統的內存以使用容量為: "+str( mem.used/( memory_convent ) ) + " MB
"
print_str = print_str + " 系統可用的內存容量為: "+str( mem.total/( memory_convent ) - mem.used/( 1024*1024 )) + "MB
"
print_str = print_str + " 內存的buffer容量為: "+str( mem.buffers/( memory_convent ) ) + " MB
"
print_str = print_str + " 內存的cache容量為:" +str( mem.cached/( memory_convent ) ) + " MB
"
if ( print_type == 1 ) or isset( sys.argv,"cpu" ) :
print_str += " CPU狀態如下:
"
cpu_status = psutil.cpu_times()
print_str = print_str + " user = " + str( cpu_status.user ) + "
"
print_str = print_str + " nice = " + str( cpu_status.nice ) + "
"
print_str = print_str + " system = " + str( cpu_status.system ) + "
"
print_str = print_str + " idle = " + str ( cpu_status.idle ) + "
"
print_str = print_str + " iowait = " + str ( cpu_status.iowait ) + "
"
print_str = print_str + " irq = " + str( cpu_status.irq ) + "
"
print_str = print_str + " softirq = " + str ( cpu_status.softirq ) + "
"
print_str = print_str + " steal = " + str ( cpu_status.steal ) + "
"
print_str = print_str + " guest = " + str ( cpu_status.guest ) + "
"
if ( print_type == 1 ) or isset ( sys.argv,"disk" ) :
print_str += " 硬碟信息如下:
"
disk_status = psutil.disk_partitions()
for item in disk_status :
print_str = print_str + " "+ str( item ) + "
"
if ( print_type == 1 ) or isset ( sys.argv,"user" ) :
print_str += " 登錄用戶信息如下:
"
user_status = psutil.users()
for item in user_status :
print_str = print_str + " "+ str( item ) + "
"
print_str += "---------------------------------------------------------------
"
print ( print_str )
handle.write( print_str )
handle.close()
import psutil
mem = psutil.virtual_memory()
print mem.total,mem.used,mem
print psutil.swap_memory() # 輸出獲取SWAP分區信息
cpu = psutil.cpu_stats()
printcpu.interrupts,cpu.ctx_switches
psutil.cpu_times(percpu=True) # 輸出每個核心的詳細CPU信息
psutil.cpu_times().user # 獲取CPU的單項數據 [用戶態CPU的數據]
psutil.cpu_count() # 獲取CPU邏輯核心數,默認logical=True
psutil.cpu_count(logical=False) # 獲取CPU物理核心數
psutil.disk_partitions() # 列出全部的分區信息
psutil.disk_usage('/') # 顯示出指定的掛載點情況【位元組為單位】
psutil.disk_io_counters() # 磁碟總的IO個數
psutil.disk_io_counters(perdisk=True) # 獲取單個分區IO個數
psutil.net_io_counter() 獲取網路總的IO,默認參數pernic=False
psutil.net_io_counter(pernic=Ture)獲取網路各個網卡的IO
psutil.pids() # 列出所有進程的pid號
p = psutil.Process(2047)
p.name() 列出進程名稱
p.exe() 列出進程bin路徑
p.cwd() 列出進程工作目錄的絕對路徑
p.status()進程當前狀態[sleep等狀態]
p.create_time() 進程創建的時間 [時間戳格式]
p.uids()
p.gids()
p.cputimes() 【進程的CPU時間,包括用戶態、內核態】
p.cpu_affinity() # 顯示CPU親緣關系
p.memory_percent() 進程內存利用率
p.meminfo() 進程的RSS、VMS信息
p.io_counters() 進程IO信息,包括讀寫IO數及位元組數
p.connections() 返回打開進程socket的nametples列表
p.num_threads() 進程打開的線程數
import psutil
from subprocess import PIPE
p =psutil.Popen(["/usr/bin/python" ,"-c","print 'helloworld'"],stdout=PIPE)
p.name()
p.username()
p.communicate()
p.cpu_times()
psutil.users() # 顯示當前登錄的用戶,和Linux的who命令差不多
psutil.boot_time() 結果是個UNIX時間戳,下面我們來轉換它為標准時間格式,如下:
datetime.datetime.fromtimestamp(psutil.boot_time()) # 得出的結果不是str格式,繼續進行轉換 datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d%H:%M:%S')
Python生成一個隨機密碼
import random, string
def GenPassword(length):
if name == ' main ':
print (GenPassword(6))
㈤ Linux Shell 腳本編程最佳實踐
IT路邊社
前言
與其它的編碼規范一樣,這里所討論的不僅僅是編碼格式美不美觀的問題, 同時也討論一些約定及編碼標准。這份文檔主要側重於我們所普遍遵循的規則,對於那些不是明確強制要求的,我們盡量避免提供意見。
編碼規范對於程序員而言尤為重要,有以下幾個原因:
本文檔中的准則致力於最大限度達到以下原則:
盡管本文檔涵蓋了許多基礎知識,但應注意的是,沒有編碼規范可以為我們回答所有問題,開發人員始終需要再編寫完代碼後,對上述原則做出正確的判斷。
注 :未明確指明的則默認為必須(Mandatory)
主要參考如下文檔:
僅建議Shell用作相對簡單的實用工具或者包裝腳本。因此單個shell腳本內容不宜太過復雜。
在選擇何時使用shell腳本時時應遵循以下原則:
可執行文件不建議有擴展名,庫文件必須使用 .sh 作為擴展名,且應是不可執行的。
執行一個程序時,無需知道其編寫語言,且shell腳本並不要求具有擴展名,所以更傾向可執行文件沒有擴展名。
而庫文件知道其編寫語言十分重要,使用 .sh 作為特定語言後綴的擴展名,可以和其他語言編寫的庫文件加以區分。
文件名要求全部小寫, 可以包含下劃線 _ 或連字元 - , 建議可執行文件使用連字元,庫文件使用下劃線。
正例:
反例:
源文件編碼格式為UTF-8。避免不同操作系統對文件換行處理的方式不同,一律使用 LF 。
每行最多不超過120個字元。每行代碼最大長度限制的根本原因是過長的行會導致閱讀障礙,使得縮進失效。
除了以下兩種情況例外:
如出現長度必須超過120個字元的字元串,應盡量使用here document或者嵌入的換行符等合適的方法使其變短。
示例:
除了在行結束使用換行符,空格是源文件中唯一允許出現的空白字元。
對從來沒有用到的或者被注釋的方法、變數等要堅決從代碼中清理出去,避免過多垃圾造成干擾。
Bash 是唯一被允許使用的可執行腳本shell。
可執行文件必須以 #!/bin/bash 開始。請使用 set 來設置shell的選項,使得用 bash echo "Process $: Done making $$$."
# 示例7:命令參數及路徑不需要引號 grep -li Hugo /dev/ "$1"
# 示例8:常規變數用雙引號,ccs可能為空的特殊情況可不用引號 git send-email --to "${reviewers}" ${ccs:+"--cc" "${ccs}"}
# 示例9:正則用單引號,$1可能為空的特殊情況可不用引號 grep -cP '([Ss]pecial||?characters*) ${1:+"$1"}
# 示例10:位置參數傳遞推薦帶引號的"$@",所有參數作為單字元串傳遞用帶引號的"$*" # content of t.sh func_t { echo num: $# echo args: 1:$1 2:$2 3:$3 }
func_t "$@" func_t "$*" # 當執行 ./t.sh a b c 時輸出如下: num: 3 args: 1:a 2:b 3:c num: 1 args: 1:a b c 2: 3:
使用 $(command) 而不是反引號。
因反引號如果要嵌套則要求用反斜杠轉義內部的反引號。而 $(command) 形式的嵌套無需轉義,且可讀性更高。
正例:
反例:
條件測試
使用 [[ ... ]] ,而不是 [ , test , 和 /usr/bin/[ 。
因為在 [[ 和 ]] 之間不會出現路徑擴展或單詞切分,所以使用 [[ ... ]] 能夠減少犯錯。且 [[ ... ]] 支持正則表達式匹配,而 [ ... ] 不支持。參考以下示例:
盡可能使用變數引用,而非字元串過濾。
Bash可以很好的處理空字元串測試,請使用空/非空字元串測試方法,而不是過濾字元,讓代碼具有更高的可讀性。正例:
反例:
正例:
反例:
正例:
反例:
文件名擴展
當進行文件名的通配符擴展時,請指定明確的路徑。
當目錄中有特殊文件名如以 - 開頭的文件時,使用帶路徑的擴展通配符 ./* 比不帶路徑的 * 要安全很多。
應該避免使用eval。
Eval在用於分配變數時會修改輸入內容,但設置變數的同時並不能檢查這些變數是什麼。反例:
請使用進程替換或者for循環,而不是通過管道連接while循環。
這是因為在管道之後的while循環中,命令是在一個子shell中運行的,因此對變數的修改是不能傳遞給父shell的。
這種管道連接while循環中的隱式子shell使得bug定位非常困難。反例:
如果你確定輸入中不包含空格或者其他特殊符號(通常不是來自用戶輸入),則可以用for循環代替。例如:
使用進程替換可實現重定向輸出,但是請將命令放入顯式子 shell,而非 while 循環創建的隱式子 shell。例如:
總是檢查返回值,且提供有用的返回值。
對於非管道命令,使用 $? 或直接通過 if 語句來檢查以保持其簡潔。
例如:
當內建命令可以完成相同的任務時,在shell內建命令和調用外部命令之間,應盡量選擇內建命令。
因內建命令相比外部命令而言會產生更少的依賴,且多數情況調用內建命令比調用外部命令可以獲得更好的性能(通常外部命令會產生額外的進程開銷)。
正例:
反例:
載入外部庫文件不建議用使用.,建議使用source,已提升可閱讀性。正例:
反例:
除非必要情況,盡量使用單個命令及其參數組合來完成一項任務,而非多個命令加上管道的不必要組合。常見的不建議的用法例如:cat和grep連用過濾字元串; cat和wc連用統計行數; grep和wc連用統計行數等。
正例:
除特殊情況外,幾乎所有函數都不應該使用exit直接退出腳本,而應該使用return進行返回,以便後續邏輯中可以對錯誤進行處理。正例:
反例:
推薦以下工具幫助我們進行代碼的規范:
原文鏈接:http://itxx00.github.io/blog/2020/01/03/shell-standards/
獲取更多的面試題、腳本等運維資料點擊: 運維知識社區 獲取
腳本之---簡訊轟炸機
腳本之---QQ微信轟炸機
ansible---一鍵搭建redis5.0.5集群
elk7.9真集群docker部署文檔
全球最全loki部署及配置文檔
最強安全加固腳本2.0
一鍵設置iptbales腳本
㈥ 菜鳥問LINUX運維工程師在工作中用這些腳本語言(Shell、Perl、Python、Ruby)做什麼
本人主要用shell,結合工作經驗,給出如下常用功能:
1.製作定時任務,可以獲得系統狀態,然後截取系統狀態,發送至故障監控平台或簡訊平台,實現故障的自動預警。
2.提取數據,形成文本毀指,處理文本,得到規格化的數據,提供給相關部門使用。
3.製作shell互動式菜單,提供給其他運維殲頃值班人員使用,用以纖改配手工監控系統各種狀態
4.版本升級、系統變更等操作需要使用。
㈦ Shell腳本語言優勢怎樣
Shell腳本語言的優勢在於處理偏操作系統底層的業務,例如:Linux系統內部的很多應用(有的是應用的一部分)都是使用Shell腳本開發的,因為有1000多個Linux系統命令為它做支撐,特別是Linux正則表達式及三劍客grep、sed、awk等命令。
對於一些常見的系統腳本使用Shell開發會更簡單、更快速,例如:讓軟體一鍵自動化安裝、優化,監控報警腳本,軟體啟動腳本,日誌分析腳本等,雖然PHP/Python語言也能夠做到這些,但是,考慮到掌握難度、開發效率、開發習慣等因素,它們可能就不如Shell腳本語言流行及有優勢了。對於一些常規的業務應用,使用Shell更符合Linux運維簡單、易用、高效的三大基本原則。
PHP語言的優勢在於小型網站系統的開發;Python語言的優勢在於開發較復雜的運維工具、Web界面的管理工具和Web業務的開發(例如:CMDB自動化運維平台、跳板機、批量管理軟體SaltStack、雲計算OpenStack軟體)等。我們在開發一個應用時應根據業務需求,結合不同語言的優勢及自身擅長的語言來選擇,揚長避短,從而達到高效開發及易於自身維護等目的。
㈧ 求推薦學習python或者shell等運維腳本語言的書
Python雖然和shell都是腳本語言,經過這么多年的發展,已經不僅僅是一種shell語言,更是比較流行的編程語言。
看學習資料建議讀官方文檔,即權威又容易獲取。
python建議看安裝自帶的chm,Python v3.X.0 documentation裡面有一章Tutorial,The Python Tutorial這一章從數據類型到常用庫都講的很詳細。
shell得看是哪種操作系統,各種操作系統的shell語言有些不同的地方,可以從基本命令開始了解,操作系統的man命令很詳細。
㈨ 請用shell寫個腳本,當apache的進程數大於10的時候發郵件給運維部
#!/bin/bash
count=`ps -ef | grep [a]pache | wc -l`
if [ $count -gt 10 ]; then
mail -s 標題 [email protected] <<!!
內容
.
!!
fi
###################
說明一下,count計算的時候,grep後面改成你真正的apache的進程名,[a]pache首字母加了個方括弧是為了防止把grep本身的進程數也算進去。
mail後面的標題,地址和內容改成你自己需要的內容。
在內容的後面那個.不要漏掉。
count=後面的引號是鍵盤上數字1左邊那個重音符號,而不是單雙引號那個引號。
這個腳本有個前提,你本機的sendmail服務是可用的,否則這個腳本是發不出郵件的。
至於sendmail服務,那就是另外的話題了。需要配置的話請google一下。