Python 如何保存for循环中的第一项
以下是我的脚本:Python 如何保存for循环中的第一项,python,python-2.7,remote-access,Python,Python 2.7,Remote Access,以下是我的脚本: 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 a
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
如何获取第一个用户?在元组或数组中,可以使用括号访问元素:
>>> x = [1,2,3,4,5]
>>> x[0]
1
在元组或数组中,可以使用括号访问元素:
>>> x = [1,2,3,4,5]
>>> x[0]
1