diff --git a/labgrid/remote/exporter.py b/labgrid/remote/exporter.py index d3b406503..3e40b8afe 100755 --- a/labgrid/remote/exporter.py +++ b/labgrid/remote/exporter.py @@ -235,7 +235,7 @@ def _start(self, start_params): assert self.local.avail assert self.child is None assert start_params["path"].startswith("/dev/") - self.port = get_free_port() + self.port = get_free_port(os.environ.get("LG_PREFERRED_NETWORKSERIAL_PORT", None)) # Ser2net has switched to using YAML format at version 4.0.0. result = subprocess.run([self.ser2net_bin, "-v"], capture_output=True, text=True) diff --git a/labgrid/util/helper.py b/labgrid/util/helper.py index bc15fd57f..356d0f351 100644 --- a/labgrid/util/helper.py +++ b/labgrid/util/helper.py @@ -15,10 +15,23 @@ re_vt100 = re.compile(r"(\x1b\[|\x9b)[^@-_a-z]*[@-_a-z]|\x1b[@-_a-z]") -def get_free_port(): +def get_free_port(prefered_port=None): """Helper function to always return an unused port.""" with closing(socket(AF_INET, SOCK_STREAM)) as s: - s.bind(('', 0)) + if prefered_port is not None: + port_range = [int(i) for i in prefered_port.split(':')] + port = port_range[0] + max_range = port_range[1] if len(port_range) > 1 else 10 + max_no_of_ports = port + max_range + while port <= max_no_of_ports: + try: + s.bind(('', port)) + except: + port += 1 + else: + break + else: + s.bind(('', 0)) return s.getsockname()[1] diff --git a/man/labgrid-exporter.1 b/man/labgrid-exporter.1 index 7a5bcf953..190e85db9 100644 --- a/man/labgrid-exporter.1 +++ b/man/labgrid-exporter.1 @@ -104,6 +104,10 @@ The following environment variable can be used to configure labgrid\-exporter. .sp This variable can be used to set the default coordinator in the format \fBHOST[:PORT]\fP (instead of using the \fB\-x\fP option). +.SS LG_PREFERRED_NETWORKSERIAL_PORT +.sp +Set a preferred port to be used with NetworkSerialPort resources. +Specify a port and a optional range counting up using the format \fBPORT[:RANGE]\fP .SH EXAMPLES .sp Start the exporter with the configuration file \fImy\-config.yaml\fP: diff --git a/man/labgrid-exporter.rst b/man/labgrid-exporter.rst index 9b80237e7..c2d605aac 100644 --- a/man/labgrid-exporter.rst +++ b/man/labgrid-exporter.rst @@ -96,6 +96,13 @@ LG_COORDINATOR This variable can be used to set the default coordinator in the format ``HOST[:PORT]`` (instead of using the ``-x`` option). +LG_PREFERRED_NETWORKSERIAL_PORT +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Set a preferred port to be used with NetworkSerialPort resources. +Specify a port and a optional range counting up using the format ``PORT[:RANGE]`` + + + EXAMPLES --------