agentmulti.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import random
  5. import socket
  6. import sys
  7. from ssh import *
  8. def make_connections(n):
  9. connections = []
  10. for _ in range(n):
  11. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  12. s.connect(os.environ["SSH_AUTH_SOCK"])
  13. connections.append(s)
  14. return connections
  15. def use_connection(s, idstring):
  16. print("Trying {}...".format(idstring), end="")
  17. sys.stdout.flush()
  18. s.send(ssh_string(ssh_byte(SSH2_AGENTC_EXTENSION) + ssh_string(
  19. b"nonexistent-agent-extension@putty.projects.tartarus.org")))
  20. length = ssh_decode_uint32(s.recv(4))
  21. assert length < AGENT_MAX_MSGLEN
  22. msg = s.recv(length)
  23. msgtype = msg[0]
  24. msgstring = (
  25. "SSH_AGENT_EXTENSION_FAILURE" if msgtype == SSH_AGENT_EXTENSION_FAILURE
  26. else "SSH_AGENT_FAILURE" if msgtype == SSH_AGENT_FAILURE
  27. else "type {:d}".format(msgtype))
  28. print("got", msgstring, "with {:d}-byte payload".format(len(msg)-1))
  29. def randomly_use_connections(connections, iterations):
  30. for _ in range(iterations):
  31. index = random.randrange(0, len(connections))
  32. s = connections[index]
  33. use_connection(connections[index], "#{:d}".format(index))
  34. def main():
  35. parser = argparse.ArgumentParser(
  36. description='Test handling of multiple agent connections.')
  37. parser.add_argument("--nsockets", type=int, default=128,
  38. help="Number of simultaneous connections to make.")
  39. parser.add_argument("--ntries", type=int, default=1024,
  40. help="Number of messages to send in total.")
  41. args = parser.parse_args()
  42. connections = make_connections(args.nsockets)
  43. randomly_use_connections(connections, args.ntries)
  44. if __name__ == '__main__':
  45. main()