def aliases(): aliases = [] with open(os.path.expanduser('~/.ssh/config'), 'rb') as f: for line in f: stripped = line.strip() if stripped.startswith('Host '): aliases.append(stripped[len('Host '):].strip()) aliases = natural_sort(aliases) print_title('SSH aliases') print('\n'.join(aliases))
def resolve(domain): print_title('system resolve (hosts, dns ...)') try: print(socket.gethostbyname(domain)) except socket.error as e: print('Unable to resolve {d}: {ec.__name__}: {e}'.format(ec=e.__class__, e=e, d=domain)) print() print_title('dns resolve (nslookup)') local('nslookup {}'.format(domain))
def tunnel(ssh_proxy_host = None, remote_host_and_port = None, local_port = None): if None in (ssh_proxy_host, remote_host_and_port, local_port): print("\nUsage:") print("f ssh.tunnel:ssh_proxy_host,remote_host_and_port,local_port") print("f ssh.tunnel:prod_host,service1:1234,4321") return 1 if remote_host_and_port.count(':') != 1: print("remote_host_and_port should be in format 'HOST:PORT'") return 1 remote_host, remote_port = remote_host_and_port.split(':') if not re.match(r'[1-9]+[0-9]*', local_port) or not is_local_port_available(int(local_port)): print(red("Local port {} is used by another process".format(local_port))) return 1 ssh_map = "{}:{}:{}".format(local_port, remote_host, remote_port) cmd = 'ssh -N -C -f -L {} {}'.format(ssh_map, ssh_proxy_host) def get_pid(): with settings(hide('warnings', 'stdout', 'running'), warn_only=True): return local('pgrep -f {}'.format(ssh_map), capture=True) tunnel_pid = get_pid() create = True if tunnel_pid: create = False print("Tunnel ever exists, create new? [y/N]") if raw_input() not in ('y', 'Y'): create = True if create: local(cmd) print_title('SSH tunnel CREATED') tunnel_pid = get_pid() else: print_title('EXISTING SSH tunnel') print('Map: {}'.format(ssh_map)) print('Web Link: {}'.format(blue('http://127.0.0.1:{}/'.format(local_port)))) print('Possible PID: {}'.format(tunnel_pid)) print(yellow('kill {}'.format(tunnel_pid)))
def randtext(only_text=False): # # ж: ) # base_url = b'aHR0cHM6Ly9yZWZlcmF0cy55YW5kZXgucnUvcmVmZXJhdHMvd3JpdGUv\n'.decode('base64') themes = [b'YXN0cm9ub215\n', b'Z2VvbG9neQ==\n', b'Z3lyb3Njb3Bl\n', b'bGl0ZXJhdHVyZQ==\n', b'bWFya2V0aW5n\n', b'bWF0aGVtYXRpY3M=\n', b'bXVzaWM=\n', b'cG9saXQ=\n', b'YWdyb2Jpb2xvZ2lh\n', b'bGF3\n', b'cHN5Y2hvbG9neQ==\n', b'Z2VvZ3JhcGh5\n', b'cGh5c2ljcw==\n', b'cGhpbG9zb3BoeQ==\n', b'Y2hlbWlzdHJ5\n', b'ZXN0ZXRpY2E=\n'] themes = [s.decode('base64') for s in themes] selected_themes = themes[:] random.shuffle(selected_themes) selected_themes = selected_themes[0:random.randint(1, len(themes))] url = b'{base}?t={themes}'.format(base=base_url, themes=b'+'.join(selected_themes)) res = urllib2.urlopen(url).read() def get_parts(tag): matches = re.findall(br'<%s>([^<]+?)</%s>' % (tag, tag), res, re.MULTILINE) if matches: return [s.decode('utf-8') for s in matches] return [] object_ = get_parts('div')[-1] title = get_parts('strong')[-1] paragraphs = '\n\n'.join(get_parts('p')) if only_text: text = paragraphs else: text = '{o}\n{t}\n\n{p}'.format(o=object_.upper(), t=title, p=paragraphs) print_title('Random text generated') print(text) with settings(hide('warnings', 'stdout', 'running'), warn_only=True): local('echo "{}" | pbcopy'.format(text)) print('') print_note('text copied to clipboard')