python 命令列模組

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-u", "--user", action="store_true", dest="users", default=False, help="user names")
parser.add_option("-p", "--port", action="store_true", dest="ports", default=False, help="user ports")
(options, args) = parser.parse_args() if options.users==True:
print("user names is true")
if options.ports==True:
print("passwd is true")

Python 批量遍歷目錄檔案,並修改訪問時間

>>> path="D:/UASM64/include/"
>>> dirs = os.listdir(path)
>>> for file in dirs:
... print(os.path.join(path,file)) 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))

遍歷目錄和檔案

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 \n"%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("------------------------------>\n")
print("使用說明,在當前目錄建立ip.txt寫入ip地址")
print("------------------------------>\n") 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('\n')) ssh.connect(hostname=line[i].strip('\n'),port=port,username=user,password=passwd)
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read() if not result:
result=stderr.read()
ssh.close() print(result.decode())

python3-實現釘釘報警

import requests
import sys
import json dingding_url = 'https://oapi.dingtalk.com/robot/send?access_token=6d11af3252812ea50410c2ccb861814a6ed11b2306606934a5d4ca9f2ec8c09' data = {"msgtype": "markdown","markdown": {"title": "監控","text": "apche異常"}} headers = {'Content-Type':'application/json;charset=UTF-8'} send_data = json.dumps(data).encode('utf-8')
requests.post(url=dingding_url,data=send_data,headers=headers)
#coding: utf-8
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=b5258c4335ed8ab792075013c965efcbf4f8940f92e7bd936cdc7842d3bf9405'
# 釘釘機器人token使用參考文件:http://www.pc6.com/infoview/Article_108931.html 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']) proc_stop = monitor_name - proc_name # 通過集合的形式來找出停掉的程序名,前者有但是後者沒有的 if proc_stop: # 如果確實有監控的程序停掉了,那麼我們需要告警以及自動重啟功能
for p in proc_stop:
p_status = '停止'
p_name = p
data = {
"msgtype": "markdown",
"markdown": {
"title": "監控資訊",
"text": "### %s\n" % time.strftime("%Y-%m-%d %X") +
"> #### 服務名:%s \n\n" % p_name +
"> #### 狀態:%s \n\n" % p_status +
"> #### 正在嘗試啟動"
},
}
headers = {'Content-Type':'application/json;charset=UTF-8'}
send_data = json.dumps(data).encode('utf-8')
requests.post(url=dingding_url,data=send_data,headers=headers) os.system(monitor_map[p_name]) # 執行重啟命令,然後判斷是否重啟成功
proc_set = set()
for proc_again in psutil.process_iter(attrs=['pid','name']):
proc_set.add(proc_again.info['name']) if p in proc_set: # 如果程序啟動成功,p是以前停掉的程序,proc_set是已經重啟過一次後的所有程序集合
p_status = '成功'
p_name = p
data = {
"msgtype": "markdown",
"markdown": {
"title": "監控資訊",
"text": "### %s\n" % time.strftime("%Y-%m-%d %X") +
"> #### 服務名:%s \n\n" % p_name +
"> #### 狀態:%s \n\n" % p_status +
"> #### 已經啟動成功,服務正在執行!"
},
}
headers = {'Content-Type':'application/json;charset=UTF-8'}
send_data = json.dumps(data).encode('utf-8')
requests.post(url=dingding_url,data=send_data,headers=headers)
else:
p_status = '重啟失敗'
p_name = p
data = {
"msgtype": "markdown",
"markdown": {
"title": "監控資訊",
"text": "### %s\n" % time.strftime("%Y-%m-%d %X") +
"> #### 服務名:%s \n\n" % p_name +
"> #### 狀態:%s \n\n" % p_status +
"> #### Sorry,服務啟動失敗鳥!"
},
}
headers = {'Content-Type':'application/json;charset=UTF-8'}
send_data = json.dumps(data).encode('utf-8')
requests.post(url=dingding_url,data=send_data,headers=headers)
time.sleep(5)

判斷指定埠是否開放

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(('127.0.0.1', index))
if result == 0:
print("Port %d is open" % index)
else:
print("Port %d is not open" % index)
sock.close()

判斷指定埠並且實現釘釘輪詢報警

