Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better validation of ip address for set_urg_ip #64

Open
wants to merge 1 commit into
base: indigo-devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions scripts/set_urg_ip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

# Copyright (c) 2014 Unbounded Robotics Inc.
# Copyright (c) 2014 Unbounded Robotics Inc.
# All right reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand All @@ -11,7 +11,7 @@
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Unbounded Robotics Inc. nor the names of its
# * Neither the name of Unbounded Robotics Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
Expand All @@ -32,23 +32,20 @@

import argparse
import socket
import ipaddress

def parse_and_validate_ipv4(argument, name):
"""
Each address must have 4
Validate IP by parsing it with ipaddress
If not valid, ipaddress will raise a ValueError
"""
if len(argument.split(".")) != 4:
print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
exit(-1)
parsed = ""
for x in argument.split("."):
if len(x) > 3:
print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
exit(-1)
while len(x) < 3:
x = "0" + x
parsed += x
return parsed
ip = str(ipaddress.ip_address(argument))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument need to be cast to unicode, unicode(argument)

parsed_ip = ""
for octet in ip.split("."):
filled_octet = octet.zfill(3)
parsed_ip += filled_octet

return parsed_ip

if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
Expand Down