EDITED FOR CLARITY:
We are using 3rd party software that is supposed to invoke a Python script (that I must create). This script sends values such as sender-ip=10.10.10.10 and the script is supposed to return the userId
This 3rd party software claims if we create a credential file, it will run that Python script using the elevated credentials.
So far, this 3rd party software has IGNORED the credentials file, hence the Python script runs with regular user credentials and does not give the desired output.
When I manually run the Python script using command prompt with elevated credentials, then it works.
I am under pressure to get the 3rd party software and the Python script to "play nice", so I must hardcode the elevated credentials in the script even though I was warned this is not good programming practice.
AND, I must be able to invoke the script by executing
C:\> python myscript.py sender-ip=10.10.10.10
Below is the code
import sys, subprocess, socket, string
import wmi, win32api, win32con
for args in [item.strip('sender-ip=') for item in sys.argv[1:]]:
userIP = args
userloggedon = ""
# subprocess
ping = subprocess.Popen(
["ping", "-n", "1", userIP],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
# can we ping the user's IP address?
out, error = ping.communicate()
# if we cannot ping user's IP address then userID is the error message, and exit
if out.find("Reply from") == -1:
errorMessage = "HOST unreachable."
print 'errorMsg={}'.format(errorMessage)
sys.exit()
# if we cannot access wmi of user's IP address then userID is the error message, and exit
try:
c = wmi.WMI(userIP)
except:
errorMessage= "WMI unreachable"
print 'errorMsg={}'.format(errorMessage)
sys.exit()
# perform system lookup of IP address
user_list = []
for us in c.Win32_LogonSession():
try:
for user in us.references("Win32_LoggedOnUser"):
user_logins = user.Antecedent.Domain + "\\" + user.Antecedent.Name
user_list.append(user_logins)
except:
pass
userloggedon = user_list[0]
print 'userId={}'.format(userloggedon)