对于每个IP环路(For Each IP Loop)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
对于每个IP环路(For Each IP Loop)

我是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")

更多推荐

本文发布于:2023-04-20 18:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/89730789632f17e071877d0e610d9e82.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环路   IP   Loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!