Skip to content

Commit e839e48

Browse files
author
Casey Boettcher
committed
Initial commit.
0 parents  commit e839e48

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.idea
3+
out
4+
gen
5+
xforce.cfg
6+

README.md

Whitespace-only changes.

main.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import pprint
2+
import argparse
3+
import requests
4+
import IPy
5+
6+
XFORCE_API_BASE = 'https://api.xforce.ibmcloud.com'
7+
XFORCE_API_IP_REP = 'ipr'
8+
9+
10+
def parse_args():
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument('-x', '--xforce', required=True, type=argparse.FileType('r'), help='Path to a file containing '
13+
'your X-Force credentials.')
14+
parser.add_argument('address', nargs='?', help='An IP address to be checked via X-Force')
15+
return parser.parse_args()
16+
17+
18+
def request_valid_ip():
19+
ip = input("Enter a valid IP address you would like to check: ")
20+
return validate_ip(ip)
21+
22+
23+
def validate_ip(ip):
24+
try:
25+
ipobj = IPy.IP(ip)
26+
if ipobj.iptype() == 'PRIVATE':
27+
print("IP addresses should not be in private network ranges")
28+
ip = None
29+
except ValueError as ve:
30+
print("Invalid IP: {}".format(ve.args))
31+
ip = None
32+
finally:
33+
return ip
34+
35+
36+
def read_in_xforce_keys(file):
37+
for x in range(0, 2):
38+
if x == 0:
39+
key = file.readline().strip()
40+
if x == 1:
41+
password = file.readline().strip()
42+
return key, password
43+
44+
45+
def main():
46+
ip = None
47+
args = parse_args()
48+
49+
# get user-supplied IP address
50+
if args.address:
51+
ip = validate_ip(args.address)
52+
while not ip:
53+
ip = request_valid_ip()
54+
55+
url = "{}/{}/{}".format(XFORCE_API_BASE, XFORCE_API_IP_REP, ip)
56+
57+
# get X-Force API keys
58+
creds = read_in_xforce_keys(args.xforce)
59+
result = requests.get(url, auth=(creds[0], creds[1]))
60+
pprint.pprint(result.json())
61+
return 0
62+
63+
64+
if __name__ == '__main__':
65+
main()

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests>=2.18.4
2+
IPy>=0.83

0 commit comments

Comments
 (0)