def password_prompt(msg='Set password', min_length=8, max_length=40): pwd = None while True: pwd = prompt_for_password(msg) if not min_length <= len(pwd) <= max_length: print_red('Password must be from 8 to 40 symbols. Try again.') continue if not re.findall(r'^[\w\$\^\*\(\)\{\}\[\]\?!@#%&<>:;/+-=]+$', pwd, re.U | re.I): print_red('Password has unallowed symbol. Try again.') continue pwd_confirm = prompt_for_password('Confirm password') if pwd != pwd_confirm: print_red("Passwords aren't matching. Try again.") continue break return pwd
def test_password_prompt_uses_given_password_on_empty_input(): """ prompt_for_password() uses the supplied password if user hits Enter """ eq_( prompt_for_password(env.sudo_prompt, password), password )
def test_password_prompt_ignores_non_prompt_output(): """ prompt_for_password() ignores output which is not a prompt """ eq_( prompt_for_password("blah blah blah this is not a prompt", None), None )
def test_password_prompt_respects_env_sudo_prompt(): """ prompt_for_password() detects env.sudo_prompt() """ eq_( prompt_for_password(env.sudo_prompt, None), password )
def install(): """Begin installation""" print(white("\nBegin installation!\n", True)) print("Before we begin, we need to know where you want to install PyPanel.") print("You can either install remotely or locally.") print("Either way we require root access in order to install.") print("So if you don't have your root password, get it now.") print("Also make sure that you can SSH into the remote/local host as root.\n") env.host = prompt("Enter server hostname or IP:", default="localhost") env.host_string = env.host env.user = prompt("Enter root username:"******"root") env.password = prompt_for_password("Enter root password: "******"\n%s Server is running Ubuntu 10.04" % green("[SUCCESS]", True)) else: print("\n%s Server is not running Ubuntu 10.04" % red("[FAILURE]", True)) exit()
def connect(user): """Get an authenticated pyrax client, list of current boxen""" env.rackspace_user = user if not os.environ.get('RACKSPACE_API_KEY', False): key = prompt_for_password("Rackspace API key for {0}".format(user)) else: key = os.environ['RACKSPACE_API_KEY'] env.rackspace_api_key = key if not os.environ.get('RACKSPACE_TENANT_ID', False): tenant = prompt("Rackspace Tenant ID (account #) for {0}".format(user)) else: tenant = os.environ['RACKSPACE_TENANT_ID'] env.rackspace_tenant_id = tenant pyrax.set_credentials(user, key) refresh_boxen()
def connect(user): """Get an authenticated pyrax client, list of current boxen""" env.rackspace_user = user if not os.environ.get('RACKSPACE_API_KEY', False): key = prompt_for_password("Rackspace API key for {0}".format(user)) else: key = os.environ['RACKSPACE_API_KEY'] env.rackspace_api_key = key if not os.environ.get('RACKSPACE_TENANT_ID', False): tenant = prompt( "Rackspace Tenant ID (account #) for {0}".format(user)) else: tenant = os.environ['RACKSPACE_TENANT_ID'] env.rackspace_tenant_id = tenant pyrax.set_credentials(user, key) refresh_boxen()
def connect_forward(gw, host, port, user): """ Create a different connect that works with a gateway. We really need to create the socket and destroy it when the connection fails and then retry the connect. """ client = ForwardSSHClient() while True: # Load known host keys (e.g. ~/.ssh/known_hosts) unless user says not to. if not s.env.disable_known_hosts: client.load_system_host_keys() # Unless user specified not to, accept/add new, unknown host keys if not s.env.reject_unknown_hosts: client.set_missing_host_key_policy(ssh.AutoAddPolicy()) sock = gw.get_transport().open_channel('direct-tcpip', (host, int(port)), ('', 0)) try: client.connect(host, sock, int(port), user, s.env.password, key_filename=s.env.key_filename, timeout=10) client._sock_ = sock return client except (ssh.AuthenticationException, ssh.PasswordRequiredException, ssh.SSHException), e: if e.__class__ is ssh.SSHException and password: network.abort(str(e)) s.env.password = network.prompt_for_password(s.env.password) sock.close() except (EOFError, TypeError): # Print a newline (in case user was sitting at prompt) print('') sys.exit(0)
def install(): """Begin installation""" print(white("\nBegin installation!\n", True)) print( "Before we begin, we need to know where you want to install PyPanel.") print("You can either install remotely or locally.") print("Either way we require root access in order to install.") print("So if you don't have your root password, get it now.") print( "Also make sure that you can SSH into the remote/local host as root.\n" ) env.host = prompt("Enter server hostname or IP:", default="localhost") env.host_string = env.host env.user = prompt("Enter root username:"******"root") env.password = prompt_for_password("Enter root password: "******"\n%s Server is running Ubuntu 10.04" % green("[SUCCESS]", True)) else: print("\n%s Server is not running Ubuntu 10.04" % red("[FAILURE]", True)) exit()
def connect_forward(gw, host, port, user): """ Create a different connect that works with a gateway. We really need to create the socket and destroy it when the connection fails and then retry the connect. """ client = ForwardSSHClient() while True: # Load known host keys (e.g. ~/.ssh/known_hosts) unless user says not to. if not s.env.disable_known_hosts: client.load_system_host_keys() # Unless user specified not to, accept/add new, unknown host keys if not s.env.reject_unknown_hosts: client.set_missing_host_key_policy(ssh.AutoAddPolicy()) sock = gw.get_transport().open_channel('direct-tcpip', (host, int(port)), ('', 0)) try: client.connect(host, sock, int(port), user, s.env.password, key_filename=s.env.key_filename, timeout=10) client._sock_ = sock return client except ( ssh.AuthenticationException, ssh.PasswordRequiredException, ssh.SSHException ), e: if e.__class__ is ssh.SSHException and password: network.abort(str(e)) s.env.password = network.prompt_for_password(s.env.password) sock.close() except (EOFError, TypeError): # Print a newline (in case user was sitting at prompt) print('') sys.exit(0)
def output_loop(chan, which, capture): # Obtain stdout or stderr related values func = getattr(chan, which) if which == 'recv': prefix = "out" pipe = sys.stdout else: prefix = "err" pipe = sys.stderr host_prefix = "[%s]" % env.host_string if env.colors: host_prefix = env.color_settings['host_prefix'](host_prefix) printing = getattr(output, 'stdout' if (which == 'recv') else 'stderr') # Initialize loop variables reprompt = False initial_prefix_printed = False while True: # Handle actual read/write byte = func(1) if byte == '': break # A None capture variable implies that we're in open_shell() if capture is None: # Just print directly -- no prefixes, no capturing, nada # And since we know we're using a pty in this mode, just go # straight to stdout. _flush(sys.stdout, byte) # Otherwise, we're in run/sudo and need to handle capturing and # prompts. else: _prefix = "[%s] %s: " % (env.host_string, prefix) # Print to user if printing: # Initial prefix if not initial_prefix_printed: _flush(pipe, _prefix) initial_prefix_printed = True # Byte itself _flush(pipe, byte) # Trailing prefix to start off next line if byte in ("\n", "\r"): _flush(pipe, _prefix) # Store in capture buffer capture += byte # Handle prompts prompt = _endswith(capture, env.sudo_prompt) try_again = (_endswith(capture, env.again_prompt + '\n') or _endswith(capture, env.again_prompt + '\r\n')) if prompt: # Obtain cached password, if any password = get_password() # Remove the prompt itself from the capture buffer. This is # backwards compatible with Fabric 0.9.x behavior; the user # will still see the prompt on their screen (no way to avoid # this) but at least it won't clutter up the captured text. del capture[-1*len(env.sudo_prompt):] # If the password we just tried was bad, prompt the user again. if (not password) or reprompt: # Print the prompt and/or the "try again" notice if # output is being hidden. In other words, since we need # the user's input, they need to see why we're # prompting them. if not printing: _flush(pipe, _prefix) if reprompt: _flush(pipe, env.again_prompt + '\n' + _prefix) _flush(pipe, env.sudo_prompt) # Prompt for, and store, password. Give empty prompt so the # initial display "hides" just after the actually-displayed # prompt from the remote end. password = prompt_for_password( prompt=" ", no_colon=True, stream=pipe ) # Update env.password, env.passwords if necessary set_password(password) # Reset reprompt flag reprompt = False # Send current password down the pipe chan.sendall(password + '\n') elif try_again: # Remove text from capture buffer capture = capture[:len(env.again_prompt)] # Set state so we re-prompt the user at the next prompt. reprompt = True
def output_loop(chan, which, capture): # Obtain stdout or stderr related values func = getattr(chan, which) if which == 'recv': prefix = "out" pipe = sys.stdout else: prefix = "err" pipe = sys.stderr host_prefix = "[%s]" % env.host_string if env.colors: host_prefix = env.color_settings['host_prefix'](host_prefix) printing = getattr(output, 'stdout' if (which == 'recv') else 'stderr') # Initialize loop variables reprompt = False initial_prefix_printed = False while True: # Handle actual read/write byte = func(1) if byte == '': break # A None capture variable implies that we're in open_shell() if capture is None: # Just print directly -- no prefixes, no capturing, nada # And since we know we're using a pty in this mode, just go # straight to stdout. _flush(sys.stdout, byte) # Otherwise, we're in run/sudo and need to handle capturing and # prompts. else: _prefix = "%s %s: " % (host_prefix, prefix) # Print to user if printing: # Initial prefix if not initial_prefix_printed: _flush(pipe, _prefix) initial_prefix_printed = True # Byte itself _flush(pipe, byte) # Trailing prefix to start off next line if byte in ("\n", "\r"): _flush(pipe, _prefix) # Store in capture buffer capture += byte # Handle prompts prompt = _endswith(capture, env.sudo_prompt) try_again = (_endswith(capture, env.again_prompt + '\n') or _endswith(capture, env.again_prompt + '\r\n')) if prompt: # Obtain cached password, if any password = get_password() # Remove the prompt itself from the capture buffer. This is # backwards compatible with Fabric 0.9.x behavior; the user # will still see the prompt on their screen (no way to avoid # this) but at least it won't clutter up the captured text. del capture[-1 * len(env.sudo_prompt):] # If the password we just tried was bad, prompt the user again. if (not password) or reprompt: # Print the prompt and/or the "try again" notice if # output is being hidden. In other words, since we need # the user's input, they need to see why we're # prompting them. if not printing: _flush(pipe, _prefix) if reprompt: _flush(pipe, env.again_prompt + '\n' + _prefix) _flush(pipe, env.sudo_prompt) # Prompt for, and store, password. Give empty prompt so the # initial display "hides" just after the actually-displayed # prompt from the remote end. password = prompt_for_password(prompt=" ", no_colon=True, stream=pipe) # Update env.password, env.passwords if necessary set_password(password) # Reset reprompt flag reprompt = False # Send current password down the pipe chan.sendall(password + '\n') elif try_again: # Remove text from capture buffer capture = capture[:len(env.again_prompt)] # Set state so we re-prompt the user at the next prompt. reprompt = True