以下是我的剧本:
from __future__ import print_function
# import statements
import sys, subprocess
import wmi, win32api, win32con
# get the arguments and extract user's IP address
argument = sys.argv[1]
attr_map = dict(item.strip().split('=') for item in argument.split(','))
userIP = attr_map['sender-ip']
print (userIP)
# 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 print error message and exit program
if out.find("Reply from") == -1:
print (userIP, "is NOT pingable.")
sys.exit()
# try to access wmi
try:
c = wmi.WMI(userIP)
except:
print ("Cannot access WMI for", userIP)
sys.exit()
for os in c.Win32_OperatingSystem():
print (os.Caption)
# perform system lookup of IP address
for us in c.Win32_LogonSession():
try:
for user in us.references("Win32_LoggedOnUser"):
print(user.Antecedent.Domain, user.Antecedent.Name, sep="\\")
except:
pass
但它会输出多个用户。
DOMAIN\Glowie
DOMAIN\service_account
DOMAIN\service_account
DOMAIN\service_account
DOMAIN\service_account
如何获得第一位用户?
答案 0 :(得分:3)
在元组或数组中,您可以使用括号来访问元素:
>>> x = [1,2,3,4,5]
>>> x[0]
1