executable_ssh-sudo 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #! /usr/bin/env cached-nix-shell
  2. #! nix-shell -i python3 -p python3 python3Packages.paramiko
  3. import os
  4. import sys
  5. import paramiko
  6. import warnings
  7. warnings.filterwarnings(action="ignore",module=".*paramiko.*")
  8. os.environ["TERM"] = "dumb"
  9. ssh_user = os.environ.get("SSH_USER", "eng")
  10. ssh_key = os.environ["SSH_KEY"]
  11. ssh_host = sys.argv[1]
  12. become_user = os.environ.get("BECOME_USER", "root")
  13. become_password = os.environ["BECOME_PASSWORD"]
  14. command = " ".join(sys.argv[2:])
  15. user_prompt = ssh_user + "@" + ssh_host.replace(".intr", "") + ":~$ "
  16. root_prompt = become_user + "@" + ssh_host.replace(".intr", "") + " ~ # "
  17. ssh_command = "ssh {}".format(ssh_host)
  18. ssh = paramiko.SSHClient()
  19. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  20. ssh.connect(ssh_host, username=ssh_user, key_filename=ssh_key)
  21. stdin, stdout, stderr = ssh.exec_command("sudo -i {}".format(command), get_pty=True)
  22. stdin.write("{}\n".format(become_password))
  23. stdin.flush()
  24. print("\n".join(stdout.read().decode("utf-8").splitlines()[1:]))
  25. stderr_output=stderr.read().decode("utf-8")
  26. if (len(stderr_output) > 0):
  27. print("\n".join(stderr_output), file=sys.stderr)