-
Notifications
You must be signed in to change notification settings - Fork 87
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
Add support for reading hosts/subnets from a file #130
base: master
Are you sure you want to change the base?
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. |
@@ -444,6 +465,7 @@ def parse_args_and_env(args=None, environ=os.environ): | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant strip
:
if line.strip().startswith('#'): | |
if line.startswith('#'): |
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
-f
for this feature?Differences to the implementation in #42
fromfile_prefix_chars
, but instead a simple custom parser that knows how to handle comments