#By LyShark

import requests
import sys
import json
import socket
import time def dingding(title,text):
dingding_url = 'https://oapi.dingtalk.com/robot/send?access_token=6d11af3252812ea50410c2ccb861814a69ed11b2306606934a5d4ca9f2c8c09'
data = {"msgtype": "markdown","markdown": {"title": title,"text": text}}
headers = {'Content-Type':'application/json;charset=UTF-8'}
send_data = json.dumps(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(('127.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執行命令

# By:LyShark

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('\n'))
ssh.connect(hostname=line[i].strip('\n'),port=port,username=user,password=passwd)
cmd=cmd
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read() if not result:
result=stderr.read()
ssh.close() print(result.decode()) 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))

寫個程式,接受使用者輸入數字,並進行校驗,非數字給出錯誤提示,然後重新等待使用者輸入。

#根據使用者輸入數字,輸出從0到該數字之間所有的素數。(只能被1和自身整除的數為素數)
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("\n")

ps 可以檢視程序的記憶體佔用大小,寫一個指令碼計算一下所有程序所佔用記憶體大小的和。

#(提示,使用ps aux 列出所有程序,過濾出RSS那列,然後求和)
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:] if option == "version":
print ("版本資訊")
elif option == "help":
print ("幫助選單")
elif option == "option":
print("配置選單")
else:
print ("異常")
sys.exit()

利用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\n")
print("已使用記憶體:"+str(mem.used/(memory_convent))+"MB\n")
print("可用記憶體:"+str(mem.total/(memory_convent)-mem.used/(1024*1024))+"MB\n")
print("buffer容量:"+str(mem.buffers/( memory_convent ))+"MB\n")
print("cache容量:"+str(mem.cached/(memory_convent))+"MB\n")

Python-通過SNMP協議監控CPU

注意:被監控的機器上需要支援snmp協議 yum install -y net-snmp*

#!/usr/bin/python
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('\n')[:-1]
return sn1 def getDate(host):
items = getAllitems(host, '.1.3.6.1.4.1.2021.11') date = []
rate = []
cpu_total = 0 #us = us+ni, sy = sy + irq + sirq for item in items:
float_item = float(item.split(' ')[3])
cpu_total += float_item
if item == items[0]:
date.append(float(item.split(' ')[3]) + float(items[1].split(' ')[3]))
elif item == item[2]:
date.append(float(item.split(' ')[3] + items[5].split(' ')[3] + items[6].split(' ')[3]))
else:
date.append(float_item) #calculate cpu usage percentage for item in date:
rate.append((item/cpu_total)*100) mean = ['%us','%ni','%sy','%id','%wa','%cpu_irq','%cpu_sIRQ'] #calculate cpu usage percentage
result = map(None,rate,mean)
return result if __name__ == '__main__': hosts = ['192.168.1.17','192.168.1.17'] for host in hosts:
print '==========' + host + '=========='
result = getDate(host)
print 'Cpu(s)',
#print result
for i in range(5):
print ' %.2f%s' % (result[i][0],result[i][1]),
print
print

Python-通過SNMP協議監控系統負載

注意:被監控的機器上需要支援snmp協議 yum install -y net-snmp*

#!/usr/bin/python
import os
import sys def getAllitems(host, oid):
sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')
return sn1 def getload(host,loid):
load_oids = '1.3.6.1.4.1.2021.10.1.3.' + str(loid)
return getAllitems(host,load_oids)[0].split(':')[3] if __name__ == '__main__': hosts = ['192.168.1.17','192.168.1.17'] print ('==============System Load==============') for host in hosts:
load1 = getload(host, 1)
load10 = getload(host, 2)
load15 = getload(host, 3)
print ('%s load(1min): %s ,load(10min): %s ,load(15min): %s' % (host,load1,load10,load15))

Python-通過SNMP協議監控記憶體

注意:被監控的機器上需要支援snmp協議 yum install -y net-snmp*

