@@ -0,0 +1,3338 @@
+import subprocess, sys, fileinput, argparse%0A%0Adef get_auth_methods(hostname, port=22, verbose=False):%0A    try:%0A        success_output = subprocess.check_output(%5B%0A            'ssh',%0A            '-o', 'StrictHostKeyChecking=no',%0A            '-o', 'PreferredAuthentications=none',%0A            '-p', str(port),%0A            hostname,%0A            'exit'%5D,    # the command to be executed upon success%0A            stderr=subprocess.STDOUT)%0A        # If we make it here, the server allowed us shell access without%0A        # authentication. Thankfully, the 'exit' command should have%0A        # left immediately.%0Amike@semper:~$ cat ssh_auth_methods.py %0Aimport subprocess, sys, fileinput%0A%0Adef get_auth_methods(hostname, port=22, verbose=False):%0A    try:%0A        success_output = subprocess.check_output(%5B%0A            'ssh',%0A            '-o', 'StrictHostKeyChecking=no',%0A            '-o', 'PreferredAuthentications=none',%0A            '-p', str(port),%0A            hostname,%0A            'exit'%5D,    # the command to be executed upon success%0A            stderr=subprocess.STDOUT)%0A        # If we make it here, the server allowed us shell access without%0A        # authentication. Thankfully, the 'exit' command should have%0A        # left immediately.%0A        if verbose:%0A            print('Eek! Server allowed unauthenticated login! Exiting.')%0A        return %5B'none'%5D%0A    # This is in fact the expected case, as we expect the SSH server to%0A    # reject the unauthenticated connection, and therefore expect exit code%0A    # 255, OpenSSH's sole error code.%0A    except subprocess.CalledProcessError as e:%0A        # ssh's result to stderr%0A        result = str(e.output.strip(), 'utf-8')%0A%0A        if e.returncode != 255:%0A            if verbose:%0A                print('Eek! Server allowed unauthenticated login! '%0A                'Also, the command passed had a non-zero exit status.')%0A            return %5B'none'%5D%0A%0A        elif result.startswith('ssh: Could not resolve hostname'):%0A            if verbose:%0A                print('address resolution failed - '%0A                'maybe the server is down, '%0A                'the SSH server is on another port, '%0A                'or your IP is blacklisted?')%0A            raise Exception('resolution of address ' +  hostname + ' failed')%0A%0A        elif result.startswith('Permission denied (') %5C%0A                and result.endswith(').'):%0A            # assume the format specified in the above condition with%0A            # comma-delimited auth methods%0A            return result%5B19:-2%5D.split(',')%0A%0A        else:%0A            raise Exception('unexpected SSH error response: ' + result)%0A%0A%0Adef main():%0A    # the only two currently acceptable argument situations%0A    # a more complex argument system (using argparse, for example) may%0A    # be added later if needed.%0A    if len(sys.argv) == 1 or %5C%0A            len(sys.argv) == 2 and sys.argv%5B1%5D == '--verbose':%0A        verbose = len(sys.argv) == 2%0A%0A        for line in fileinput.input():%0A            hostname = line.strip()%0A            # space-delimited lines%0A            print(hostname, *get_auth_methods(hostname, verbose=verbose))%0A%0A    else:%0A        print('ERROR: input must be line-delimited hostnames from stdin',%0A                file=sys.stdin)%0A        print('usage: python3 ssh_password.py %5B-v%5D',%0A                file=sys.stdin)%0A%0A%0Aif __name__ == '__main__':%0A    main()%0A

add ssh_auth_methods.py