Reputation: 13
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")
Upvotes: 1
Views: 338
Reputation: 336178
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")
Upvotes: 4