-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPySetBan.py
59 lines (50 loc) · 2.55 KB
/
PySetBan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import json
import sys
import argparse
import subprocess
from time import sleep
parser = argparse.ArgumentParser(description='Ban the peers not running version X\nRequirement: wallet must support the setban command')
parser.add_argument('--outfile', nargs='?', type=str, help='output json file', default="")
parser.add_argument('--infile', nargs='?', type=str, help='input json file', default="")
parser.add_argument('-s', '--seconds', nargs='?', type=int, dest="seconds", default=86400, help='ban for this many seconds')
parser.add_argument('-c', '--cli', nargs='?', type=str, dest="cli", default="numusd", help="wallet cli command")
parser.add_argument('--subver', nargs='?', type=str, default="", help="Accept only this subversion string")
parser.add_argument('--protocol', nargs='?', type=str, default="", help="Accept only this protocol version")
parser.add_argument('-d', '--delay', nargs='?', type=int, dest="delay", default=3, help="Delay between issued ban commands, in seconds. Default:3")
args = parser.parse_args()
print(args.infile)
#path = os.path.join(os.getcwd(),"testproject","peerinfo.json")
if args.infile:
jdata = json.load(open(args.infile))
else:
try:
output = subprocess.check_output([args.cli, "getpeerinfo"])
jdata = json.loads(output)
except subprocess.CalledProcessError as e:
print("getpeerinfo output:\n", e.output)
if args.outfile:
logFile = open(args.outfile, "w+")
for peer in jdata:
if args.subver:
if peer["subver"] != args.subver:
ipaddr = peer["addr"].split(":")[0]
command = args.cli + " setban " + ipaddr + " add " + str(args.seconds)
try:
if 'logFile' in locals():
logFile.write(command + "\n")
retcode = subprocess.call(command, shell=True)
sleep(args.delay)
except OSError as e:
print("Execution failed:", e)
if args.protocol:
if peer["version"] != int(args.protocol):
ipaddr = peer["addr"].split(":")[0]
command = args.cli + " setban " + ipaddr + " add " + str(args.seconds)
try:
if 'logFile' in locals():
logFile.write(command + "\n")
retcode = subprocess.call(command, shell=True)
sleep(args.delay)
except OSError as e:
print("Execution failed:", e)