我是Python的新手,我正在尝试编写一个脚本来搜索网络范围,为每个IP映射网络驱动器,并在特定文件夹中的机器上搜索特定文件。
我有IPrange部分工作,以及映射和搜索。 问题是脚本想要一次性运行,这会导致问题,因为映射的驱动器仍在使用中。
我需要它能够运行整个脚本一次,然后转到下一个IP地址。 任何帮助或建议将不胜感激。
import fnmatch import os from netaddr import * IPSet(IPRange('192.168.25.47', '192.168.25.50')) for ip in IPSet(IPRange('192.168.25.47', '192.168.25.50')): os.system(r'net use z: \\%s\c$' % ip) for file in os.listdir('z:\Windows\system32'): if fnmatch.fnmatch(file, 'bob.exe'): print (ip, file) os.system(r"net use Z: /delete /Y")I'm new to Python, and am trying to write a script that will search a network range, map a network drive for each IP, and search for a specific file on the machine in a specific folder.
I have the IPrange part working, as well as the mapping and searching. Problem is that the script wants to run all at once, which causes a problem as the mapped drive is still in use.
I need it to be able to run the whole script once, then move on to the next IP address. Any help or advice will be appreciated.
import fnmatch import os from netaddr import * IPSet(IPRange('192.168.25.47', '192.168.25.50')) for ip in IPSet(IPRange('192.168.25.47', '192.168.25.50')): os.system(r'net use z: \\%s\c$' % ip) for file in os.listdir('z:\Windows\system32'): if fnmatch.fnmatch(file, 'bob.exe'): print (ip, file) os.system(r"net use Z: /delete /Y")最满意答案
缩进在Python中很重要。 您需要缩进部分代码,使其在最外层for循环的每次迭代中运行一次,而不是在它完成后运行:
import fnmatch import os from netaddr import * for ip in IPSet(IPRange('192.168.25.47', '192.168.25.50')): os.system(r'net use z: \\%s\c$' % ip) for file in os.listdir(r'z:\Windows\system32'): if fnmatch.fnmatch(file, 'bob.exe'): print (ip, file) os.system(r"net use Z: /delete /Y")Indentation matters a lot in Python. You need to indent part of your code so it runs once in each iteration of the outermost for loop, not after it has finished:
import fnmatch import os from netaddr import * for ip in IPSet(IPRange('192.168.25.47', '192.168.25.50')): os.system(r'net use z: \\%s\c$' % ip) for file in os.listdir(r'z:\Windows\system32'): if fnmatch.fnmatch(file, 'bob.exe'): print (ip, file) os.system(r"net use Z: /delete /Y")更多推荐
发布评论