Skip to content

Instantly share code, notes, and snippets.

@atucom
Created June 27, 2022 16:34
Show Gist options
  • Save atucom/a84c5bc4f1a2f02dc92274a674f89db8 to your computer and use it in GitHub Desktop.
Save atucom/a84c5bc4f1a2f02dc92274a674f89db8 to your computer and use it in GitHub Desktop.
only return IP if either IP or hostname is supplied
#!/usr/bin/python3
# @atucom
''' parse simple command line arguments '''
import argparse
parser = argparse.ArgumentParser(description='only return IP if either IP or hostname is supplied')
parser.add_argument('target', help='IP or hostname')
args = parser.parse_args()
def ip_filter(input):
''' return ip if already an ip, otherwise resolve hostname to ip '''
import socket
try:
ip = socket.gethostbyname(input)
return ip
except socket.gaierror as e:
print("BAD INPUT: {}".format(input))
except:
return input
ip = ip_filter(args.target)
print(ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment