Add support for reading hosts/subnets from a file#130
Add support for reading hosts/subnets from a file#130urhot wants to merge 1 commit intodlenski:masterfrom
Conversation
New parameter: --routes-file <filename> or -f <filename> Syntax is one host per line. Single-line or postfix comments are handled and ignored. For example: 1.2.3.4 host1.example.com # This is one more comment host2.example.com
|
Hi all, is there any update on this case? Regards |
|
Hi guys. |
| p = argparse.ArgumentParser() | ||
| p.add_argument('routes', nargs='*', type=net_or_host_param, help='List of VPN-internal hostnames, included subnets (e.g. 192.168.0.0/24), excluded subnets (e.g. %%8.0.0.0/8), or aliases (e.g. host1=192.168.1.2) to add to routing and /etc/hosts.') | ||
| g = p.add_argument_group('Subprocess options') | ||
| p.add_argument('-f', '--routes-file', default=None, type=file_path_param, help='Path to List of VPN-internal hostnames, subnets (e.g. 192.168.0.0/24), or aliases (e.g. host1=192.168.1.2) to add to routing and /etc/hosts.') |
There was a problem hiding this comment.
FileType might be better for handling file as arguments, like this one:
| p.add_argument('-f', '--routes-file', default=None, type=file_path_param, help='Path to List of VPN-internal hostnames, subnets (e.g. 192.168.0.0/24), or aliases (e.g. host1=192.168.1.2) to add to routing and /etc/hosts.') | |
| p.add_argument('-f', '--routes-file', default=None, type=argparse.FileType('r'), help='Path to List of VPN-internal hostnames, subnets (e.g. 192.168.0.0/24), or aliases (e.g. host1=192.168.1.2) to add to routing and /etc/hosts.') |
| lines = [x.strip() for x in f.readlines()] | ||
| for line in lines: |
There was a problem hiding this comment.
No need to read the entire file in memory beforehand, you can just iterate over the lines like this:
| lines = [x.strip() for x in f.readlines()] | |
| for line in lines: | |
| for line in f: | |
| line = line.strip() |
| lines = [x.strip() for x in f.readlines()] | ||
| for line in lines: | ||
| # ignore empty lines | ||
| if not len(line.strip()): |
There was a problem hiding this comment.
Redundant strip:
| if not len(line.strip()): | |
| if not line: |
| if not len(line.strip()): | ||
| continue | ||
| # ignore comment lines | ||
| if line.strip().startswith('#'): |
There was a problem hiding this comment.
Redundant strip:
| if line.strip().startswith('#'): | |
| if line.startswith('#'): |
|
I see this has been approved, but not merged. Any prognosis? |
|
There are some valid suggestions by @marmitar, however I have not gotten around to updating this PR. Partly because I don't currently use vpn-slice myself. |
This pull requests adds new argument to allow reading the hosts/subnets from a file.
This is sort of a duplicate of another pull request #42, but with a different approach. I leave it up to the author to decide if he wants to go forward with either of these.
Usage
New parameter:
--routes-file <filename>or-f <filename>For example:
The routes file should have one host per line, with optional comments:
To be considered
-ffor this feature?Differences to the implementation in #42
fromfile_prefix_chars, but instead a simple custom parser that knows how to handle comments