Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.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 我的子流程就像映射了驱动器一样,但它';不是。修理_Python_Windows_Networking_Subprocess - Fatal编程技术网

Python 我的子流程就像映射了驱动器一样,但它';不是。修理

Python 我的子流程就像映射了驱动器一样,但它';不是。修理,python,windows,networking,subprocess,Python,Windows,Networking,Subprocess,我正在使用Spyder解释器在Python 3.7中编写一个脚本,该解释器使用子流程模块将远程网络驱动器映射到计算机,然后检查该驱动器中是否存在文件。例如,我正在将我的计算机上的驱动器映射为“S:\”,但在他们的计算机上它映射为“C:\”。它正在检查C:\上是否存在文件“nircmd.exe”。当我运行代码时,我得到的是: [-] Could not connect to 10.98.80.142 [-] Could not connect to 10.98.80.147 当我打印时:b' 打

我正在使用Spyder解释器在Python 3.7中编写一个脚本,该解释器使用子流程模块将远程网络驱动器映射到计算机,然后检查该驱动器中是否存在文件。例如,我正在将我的计算机上的驱动器映射为“S:\”,但在他们的计算机上它映射为“C:\”。它正在检查C:\上是否存在文件“nircmd.exe”。当我运行代码时,我得到的是:

[-] Could not connect to 10.98.80.142
[-] Could not connect to 10.98.80.147
当我打印时:
b'

打印错误时:
“出现系统错误85。\r\n\r\n本地设备名已在使用。\r\n\r\n'
现在我明白了,这意味着我已经映射了驱动器,但我没有

from subprocess import Popen, PIPE
from os import path

def detectNircmd(computers):
    NIRCMD = "Windows\\System32\\nircmd.exe"
    valids = []
    invalids = []
    for computer in computers:
        try:
            p = Popen(['net', 'use', 'S:', '\\\%s\\c$' % computer],stderr=PIPE,stdin=PIPE,stdout=PIPE)
            out, err = p.communicate()
            if 'success' in out:
                if path.isfile("S:\\%s" % NIRCMD):
                    valids.append(computer)
                    p = Popen(['net', 'use', 'S:', '/delete'],stderr=PIPE,stdin=PIPE,stdout=PIPE)
                else:
                    invalids.append(computer)
                    p = Popen(['net', 'use', 'S:', '/delete'],stderr=PIPE,stdin=PIPE,stdout=PIPE)
            else:
                invalids.append(computer)
                p = Popen(['net', 'use', 'S:', '/delete'],stderr=PIPE,stdin=PIPE,stdout=PIPE)

        except:
            print("[-] Could not connect to %s" % computer)
    print(valids, invalids)

tests = ['10.98.80.142', '10.98.80.147']
detectNircmd(tests)

我发现使用try/except语句时出现了一些问题,因此我在没有使用try/except语句的情况下重写了算法,现在它可以工作了。

您可以通过
import ctypes,string
d=ctypes.windell('kernel32').GetLogicalDrives()
打印([c代表i,c在枚举(string.ascii_大写)中,如果(d>>i)&1])
。要显示刚刚映射的驱动器的详细信息,请运行
import子进程
打印(subprocess.run('net use',capture\u output=True,encoding='oem').stdout)