【发布时间】:2021-08-11 05:09:08
【问题描述】:
我有一个脚本在 linux 上作为 ROOT 运行,收集来自不同用户的数据。给定每个用户的 nfs 路径 1) 验证主管不存在 2) 验证权限被拒绝
verify_model_path_not_found = '/usr/bin/su ' + userID + ' -c \'/usr/bin/ls ' + u_model_path + ' | /usr/bin/grep \"Permission denied\|No such file or directory\"\''
perm_denied_str = 'Permission denied'
no_file_dir_str = 'No such file or directory'
print("verify_model_path_not_found:", verify_model_path_not_found)
#Run as a root
try:
verify_cmd_out = subprocess.check_output(verify_model_path_not_found, shell=True)
verify_cmd_out = str(verify_cmd_out)
print("verify_cmd_out:", verify_cmd_out, "\n")
except subprocess.CalledProcessError as errorcatch:
print(datetime.datetime.now(), " Error while executing ", verify_model_path_not_found)
print("error code", errorcatch.returncode, errorcatch.output, "\n")
continue
#only add items that are not accessible (perm denied or no file found)
if ((perm_denied_str in verify_cmd_out) or (no_file_dir_str in verify_cmd_out)):
#remaining actions .... send an email to the user ...
示例输出错误:
verify_model_path_not_found: /usr/bin/su xxxx -c '/usr/bin/ls /nfs/x/y/z | /usr/bin/grep "Permission denied\|No such file or directory"'
2021-08-10 17:00:31.827186 Error while executing /usr/bin/su xxxx -c '/usr/bin/ls /nfs/x/y/z | /usr/bin/grep "Permission denied\|No such file or directory"'
error code 1 b'' #I know this dir does not exist or perm denied - still getting error
给定 /nfs/x/y/z,如果用户没有读取权限,我想使用 grep 获取“权限被拒绝” - “权限被拒绝”应该是 verify_cmd_out 的值
给定 /nfs/x/y/z,如果目录不存在,我想使用 grep 获取“没有这样的文件或目录” - “没有这样的文件或目录”应该是 verify_cmd_out 的值
一旦 perm 被拒绝或没有为用户确认此类文件,就需要执行某些操作。
/usr/bin/su xxxx -c ... 命令无法正常工作,有什么想法或想法如何解决这个问题?
【问题讨论】: