Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 """
00030 Change the IP address of a Hokuyo URG Laser
00031 """
00032
00033 import argparse
00034 import socket
00035
00036 def parse_and_validate_ipv4(argument, name):
00037 """
00038 Each address must have 4
00039 """
00040 if len(argument.split(".")) != 4:
00041 print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
00042 exit(-1)
00043 parsed = ""
00044 for x in argument.split("."):
00045 if len(x) > 3:
00046 print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
00047 exit(-1)
00048 while len(x) < 3:
00049 x = "0" + x
00050 parsed += x
00051 return parsed
00052
00053 if __name__ == "__main__":
00054 parser = argparse.ArgumentParser(description=__doc__)
00055 parser.add_argument('new_ip', help='The desired IP address for the laser')
00056 parser.add_argument('new_gw', help='The desired gateway for the laser')
00057 parser.add_argument('--nm', help='The desired netmask for the laser', default="255.255.255.0")
00058 parser.add_argument('--ip', help='The current IP address of the laser', default="192.168.0.10")
00059 args = parser.parse_args()
00060
00061
00062
00063
00064
00065 ip = parse_and_validate_ipv4(args.new_ip, "IP address")
00066 gw = parse_and_validate_ipv4(args.new_gw, "gateway address")
00067 nm = parse_and_validate_ipv4(args.nm, "netmask")
00068 msg = "$IP" + ip + nm + gw + "\x0a"
00069
00070 print("Connecting to %s" % args.ip)
00071 sock = socket.socket()
00072 sock.connect((args.ip, 10940))
00073
00074 print("Updating settings")
00075 sock.send(msg)
00076 try:
00077 sock.settimeout(5)
00078 returned = sock.recv(40)
00079 except socket.timeout:
00080 print("Laser did not return any packet, is probably not updated.")
00081 exit(-1)
00082 if msg != returned:
00083 print("Laser does not appear to have updated")
00084 exit(-1)
00085 print("Done updating, cycle power on laser")