mirror of
https://github.com/EttusResearch/uhd.git
synced 2026-10-25 17:36:10 +00:00
utils: Fix usrp2_recovery.py for Python3
The script was using invalid string/binary string conversions with Python3. Also, it was moved from optparse to argparse.
This commit is contained in:
parent
01ccb69459
commit
a48747be45
@ -17,42 +17,52 @@ The packet will contain a known ip address to burn into eeprom.
|
||||
Because the recovery packet is sent with a broadcast mac address,
|
||||
only one usrp2 should be present on the interface upon execution.
|
||||
|
||||
This app requires super-user privileges and only works on linux.
|
||||
This app requires super-user privileges and only works on linux.
|
||||
"""
|
||||
|
||||
import socket
|
||||
import struct
|
||||
import optparse
|
||||
import argparse
|
||||
|
||||
BCAST_MAC_ADDR = 'ff:ff:ff:ff:ff:ff'
|
||||
RECOVERY_ETHERTYPE = 0xbeee
|
||||
IP_RECOVERY_CODE = 'addr'
|
||||
IP_RECOVERY_CODE = b'addr'
|
||||
|
||||
def mac_addr_repr_to_binary_string(mac_addr):
|
||||
return ''.join([chr(int(x, 16)) for x in mac_addr.split(':')])
|
||||
"""
|
||||
Convert string representation of MAC address (xx:yy:zz:aa:bb:cc) into a
|
||||
binary string.
|
||||
"""
|
||||
return bytes(bytearray([int(x, 16) for x in mac_addr.split(':')]))
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = optparse.OptionParser(usage='usage: %prog [options]\n'+__doc__)
|
||||
parser.add_option('--ifc', type='string', help='ethernet interface name [default=%default]', default='eth0')
|
||||
parser.add_option('--new-ip', type='string', help='ip address to set [default=%default]', default='192.168.10.2')
|
||||
(options, args) = parser.parse_args()
|
||||
parser = argparse.ArgumentParser(usage='usage: %(prog)s [options]\n'+__doc__)
|
||||
parser.add_argument(
|
||||
'--ifc',
|
||||
help='ethernet interface name [default=%(default)s]',
|
||||
default='eth0')
|
||||
parser.add_argument(
|
||||
'--new-ip',
|
||||
help='ip address to set [default=%(default)s]',
|
||||
default='192.168.10.2')
|
||||
args = parser.parse_args()
|
||||
|
||||
#create the raw socket
|
||||
print("Opening raw socket on interface:", options.ifc)
|
||||
print("Opening raw socket on interface:", args.ifc)
|
||||
soc = socket.socket(socket.PF_PACKET, socket.SOCK_RAW)
|
||||
soc.bind((options.ifc, RECOVERY_ETHERTYPE))
|
||||
soc.bind((args.ifc, RECOVERY_ETHERTYPE))
|
||||
|
||||
#create the recovery packet
|
||||
print("Loading packet with ip address:", options.new_ip)
|
||||
print("Loading packet with ip address:", args.new_ip)
|
||||
packet = struct.pack(
|
||||
'!6s6sH4s4s',
|
||||
mac_addr_repr_to_binary_string(BCAST_MAC_ADDR),
|
||||
mac_addr_repr_to_binary_string(BCAST_MAC_ADDR),
|
||||
RECOVERY_ETHERTYPE,
|
||||
IP_RECOVERY_CODE,
|
||||
socket.inet_aton(options.new_ip),
|
||||
socket.inet_aton(args.new_ip),
|
||||
)
|
||||
|
||||
print("Sending packet (%d bytes)"%len(packet))
|
||||
print("Sending packet ({} bytes)".format(len(packet)))
|
||||
soc.send(packet)
|
||||
print("Done")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user