#!/usr/bin/python
import os def getAllitems(host, oid): sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]
return sn1 def getSwapTotal(host): swap_total = getAllitems(host, 'UCD-SNMP-MIB::memTotalSwap.0')[0].split(' ')[3]
return swap_total def getSwapUsed(host): swap_avail = getAllitems(host, 'UCD-SNMP-MIB::memAvailSwap.0')[0].split(' ')[3]
swap_total = getSwapTotal(host)
swap_used = str(round(((float(swap_total)-float(swap_avail))/float(swap_total))*100 ,2)) + '%'
return swap_used def getMemTotal(host): mem_total = getAllitems(host, 'UCD-SNMP-MIB::memTotalReal.0')[0].split(' ')[3]
return mem_total def getMemUsed(host): mem_total = getMemTotal(host)
mem_avail = getAllitems(host, 'UCD-SNMP-MIB::memAvailReal.0')[0].split(' ')[3]
mem_used = str(round(((float(mem_total)-float(mem_avail))/float(mem_total))*100 ,2)) + '%'
return mem_used if __name__ == '__main__': hosts = ['192.168.1.17','192.168.1.17']
print ("Monitoring Memory Usage") for host in hosts: mem_used = getMemUsed(host)
swap_used = getSwapUsed(host) print ('==========' + host + '==========')
print ('Mem_Used = %-15s Swap_Used = %-15s' %(mem_used,swap_used))
print()

Python-通過SNMP協議監控磁碟

注意:被監控的機器上需要支援snmp協議 yum install -y net-snmp*

#!/usr/bin/python

import re
import os def getAllitems(host,oid): sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]
return sn1 def getDate(source,newitem): for item in source[5:]:
newitem.append(item.split(':')[3].strip())
return newitem def getRealDate(item1,item2,listname): for i in range(len(item1)):
listname.append(int(item1[i])*int(item2[i])/1024)
return listname def caculateDiskUsedRate(host): hrStorageDescr = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageDescr')
hrStorageUsed = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageUsed')
hrStorageSize = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageSize')
hrStorageAllocationUnits = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageAllocationUnits') disk_list = []
hrsused = []
hrsize = []
hrsaunits = [] #get disk_list for item in hrStorageDescr:
if re.search('/',item):
disk_list.append(item.split(':')[3]) #print disk_list getDate(hrStorageUsed,hrsused)
getDate(hrStorageSize,hrsize) #print getDate(hrStorageAllocationUnits,hrsaunits) #get hrstorageAllocationUnits for item in hrStorageAllocationUnits[5:]:
hrsaunits.append(item.split(':')[3].strip().split(' ')[0]) #caculate the result #disk_used = hrStorageUsed * hrStorageAllocationUnits /1024 (KB) disk_used = []
total_size = []
disk_used = getRealDate(hrsused,hrsaunits,disk_used)
total_size = getRealDate(hrsize,hrsaunits,total_size) diskused_rate = [] for i in range(len(disk_used)):
diskused_rate.append(str(round((float(disk_used[i])/float(total_size[i])*100), 2)) + '%') return diskused_rate,disk_list if __name__ == '__main__': hosts = ['192.168.1.17','192.168.1.17'] for host in hosts: result = caculateDiskUsedRate(host)
diskused_rate = result[0]
partition = result[1] print ("==========",host,'==========') for i in range(len(diskused_rate)):
print ('%-20s used: %s' %(partition[i],diskused_rate[i]))
print()

Python-通過SNMP協議監控網絡卡流量

注意:被監控的機器上需要支援snmp協議 yum install -y net-snmp*

#!/usr/bin/python

import re
import os #get SNMP-MIB2 of the devices
def getAllitems(host,oid):
sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]
return sn1 #get network device
def getDevices(host):
device_mib = getAllitems(host,'RFC1213-MIB::ifDescr')
device_list = [] for item in device_mib:
if re.search('eth',item):
device_list.append(item.split(':')[3].strip())
return device_list #get network date def getDate(host,oid):
date_mib = getAllitems(host,oid)[1:]
date = [] for item in date_mib:
byte = float(item.split(':')[3].strip())
date.append(str(round(byte/1024,2)) + ' KB')
return date if __name__ == '__main__': hosts = ['192.168.1.17','192.168.1.17'] for host in hosts:
device_list = getDevices(host)
inside = getDate(host,'IF-MIB::ifInOctets')
outside = getDate(host,'IF-MIB::ifOutOctets') print '==========' + host + '==========' for i in range(len(inside)):
print '%s : RX: %-15s TX: %s ' % (device_list[i], inside[i], outside[i])
print

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() try: if(temp=="show"):
for i in range(len(ip)):
print(ip[i])
elif( temp1[0] == "user" and temp1[1] != "" ):
print("您的執行引數是:"+temp1[1])
except Exception:
continue

