简体   繁体   中英

How to set the rights of a python-generated file, executed by logrotate

I'm using syslog-ng to write the logs of a cisco pix firewall with a raspberry pi. The logs are rotated daily. After rotation, the last file is passed to a python script, that searches all ip addresses in the log file, then looks up according hostnames (if possible) and replaces the ip addresses with the hostnames. The resulting lines are written in a separate logfile. So far so good.

The problem that I am having is, that the files, generated by the python script, have another GUID as the files generated by syslog-ng.

Here's the output of ls -l as *

-rw-r--r-- 1 root root  82799344 Jul  3 03:45 asa_ip_to_hostnames.log
-rw-r----- 1 root adm  200182806 Jul  3 14:05 asa.log
-rw-r----- 1 root adm  135410305 Jul  3 02:25 asa.log.1
-rw-r----- 1 root adm      46145 Jul  2 13:52 asa.log.2.gz
-rw-r----- 1 root adm      36942 Jul  2 13:50 asa.log.3.gz
-rw-r----- 1 root adm      30969 Jul  2 13:49 asa.log.4.gz
-rw-r----- 1 root adm      55544 Jul  2 13:48 asa.log.5.gz
-rw-r----- 1 root adm      74464 Jul  2 13:46 asa.log.6.gz
-rw-r--r-- 1 root root 153725702 Jul  3 03:45 asa_w_hostnames.log
-rw-r--r-- 1 root root    639542 Jul  2 13:53 asa_w_hostnames.log.1
-rw-r--r-- 1 root root     38303 Jul  2 13:51 asa_w_hostnames.log.2.gz
-rw-r--r-- 1 root root     31992 Jul  2 13:49 asa_w_hostnames.log.3.gz
-rw-r--r-- 1 root root     57687 Jul  2 13:48 asa_w_hostnames.log.4.gz
  • asa.log is generated by syslog-ng
  • asa_w_hostnames.log is generated by my python script (executed through logrotate)
  • asa_ip_to_hostnames.log is the textual output of the python script

As you can see the files that are created through logrotate are associated to root/root, while the asa_log files are associated to adm/root.

I want all files associated to adm/root, so that I can add a read-only samba user to the group adm, so that the logfiles may be copied on a windows system. I don't want to add the samba user to adm AND root.

Any suggestions?

import os
import pwd

admdetails = pwd.getpwnam('adm')
rootdetails = pwd.getpwnam('root')

for fname in ['name1', ,,,]:
   os.chown(fname, rootdetails.pw_uid, admdetails.pr_gid)

Or something like it should be adaptable to the job. NB You will probably need to run as root or sudo as changing ownership away from root without being root is discouraged .

Steve Barnes answer is perfect, it just needed a little tweaking. Here's the final code:

import os, pwd, grp, stat

grpdetails = grp.getgrnam('adm')
rootdetails = pwd.getpwnam('root')

os.chown(filename_out, rootdetails.pw_uid, grpdetails.gr_gid)
os.chmod(filename_out, stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM