python檢查目錄文件權(quán)限并修改目錄文件權(quán)限的操作
我就廢話(huà)不多說(shuō)了,還是直接看代碼吧!
# -*- coding: utf-8 -*-
# @author flynetcn
import sys, os, pwd, stat, datetime;
LOG_FILE = '/var/log/checkDirPermission.log';
nginxWritableDirs = [
'/var/log/nginx',
'/usr/local/www/var',
];
otherReadableDirs = [
'/var/log/nginx',
'/usr/local/www/var/log',
];
dirs = [];
files = [];
def logger(level, str):
logFd = open(LOG_FILE, 'a');
logFd.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')+": "+("WARNING " if level else "NOTICE ")+str);
logFd.close();
def walktree(top, callback):
for f in os.listdir(top):
pathname = os.path.join(top, f);
mode = os.stat(pathname).st_mode;
if stat.S_ISDIR(mode):
callback(pathname, True);
walktree(pathname, callback);
elif stat.S_ISREG(mode):
callback(pathname, False);
else:
logger(1, "walktree skipping %s\n" % (pathname));
def collectPath(path, isDir=False):
if isDir:
dirs.append(path);
else:
files.append(path);
def checkNginxWritableDirs(paths):
uid = pwd.getpwnam('nginx').pw_uid;
gid = pwd.getpwnam('nginx').pw_gid;
for d in paths:
dstat = os.stat(d);
if dstat.st_uid != uid:
try:
os.chown(d, uid, gid);
except:
logger(1, "chown(%s, nginx, nginx) failed\n" % (d));
def checkOtherReadableDirs(paths, isDir=False):
for d in paths:
dstat = os.stat(d);
if isDir:
checkMode = 5;
willBeMode = dstat.st_mode | stat.S_IROTH | stat.S_IXOTH;
else:
checkMode = 4;
willBeMode = dstat.st_mode | stat.S_IROTH;
if int(oct(dstat.st_mode)[-1:]) & checkMode != checkMode:
try:
os.chmod(d, willBeMode);
except:
logger(1, "chmod(%s, %d) failed\n" % (d, oct(willBeMode)));
if __name__ == "__main__":
for d in nginxWritableDirs:
walktree(d, collectPath)
dirs = dirs + files;
checkNginxWritableDirs(dirs);
dirs = [];
files = [];
for d in otherReadableDirs:
walktree(d, collectPath)
checkOtherReadableDirs(dirs, True);
checkOtherReadableDirs(files, False);
補(bǔ)充知識(shí):Python中獲取某個(gè)用戶(hù)對(duì)某個(gè)文件或目錄的訪(fǎng)問(wèn)權(quán)限
在Python中我們通??梢允褂胦s.access()函數(shù)來(lái)獲取當(dāng)前用戶(hù)對(duì)某個(gè)文件或目錄是否有某種權(quán)限,但是要獲取某個(gè)用戶(hù)對(duì)某個(gè)文件或目錄是否有某種權(quán)限python中沒(méi)有很好的方法直接獲取,因此我寫(xiě)了個(gè)函數(shù)使用stat和pwd模塊來(lái)實(shí)現(xiàn)這一功能。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pwd
import stat
def is_readable(path, user):
user_info = pwd.getpwnam(user)
uid = user_info.pw_uid
gid = user_info.pw_gid
s = os.stat(path)
mode = s[stat.ST_MODE]
return (
((s[stat.ST_UID] == uid) and (mode & stat.S_IRUSR > 0)) or
((s[stat.ST_GID] == gid) and (mode & stat.S_IRGRP > 0)) or
(mode & stat.S_IROTH > 0)
)
def is_writable(path, user):
user_info = pwd.getpwnam(user)
uid = user_info.pw_uid
gid = user_info.pw_gid
s = os.stat(path)
mode = s[stat.ST_MODE]
return (
((s[stat.ST_UID] == uid) and (mode & stat.S_IWUSR > 0)) or
((s[stat.ST_GID] == gid) and (mode & stat.S_IWGRP > 0)) or
(mode & stat.S_IWOTH > 0)
)
def is_executable(path, user):
user_info = pwd.getpwnam(user)
uid = user_info.pw_uid
gid = user_info.pw_gid
s = os.stat(path)
mode = s[stat.ST_MODE]
return (
((s[stat.ST_UID] == uid) and (mode & stat.S_IXUSR > 0)) or
((s[stat.ST_GID] == gid) and (mode & stat.S_IXGRP > 0)) or
(mode & stat.S_IXOTH > 0)
)
使用方法
print is_readable('/home', root)
print is_writable('/home', root)
print is_executable('/home', root)
print is_readable('/tmp', admin)
print is_writable('/tmp', admin)
print is_executable('/tmp', admin)
以上這篇python檢查目錄文件權(quán)限并修改目錄文件權(quán)限的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用py2neo操作圖數(shù)據(jù)庫(kù)neo4j的方法詳解
這篇文章主要介紹了Python使用py2neo操作圖數(shù)據(jù)庫(kù)neo4j的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python使用py2neo操作圖數(shù)據(jù)庫(kù)neo4j的具體步驟、原理、相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
python實(shí)現(xiàn)LRU熱點(diǎn)緩存及原理
LRU算法根據(jù)數(shù)據(jù)的歷史訪(fǎng)問(wèn)記錄來(lái)進(jìn)行淘汰數(shù)據(jù),其核心思想是“如果數(shù)據(jù)最近被訪(fǎng)問(wèn)過(guò),那么將來(lái)被訪(fǎng)問(wèn)的幾率也更高”。 。這篇文章主要介紹了python實(shí)現(xiàn)LRU熱點(diǎn)緩存,需要的朋友可以參考下2019-10-10
解決pycharm 遠(yuǎn)程調(diào)試 上傳 helpers 卡住的問(wèn)題
今天小編就為大家分享一篇解決pycharm 遠(yuǎn)程調(diào)試 上傳 helpers 卡住的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
圖文詳解感知機(jī)算法原理及Python實(shí)現(xiàn)
感知機(jī)是二類(lèi)分類(lèi)的線(xiàn)性分類(lèi)模型,其輸入為實(shí)例的特征向量,輸出為實(shí)例的類(lèi)別(取+1和-1二值)。本文將為大家詳細(xì)講講感知機(jī)算法的原理及實(shí)現(xiàn),需要的可以參考一下2022-08-08
Python標(biāo)準(zhǔn)庫(kù)copy的具體使用
copy模塊是Python標(biāo)準(zhǔn)庫(kù)中用于對(duì)象拷貝的核心模塊,提供了淺拷貝(copy)和深拷貝(deepcopy)兩種對(duì)象復(fù)制機(jī)制,本文主要介紹了Python標(biāo)準(zhǔn)庫(kù)copy的具體使用,感興趣的可以了解一下2025-04-04
Python批量將Word文件轉(zhuǎn)為PDF文件的實(shí)現(xiàn)示例
如果想要批量把Word文檔轉(zhuǎn)換為PDF文檔,我們可以使用第三方模塊win32com,本文就來(lái)詳細(xì)的介紹一下Python批量將Word文件轉(zhuǎn)為PDF文件的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-08-08
Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫(kù),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05
Python虛擬環(huán)境與依賴(lài)管理使用方法全指南
這篇文章主要介紹了如何使用虛擬環(huán)境和pip來(lái)管理Python項(xiàng)目的依賴(lài)和包版本,虛擬環(huán)境可以幫助隔離不同項(xiàng)目的依賴(lài),避免版本沖突,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01