檢查各個程序讀寫的磁碟IO

#!/usr/bin/env python
# -*- coding=utf-8 -*- 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_dump')
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_dump')
sys.exit(0) if __name__=="__main__":
main()

利用Pexpect實現自動非互動登陸linux

#!/usr/bin/env python
# -*- coding: utf-8 -*- 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 += " 記憶體狀態如下:\n"
print_str = print_str + " 系統的記憶體容量為: "+str( mem.total/( memory_convent ) ) + " MB\n"
print_str = print_str + " 系統的記憶體以使用容量為: "+str( mem.used/( memory_convent ) ) + " MB\n"
print_str = print_str + " 系統可用的記憶體容量為: "+str( mem.total/( memory_convent ) - mem.used/( 1024*1024 )) + "MB\n"
print_str = print_str + " 記憶體的buffer容量為: "+str( mem.buffers/( memory_convent ) ) + " MB\n"
print_str = print_str + " 記憶體的cache容量為:" +str( mem.cached/( memory_convent ) ) + " MB\n" #獲取cpu的相關資訊
if ( print_type == 1 ) or isset( sys.argv,"cpu" ) :
print_str += " CPU狀態如下:\n"
cpu_status = psutil.cpu_times()
print_str = print_str + " user = " + str( cpu_status.user ) + "\n"
print_str = print_str + " nice = " + str( cpu_status.nice ) + "\n"
print_str = print_str + " system = " + str( cpu_status.system ) + "\n"
print_str = print_str + " idle = " + str ( cpu_status.idle ) + "\n"
print_str = print_str + " iowait = " + str ( cpu_status.iowait ) + "\n"
print_str = print_str + " irq = " + str( cpu_status.irq ) + "\n"
print_str = print_str + " softirq = " + str ( cpu_status.softirq ) + "\n"
print_str = print_str + " steal = " + str ( cpu_status.steal ) + "\n"
print_str = print_str + " guest = " + str ( cpu_status.guest ) + "\n" #檢視硬碟基本資訊
if ( print_type == 1 ) or isset ( sys.argv,"disk" ) :
print_str += " 硬碟資訊如下:\n"
disk_status = psutil.disk_partitions()
for item in disk_status :
print_str = print_str + " "+ str( item ) + "\n" #檢視當前登入的使用者資訊
if ( print_type == 1 ) or isset ( sys.argv,"user" ) :
print_str += " 登入使用者資訊如下:\n "
user_status = psutil.users()
for item in user_status :
print_str = print_str + " "+ str( item ) + "\n" print_str += "---------------------------------------------------------------\n"
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使用情況 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的namedutples列表
p.num_threads() 程序開啟的執行緒數 #下面的例子中,Popen類的作用是獲取使用者啟動的應用程式程序資訊,以便跟蹤程式程序的執行情況 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生成一個隨機密碼

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random, string
def GenPassword(length): #隨機出數字的個數
numOfNum = random.randint(1,length-1)
numOfLetter = length - numOfNum #選中numOfNum個數字
slcNum = [random.choice(string.digits) for i in range(numOfNum)] #選中numOfLetter個字母
slcLetter = [random.choice(string.ascii_letters) for i in range(numOfLetter)] #打亂這個組合
slcChar = slcNum + slcLetter
random.shuffle(slcChar)
#生成密碼
genPwd = ''.join([i for i in slcChar])
return genPwd
if __name__ == '__main__':
print (GenPassword(6))

一個沒有完成的命令列工具

