Advertisement
Guest User

Untitled

a guest
May 24th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import paramiko
  4. import socket
  5. import sys
  6.  
  7. def ihandler(title, instructions, prompt_list):
  8. results = []
  9. passwd_type = False
  10. if len(instructions) > 0:
  11. print "'" + instructions
  12. for prompt in prompt_list:
  13. if prompt[1] == True:
  14. print ">" + prompt[0]
  15. passwd_type = False
  16. else:
  17. print ":" + prompt[0]
  18. passwd_type = True
  19. result = raw_input()
  20. results.append(result)
  21. if passwd_type:
  22. print '{PAM_AUTHTOK=' + result
  23. return results
  24.  
  25. if __name__=="__main__":
  26. # Get our hostname from the command line
  27. for arg in sys.argv:
  28. if 'host' in arg:
  29. argname, hostname = arg.split('=')
  30.  
  31. print ")Username: "
  32. username = raw_input()
  33.  
  34. # paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
  35. try:
  36. sock = socket.socket()
  37. sock.connect((hostname, 22))
  38. except:
  39. print '@PAM_SYSTEM_ERR'
  40. sys.exit(0)
  41.  
  42. transport = paramiko.Transport(sock)
  43.  
  44. # t.set_log_channel("paramiko.transport")
  45. transport.start_client()
  46. try:
  47. transport.auth_none(username)
  48. except paramiko.BadAuthenticationType, err:
  49. allowed_types = err.allowed_types
  50.  
  51. for type in allowed_types:
  52. if type == 'keyboard-interactive':
  53. try:
  54. transport.auth_interactive (username, ihandler)
  55. except paramiko.AuthenticationException, err:
  56. print '@PAM_AUTH_ERR'
  57. sys.exit(0)
  58. break
  59.  
  60. if type == 'password':
  61. # try password authentication
  62. print ":Password: "
  63. password = raw_input()
  64. try:
  65. transport.auth_password(username, password)
  66. except paramiko.AuthenticationException, err:
  67. print '@PAM_AUTH_ERR'
  68. sys.exit(0)
  69.  
  70. print '{PAM_AUTHTOK=' + password
  71. break
  72.  
  73. channel = transport.open_channel(kind='session')
  74. channel.exec_command('id -u')
  75. uid = ''
  76. while not channel.exit_status_ready():
  77. uid += channel.recv(1024)
  78. print '=SSH_USER_UID=' + uid.rstrip()
  79.  
  80. channel = transport.open_channel(kind='session')
  81. channel.exec_command('id -g')
  82. gid = ''
  83. while not channel.exit_status_ready():
  84. gid += channel.recv(1024)
  85. print '=SSH_USER_GID=' + gid.rstrip()
  86.  
  87. transport.close()
  88. print '@PAM_SUCCESS'
  89. sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement