One way to identify who modified a file

I was attempting to identify who had modified one of my script files. I used the stat command to identify when the modifications occurred:

[prodsrvr-PRODDB01] stat check_status.ksh
  File: `check_status.ksh'
  Size: 1117            Blocks: 8          IO Block: 4096   regular file
Device: 17c0fc01h/398523393d    Inode: 473         Links: 1
Access: (0644/-rw-r--r--)  Uid: (24718/  oracle)   Gid: (24718/     dba)
Access: 2017-01-11 19:03:35.972217538 -0500
Modify: 2016-12-19 15:10:07.556675538 -0500
Change: 2016-12-19 15:10:07.000000000 -0500

The above indicates that the modification occurred on December 12, 2016 at 15:10.

I then issued the last command and grepped for Dec to see who was logged on at that date/time:

[prodsrvr-PRODDB01] last | grep -i Dec
user01   pts/1        144.28.20.203    Tue Dec 20 19:18 - 19:33  (00:15)    
user02   pts/0        10.69.96.57      Tue Dec 20 19:10 - 22:22 (1+03:11)   
user03   pts/0        139.49.5.29      Mon Dec 19 15:06 - 21:41  (06:34)    
user05   pts/0        144.28.21.98     Mon Dec 19 11:54 - 13:20  (01:26)    

This indicates a very strong possibility that user03 performed the modification. While not 100% accurate it is a reasonable way to narrow the list of users who may have made the modification.

Calling a function with multiple arguments

The below code snippet demonstrates how you can call a Python function with multiple arguments:

def say_hi (first, last):
    print('Hi {} {}!'.format(name))
 
say_hi('Jane','Doe')

The above line of code is calling with positional parameters. The first parameter is associated with first variable and so on. The function can also be called as

say_hi(last = 'Doe', first = 'Jane')

Python script setting a shell environment variable

os.environ is a useful way to set an environment variable in the shell as shown below:

#!/usr/bin/python
import pdb
import os
import sys
import subprocess
import commands
import time
import socket
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
 
team_name = 'Delta Team'
msg_line = 'The '+team_name+' is executing this script.'
os.environ['msg_line'] = msg_line
os.system('echo "${msg_line}" | mail -s "Email subject goes here" "Dean.Capps@somecompany.com"')

(I may have extra imports as this was taken from a much larger script.)

Python script to list the user account and full name of the current user

I had a requirement to identify the actual user account and full name of the current user who was currently “Sued” to a higher privileged user account such as Oracle. The below script provides me with that functionality:

#!/usr/bin/python
import pdb
import os
import sys
import subprocess
import commands
import time
import socket
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
 
user_name = os.popen("who am i| awk '{print $1}'").read()
user_name=user_name.rstrip('\n')
print user_name
 
os.environ['py_user_name'] = user_name
user_name = os.popen("cat  /etc/passwd | grep ${py_user_name} | awk -F \":\" '{print $5}'").read()
user_name=user_name.rstrip('\n')
print user_name

(I may have extra imports as this was taken from a much larger script.)

142
51
73
77