login.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. import sys
  3. import socket, os
  4. import json
  5. soket = os.getenv("GREETD_SOCK")
  6. #print(soket)
  7. client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  8. client.connect(soket)
  9. def g_send(json_req):
  10. req = json.dumps(jreq)
  11. client.send( len(req).to_bytes(4,"little") + req.encode())
  12. #client.send( len(req).to_bytes(4,"little") + req.encode())
  13. return client.recv(128)
  14. start = 1
  15. while True:
  16. try:
  17. if start == 1:
  18. username = input("user:")
  19. jreq = {"type": "create_session", "username": username }
  20. g_send(jreq)
  21. start = 2
  22. if start == 2:
  23. password = input("password:")
  24. jreq = {"type": "post_auth_message_response", "response": password}
  25. resp = g_send(jreq)
  26. resp = json.loads(resp.decode("utf-8"))
  27. #print("resp",resp)
  28. start = 3
  29. if start == 3:
  30. cmd = input("cmd:")
  31. jreq = {"type": "start_session", "cmd": cmd.split() }
  32. resp_raw = g_send(jreq)
  33. resp_len = int.from_bytes(resp_raw[0:4],"little")
  34. respt = resp_raw[4:resp_len+4].decode()
  35. resp = json.loads(respt)
  36. #print("resp",resp)
  37. if "error_type" in resp and resp["error_type"] == "auth_error":
  38. start = 1
  39. print("auth error - try again")
  40. elif "type" in resp and resp["type"] == "success":
  41. print(resp)
  42. sys.exit()
  43. except KeyboardInterrupt as k:
  44. print("kapat")
  45. client.close()
  46. break