Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import paramiko
- import socket
- import sys
- def ihandler(title, instructions, prompt_list):
- results = []
- passwd_type = False
- if len(instructions) > 0:
- print "'" + instructions
- for prompt in prompt_list:
- if prompt[1] == True:
- print ">" + prompt[0]
- passwd_type = False
- else:
- print ":" + prompt[0]
- passwd_type = True
- result = raw_input()
- results.append(result)
- if passwd_type:
- print '{PAM_AUTHTOK=' + result
- return results
- if __name__=="__main__":
- # Get our hostname from the command line
- for arg in sys.argv:
- if 'host' in arg:
- argname, hostname = arg.split('=')
- print ")Username: "
- username = raw_input()
- # paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
- try:
- sock = socket.socket()
- sock.connect((hostname, 22))
- except:
- print '@PAM_SYSTEM_ERR'
- sys.exit(0)
- transport = paramiko.Transport(sock)
- # t.set_log_channel("paramiko.transport")
- transport.start_client()
- try:
- transport.auth_none(username)
- except paramiko.BadAuthenticationType, err:
- allowed_types = err.allowed_types
- for type in allowed_types:
- if type == 'keyboard-interactive':
- try:
- transport.auth_interactive (username, ihandler)
- except paramiko.AuthenticationException, err:
- print '@PAM_AUTH_ERR'
- sys.exit(0)
- break
- if type == 'password':
- # try password authentication
- print ":Password: "
- password = raw_input()
- try:
- transport.auth_password(username, password)
- except paramiko.AuthenticationException, err:
- print '@PAM_AUTH_ERR'
- sys.exit(0)
- print '{PAM_AUTHTOK=' + password
- break
- channel = transport.open_channel(kind='session')
- channel.exec_command('id -u')
- uid = ''
- while not channel.exit_status_ready():
- uid += channel.recv(1024)
- print '=SSH_USER_UID=' + uid.rstrip()
- channel = transport.open_channel(kind='session')
- channel.exec_command('id -g')
- gid = ''
- while not channel.exit_status_ready():
- gid += channel.recv(1024)
- print '=SSH_USER_GID=' + gid.rstrip()
- transport.close()
- print '@PAM_SUCCESS'
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement