Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何保存for循环中的第一项_Python_Python 2.7_Remote Access - Fatal编程技术网

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