import sys
import os iplist=[('192.168.1.10', 44123), ('192.168.1.20', 44125), ('192.168.1.30', 44126), ('192.168.1.40', 44127), ('192.168.1.50', 44130)]
cmd="[Shell] # " def list():
for i in range(len(iplist)):
print("主機:%s 埠號:%s"%(iplist[i][0],iplist[i][1])) def use(temp):
for i in range(len(iplist)):
if iplist[i][0] ==temp:
x="["+str(temp)+"] #"
return x
return cmd def main():
while True:
global cmd
try:
shell=str(input(cmd))
if(shell == ""):
continue
elif(shell == "show"):
list()
elif(shell.split(" ")[0].strip() == "use"):
cmd=str(use(shell.split(" ")[1].strip()))
elif(shell=="exit"):
if cmd!="[Shell] # ":
cmd="[Shell] # "
else:
exit(0)
else:
print("未知命令列")
except Exception:
continue

另一個簡單的登入例子

import socket
import os db=["admin","guest","lyshark"] def check(db,recv):
for i in range(len(db)):
if bytes(db[i],encoding="utf-8") == bytes(recv):
return 1
return 0 def sock():
server=socket.socket()
server.bind(("localhost",9999))
server.listen(5) conn,addr=server.accept()
recv_data=conn.recv(1024)
num=check(db,recv_data)
if num ==1:
print("賬戶存在...")
conn.send(bytes("賬戶存在....",encoding="utf-8"))
else:
print("賬戶不存在...")
conn.send(bytes("賬戶不存在....", encoding="utf-8"))
sock()
import socket
import os
client=socket.socket()
client.connect(("localhost",9999)) user=input("輸入名稱:").strip()
client.send(bytes(user,encoding="utf-8")) num=client.recv(1024)
print(str(num,encoding="utf-8"))

常用系統指令碼

監控系統CPU

import psutil

>>> psutil.cpu_times()
scputimes(user=8277.5625, system=2510.953125, idle=31077.65625, interrupt=147.375, dpc=133.0625)
>>> psutil.cpu_times().user
8277.5625
>>> psutil.cpu_count(logical=False)
8
>>> psutil.cpu_percent(interval=1, percpu=True)
[18.5, 14.1, 14.1, 4.7]

監控系統記憶體

>>> import psutil
>>>
>>> psutil.virtual_memory()
svmem(total=8457035776, available=5508038656, percent=34.9, used=2948997120, free=5508038656)
>>> psutil.total
8457035776
>>> psutil.free
5508038656
>>> psutil.swap_memory()
sswap(total=9799213056, used=3736629248, free=6062583808, percent=38.1, sin=0, sout=0)

監控系統磁碟

>>> import psutil
>>>
>>> psutil.disk_usage("C:\\") #獲取指定分割槽(引數)的使用情況
sdiskusage(total=115865546752, used=31459299328, free=84406247424, percent=27.2)
>>> psutil.disk_io_counters() #獲取硬碟總的IO個數,與讀寫資訊
sdiskio(read_count=1577844, write_count=1529528, read_bytes=71110199808, write_bytes=103924939776, read_time=6624, write_time=8764)

監控網路資料包

