def main():
    '''
    Provide a generic wrapper around the password_from_netrc() and
    execute_with_prompt() functions, with some options for setting the
    machine, user, and prompt. The command to run should be the first argument
    and will most likely need to be put in quotes.

    Suppose our username is "foo" and we have a ~/.netrc file as follows:

      default login foo password secret
      default login bar password anothersecret
      machine blah login foo password y-a-secret

    If this script is saved as ewp.py, the default should allow for a simple
    command line:

      ewp.py "ssh somewhere ls /etc"

    This will provide ssh with the password 'secret' when prompted.

    If we need to login as as 'bar', the code here can determine this from the
    use of the @ symbol:

      ewp.py "ssh bar@somewhere ls /etc"

    This will provide ssh with the password 'anothersecret' when prompted.

    Finally, logging into the machine 'blah':

      ewp.py -m blah "ssh blah ls /etc"

    This will provide ssh with the password 'y-a-secret' when prompted. We had
    to use the '-m' flag here because the machine name cannot be determined
    automatically (there must be an @ symbol for the present code to work).
    '''
    opt = optparse.OptionParser()
    opt.add_option('--machine', '-m', dest='machine')
    opt.add_option('--user', '-u',    dest='user')
    opt.add_option('--prompt', '-p',  dest='prompt', default='ssword:')
    opts, args = opt.parse_args()

    command = args[0]
    machine = opts.machine
    user = opts.user

    # if machine or user were not specified, try to determine their values
    # from the command line
    if machine is None or user is None:
        match = re.search(r'(\w[\w\-.]+)@(\w[\w\-.]+)', command)
        if match:
            if user is None: user = match.group(1)
            if machine is None: machine = match.group(2)

        # fall-through case: use login name
        if user is None:
            user = os.getlogin()

    password = password_from_netrc(machine, user)

    # run an arbitrary command passed in as the first command line argument
    # (surround the full command with quotes).
    command = args[0]
    for line in execute_with_prompt(command, opts.prompt, password):
        print line

    Provide a generic wrapper around the password_from_netrc() and
    execute_with_prompt() functions, with some options for setting the
    machine, user, and prompt. The command to run should be the first argument
    and will most likely need to be put in quotes.

    Suppose our username is "foo" and we have a ~/.netrc file as follows:

      default login foo password secret
      default login bar password anothersecret
      machine blah login foo password y-a-secret

    If this script is saved as ewp.py, the default should allow for a simple
    command line:

      ewp.py "ssh somewhere ls /etc"

    This will provide ssh with the password 'secret' when prompted.

    If we need to login as as 'bar', the code here can determine this from the
    use of the @ symbol:

      ewp.py "ssh bar@somewhere ls /etc"

    This will provide ssh with the password 'anothersecret' when prompted.

    Finally, logging into the machine 'blah':

      ewp.py -m blah "ssh blah ls /etc"

    This will provide ssh with the password 'y-a-secret' when prompted. We had
    to use the '-m' flag here because the machine name cannot be determined
    automatically (there must be an @ symbol for the present code to work).
    
recipes/Python/572207_Using_Pexpect_netrc_automate_commands_which/recipe-572207.py