I am writing a Python program that will download a list of bad IP addresses and add/remove those from a table in OpenBSD pf. The program downloads the list, formats them (as IP address objects), and passes them to the pfctl
command. Right now I have it working as a loop that calls pfctl
once for each string thusly:
for item in addedlist:
subprocess.run(['pfctl', '-t', const.PF_LISTNAME, '-T', 'add',\
str(item)], check=True, stderr=subprocess.DEVNULL)
for item in deletedlist:
subprocess.run(['pfctl', '-t', const.PF_LISTNAME, '-T', 'delete',\
str(item)], check=True, stderr=subprocess.DEVNULL)
where:
addedlist
and deletedlist
are the formatted lists of IP address objects to be added and removed, respectively.
I would like to run the pfctl
command only once for each list and pass addedlist
and deletedlist
to the command in one shot since the lists (coughIPv6*cough*) can be quite lengthy. When I try to do it, though, Python complains about not expecting a list.
Any help would be appreciated.
You'll have to concatenate the item
s to the argument list, not nest the items
lists in it.
We'll also use a list comprehension to call str
on the items.
subprocess.run(
["pfctl", "-t", const.PF_LISTNAME, "-T", "add"]
+ [str(item) for item in addedlist],
check=True,
stderr=subprocess.DEVNULL,
)
subprocess.run(
["pfctl", "-t", const.PF_LISTNAME, "-T", "delete"]
+ [str(item) for item in deletedlist],
check=True,
stderr=subprocess.DEVNULL,
)