def client_shell(): from pritunl_client import constants constants.set_shell() from pritunl_client import click def get_auth_headers(add_headers=None): response = requests.get('http://localhost:9797/token') headers = { 'Auth-Token': response.content, } if add_headers: headers.update(add_headers) return headers @click.group() def cli(): pass @click.command('daemon', help='Start client service daemon', ) @click.option('--pidfile', help='Path to create pid file', default=None, ) @click.option('--foreground', help='Run daemon in foreground', is_flag=True, ) def daemon_cmd(pidfile, foreground): if not foreground: pid = os.fork() if pid > 0: if pidfile: with open(pidfile, 'w') as pid_file: pid_file.write('%s' % pid) sys.exit(0) else: if pidfile: with open(pidfile, 'w') as pid_file: pid_file.write(str(os.getpid())) from pritunl_client import shell_app shell_app.ShellApp() cli.add_command(daemon_cmd) @click.command('list', help='List imported profiles and status', ) def list_cmd(): response = requests.get( 'http://*****:*****@click.command(name='import', help='Import new profile archive, conf or uri. Can be ' + \ 'path to profile conf or path to archive or profile uri', ) @click.argument('profile_ins', nargs=-1, ) def import_cmd(profile_ins): for profile_in in profile_ins: data = {} if os.path.exists(profile_in): data['profile_path'] = os.path.abspath(profile_in) else: data['profile_uri'] = profile_in response = requests.post( 'http://*****:*****@click.command('remove', help='Remove a profile by profile ID or space separated list of IDs', ) @click.argument('profile_ids', nargs=-1, ) def remove_cmd(profile_ids): for profile_id in profile_ids: response = requests.delete( 'http://*****:*****@click.command('start', help='Start a profile by profile ID or space separated list of IDs', ) @click.argument('profile_ids', nargs=-1, ) @click.option('--password', help='Password for profile if required', default=None, ) def start_cmd(profile_ids, password): for profile_id in profile_ids: if password: headers = { 'Content-type': 'application/json', } data = json.dumps({ 'passwd': password, }) else: headers = None data = None response = requests.put( 'http://*****:*****@click.command('stop', help='Stop a profile by profile ID or space separated list of IDs', ) @click.argument('profile_ids', nargs=-1, ) def stop_cmd(profile_ids): for profile_id in profile_ids: response = requests.put( 'http://*****:*****@click.command('enable', help='Enable a profile to autostart by profile ID or space ' + \ 'separated list of IDs', ) @click.argument('profile_ids', nargs=-1, ) def enable_cmd(profile_ids): for profile_id in profile_ids: response = requests.put( 'http://*****:*****@click.command('disable', help='Disable a profile to stop autostart by profile ID or space ' + \ 'separated list of IDs', ) @click.argument('profile_ids', nargs=-1, ) def disable_cmd(profile_ids): for profile_id in profile_ids: response = requests.put( 'http://localhost:9797/disable/%s' % profile_id, headers=get_auth_headers(), ) if response.status_code == 200: click.echo('Successfully disabled profile') else: click.echo(response.content) sys.exit(1) cli.add_command(disable_cmd) cli()
def client_shell(): from pritunl_client import constants constants.set_shell() from pritunl_client import click def get_auth_headers(add_headers=None): response = requests.get('http://localhost:9797/token') headers = { 'Auth-Token': response.content, } if add_headers: headers.update(add_headers) return headers @click.group() def cli(): pass @click.command( 'daemon', help='Start client service daemon', ) @click.option( '--pidfile', help='Path to create pid file', default=None, ) @click.option( '--foreground', help='Run daemon in foreground', is_flag=True, ) def daemon_cmd(pidfile, foreground): if not foreground: pid = os.fork() if pid > 0: if pidfile: with open(pidfile, 'w') as pid_file: pid_file.write('%s' % pid) sys.exit(0) else: if pidfile: with open(pidfile, 'w') as pid_file: pid_file.write(str(os.getpid())) from pritunl_client import shell_app shell_app.ShellApp() cli.add_command(daemon_cmd) @click.command( 'list', help='List imported profiles and status', ) def list_cmd(): response = requests.get( 'http://*****:*****@click.command(name='import', help='Import new profile archive, conf or uri. Can be ' + \ 'path to profile conf or path to archive or profile uri', ) @click.argument( 'profile_ins', nargs=-1, ) def import_cmd(profile_ins): for profile_in in profile_ins: data = {} if os.path.exists(profile_in): data['profile_path'] = os.path.abspath(profile_in) else: data['profile_uri'] = profile_in response = requests.post( 'http://localhost:9797/import', headers=get_auth_headers({ 'Content-type': 'application/json', }), data=json.dumps(data), ) if response.status_code == 200: click.echo('Successfully imported profile') else: click.echo(response.content) sys.exit(1) cli.add_command(import_cmd) @click.command( 'remove', help='Remove a profile by profile ID or space separated list of IDs', ) @click.argument( 'profile_ids', nargs=-1, ) def remove_cmd(profile_ids): for profile_id in profile_ids: response = requests.delete( 'http://localhost:9797/remove/%s' % profile_id, headers=get_auth_headers(), ) if response.status_code == 200: click.echo('Successfully removed profile') else: click.echo(response.content) sys.exit(1) cli.add_command(remove_cmd) @click.command( 'start', help='Start a profile by profile ID or space separated list of IDs', ) @click.argument( 'profile_ids', nargs=-1, ) @click.option( '--password', help='Password for profile if required', default=None, ) def start_cmd(profile_ids, password): for profile_id in profile_ids: if password: headers = { 'Content-type': 'application/json', } data = json.dumps({ 'passwd': password, }) else: headers = None data = None response = requests.put( 'http://localhost:9797/start/%s' % profile_id, headers=get_auth_headers(headers), data=data, ) if response.status_code == 200: click.echo('Successfully started profile') else: click.echo(response.content) sys.exit(1) cli.add_command(start_cmd) @click.command( 'stop', help='Stop a profile by profile ID or space separated list of IDs', ) @click.argument( 'profile_ids', nargs=-1, ) def stop_cmd(profile_ids): for profile_id in profile_ids: response = requests.put( 'http://*****:*****@click.command('enable', help='Enable a profile to autostart by profile ID or space ' + \ 'separated list of IDs', ) @click.argument( 'profile_ids', nargs=-1, ) def enable_cmd(profile_ids): for profile_id in profile_ids: response = requests.put( 'http://*****:*****@click.command('disable', help='Disable a profile to stop autostart by profile ID or space ' + \ 'separated list of IDs', ) @click.argument( 'profile_ids', nargs=-1, ) def disable_cmd(profile_ids): for profile_id in profile_ids: response = requests.put( 'http://localhost:9797/disable/%s' % profile_id, headers=get_auth_headers(), ) if response.status_code == 200: click.echo('Successfully disabled profile') else: click.echo(response.content) sys.exit(1) cli.add_command(disable_cmd) cli()