>>> import psutil
>>>
>>> psutil.net_io_counters()
snetio(bytes_sent=1841534, bytes_recv=11572249, packets_sent=14450, packets_recv=16930, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters().bytes_sent
1841539

管理系統程序資訊

>>> import psutil
>>>
>>> psutil.pids() #列出所有程序號
[0, 4, 360, 544, 636, 708, 716, 808, 880, 304, 384, 1028, 1120]
>>>
>>> p=psutil.Process(1956) #例項化程序PID
>>> p.name() #取程序名字
>>> p.num_threads() #取程序執行緒數
>>> p.memory_percent() #取程序利用率
>>> p.status() #程序狀態
>>> p.exe() #取bin路徑
>>> p.cwd() #程序工作目錄絕對路徑
>>> p.io_counters() #程序IO資訊,包括讀寫IO數及位元組數

文字分詞

#從左到右將字串解析為標記流(stream of tokens)
In [17]: text = 'foo = 23 + 42 * 10' In [18]: tokens= [('NAME','foo'),('EQ','='),('NUM','23'),('PLUS','+'),('NUM','42'),('TIMES','*'),('NUM','
...: 10')] In [19]: import re
#使用正則表示式
InIn [20]: NAME = r'(?P<NAME>[a-zA_][a-zA-Z_0-9]*)' In [21]: NUM = r'(?P<NUM>\d+)' In [22]: PLUS = r'(?P<PLUS>\+)' In [23]: TIMES = r'(?P<TIMES>\*)' In [24]: EQ = r'(?P<EQ>=)' In [25]: WS = r'(?P<WS>\s+)' In [26]: master_pat = re.compile('|'.join([NAME,NUM,PLUS,TIMES,EQ,WS]))
#使用模式物件的scanner()方法來完成分詞操作
In [27]: scanner = master_pat.scanner('foo = 42')
#在給定的文字中重複呼叫match()方法,一次匹配一個模式,下面是匹配過程
In [28]: scanner.match()
Out[28]: <re.Match object; span=(0, 3), match='foo'> In [29]: _.lastgroup,_.group()
Out[29]: ('NAME', 'foo') In [30]: scanner.match()
Out[30]: <re.Match object; span=(3, 4), match=' '> In [31]: _.lastgroup,_.group()
Out[31]: ('WS', ' ') In [32]: scanner.match()
Out[32]: <re.Match object; span=(4, 5), match='='> In [33]: _.lastgroup,_.group()
Out[33]: ('EQ', '=') In [34]: scanner.match()
Out[34]: <re.Match object; span=(5, 6), match=' '> In [35]: _.lastgroup,_.group()
Out[35]: ('WS', ' ') In [36]: scanner.match()
Out[36]: <re.Match object; span=(6, 8), match='42'> In [37]: _.lastgroup,_.group()
Out[37]: ('NUM', '42')
#通過生成器函式來轉化為程式碼的形式
In [40]: from collections import namedtuple In [41]: token = namedtuple('token',['type','value']) In [42]: def generate_tokens(pat,text):
...: scanner = pat.scanner(text)
...: for m in iter(scanner.match,None):
...: yield token(m.lastgroup,m.group())
...: In [43]: for tok in generate_tokens(master_pat,'foo = 42'):
...: print(tok)
...:
token(type='NAME', value='foo')
token(type='WS', value=' ')
token(type='EQ', value='=')
token(type='WS', value=' ')
token(type='NUM', value='42')
#過濾空格標記
In [45]: tokens = (tok for tok in generate_tokens(master_pat,text) if tok.type != 'WS') In [46]: for tok in tokens:print(tok)
token(type='NAME', value='foo')
token(type='EQ', value='=')
token(type='NUM', value='23')
token(type='PLUS', value='+')
token(type='NUM', value='42')
token(type='TIMES', value='*')
token(type='NUM', value='10')

編寫一個簡單的遞迴下降解析器

import re
import collections #定義文字分詞變數
NUM = r'(?P<NUM>\d+)'
PLUS = r'(?P<PLUS>\+)'
MINUS = r'(?P<MINUS>-)'
TIMES = r'(?P<TIMES>\*)'
DIVIDE = r'(?P<DIVIDE>/)'
LPAREN = r'(?P<LPAREN>\()'
RPAREN = r'(?P<RPAREN>\))'
WS = r'(?P<WS>\s+)' master_pat = re.compile('|'.join([NUM,PLUS,MINUS,TIMES,DIVIDE,LPAREN,RPAREN,WS]))
Token = collections.namedtuple('Token',['type','value']) #過濾文字分詞
def generate_tokens(text):
scanner = master_pat.scanner(text)
for m in iter(scanner.match,None):
tok = Token(m.lastgroup,m.group())
if tok.type != 'WS':
yield tok class ExpressionEvaluator:
def parse(self,text):
self.tokens = generate_tokens(text)
self.nexttok = None
self.tok = None
self._advance()
return self.expr() def _advance(self):
self.tok,self.nexttok = self.nexttok,next(self.tokens,None)
def _accept(self,toktype):
if self.nexttok and self.nexttok.type == toktype:
self._advance()
return True
else:
return False
def _expect(self,toktype):
if not self._accept(toktype):
raise SyntaxError('Expected' + toktype) def expr(self):
exprval = self.term()
while self._accept('PLUS') or self._accept('MINUS'):
op = self.tok.type
right = self.term()
if op == 'PLUS':
exprval += right
elif op == 'MINUS':
exprval -= right
return exprval def term(self):
termval = self.factor()
while self._accept('TIMES') or self._accept('DIVIDE'):
op = self.tok.type
right = self.factor()
if op == 'TIMES':
termval *= right
elif op == 'DIVIDE':
termval /= right
return termval def factor(self):
if self._accept('NUM'):
return int(self.tok.value)
elif self._accept('LPAREN'):
exprval = self.expr()
self._expect('RPAREN')
return exprval
else:
raise SyntaxError('Expected NUMBER or LPAREN') if __name__ == '__main__':
e = ExpressionEvaluator()
print(e.parse('2'))
print(e.parse('2 + 3'))
print(e.parse('2 + 3 * 4'))
print(e.parse('2 + (3 + 4) * 5'))

gitlab鉤子指令碼,實現簡單自動化操作

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018-12-18 17:41
# @Author : opsonly
# @Site :
# @File : gitlabCi.py
# @Software: PyCharm from flask import Flask,request,render_template,make_response,Response
import json,os,re,requests
import subprocess app = Flask(__name__)
null = ""
cmd = "/var/www/html/ladmin-devel/"
@app.route('/test',methods=['POST'])
def hello():
json_dict = json.loads(request.data) name = json_dict['event_name']
ref = json_dict['ref'][11:]
project = json_dict['project']['name'] if name == 'push' and ref == 'master':
os.chdir(cmd)
s = subprocess.getoutput('sudo -u nginx composer install')
return Response(s)
else:
return Response('none') if __name__ == '__main__':
app.run(host='0.0.0.0',port=8080) 原文連結:https://blog.csdn.net/weixin_44099558/java/article/details/85775937

下載阿里雲RDS二進位制日誌

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018-12-12 13:52
# @Author : opsonly
# @Site :
# @File : rds_binlog.py
# @Software: PyCharm '''
查詢阿里雲rds binlog日誌
''' import base64,urllib.request
import hashlib
import hmac
import uuid,time,json,wget class RDS_BINLOG_RELATE(object): def __init__(self):
#阿里雲的id和key
self.access_id = '**********************'
self.access_key = '**********************' #通過id和key來進行簽名
def signed(self):
timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
header = {
'Action': 'DescribeBinlogFiles',
'DBInstanceId': 'rm-wz9azm783q621n9',
'StartTime': '2018-07-11T15:00:00Z',
'EndTime': timestamp,
'Format': 'JSON',
'Version': '2014-08-15',
'AccessKeyId': self.access_id,
'SignatureVersion': '1.0',
'SignatureMethod': 'HMAC-SHA1',
'SignatureNonce': str(uuid.uuid1()),
'TimeStamp': timestamp, } #對請求頭進行排序
sortedD = sorted(header.items(), key=lambda x: x[0])
url = 'https://rds.aliyuncs.com'
canstring = '' #將請求引數以#連線
for k, v in sortedD:
canstring += '&' + self.percentEncode(k) + '=' + self.percentEncode(v) #對請求連線進行阿里雲要的編碼規則進行編碼
stiingToSign = 'GET&%2F&' + self.percentEncode(canstring[1:]) bs = self.access_key + '&'
bs = bytes(bs, encoding='utf8')
stiingToSign = bytes(stiingToSign, encoding='utf8')
h = hmac.new(bs, stiingToSign, hashlib.sha1)
stiingToSign = base64.b64encode(h.digest()).strip() #將簽名加入到請求頭
header['Signature'] = stiingToSign #返回url
url = url + "/?" + urllib.parse.urlencode(header)
return url #按照規則替換
def percentEncode(self,store):
encodeStr = store
res = urllib.request.quote(encodeStr)
res = res.replace('+', '%20')
res = res.replace('*', '%2A')
res = res.replace('%7E', '~')
return str(res) #篩選出連結下載二進位制日誌檔案
def getBinLog(self):
binlog_url = self.signed()
req = urllib.request.urlopen(binlog_url)
req = req.read().decode('utf8')
res = json.loads(req) for i in res['Items']['BinLogFile']:
wget.download(i['DownloadLink']) s = RDS_BINLOG_RELATE()
s.getBinLog() ————————————————
原文連結:https://blog.csdn.net/weixin_44099558/java/article/details/85775937