|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import string |
| 4 | +import random |
| 5 | +import subprocess |
| 6 | +import threading |
| 7 | +import time |
| 8 | +import signal |
| 9 | + |
| 10 | + |
| 11 | +class ControlMaster: |
| 12 | + host = "" |
| 13 | + status = "" |
| 14 | + master_socket = "" |
| 15 | + controldir = "" |
| 16 | + debug = False |
| 17 | + masterpid = 0 |
| 18 | + cmdpid = 0 |
| 19 | + |
| 20 | + def __init__(self, hostname, master_socket="", debug=False): |
| 21 | + self.ifdebug("in init") |
| 22 | + self.host = hostname |
| 23 | + self.check_control_dir() |
| 24 | + self.stdout = "" |
| 25 | + self.stderr = "" |
| 26 | + if master_socket == "": |
| 27 | + rnd = "".join(random.choice(string.ascii_letters) for i in range(10)) |
| 28 | + self.master_socket = self.controldir + "/" + rnd |
| 29 | + else: |
| 30 | + self.master_socket = self.controldir + "/" + master_socket |
| 31 | + self.debug = debug |
| 32 | + |
| 33 | + def ifdebug(self, args): |
| 34 | + if self.debug: |
| 35 | + print(args) |
| 36 | + |
| 37 | + def cmd(self, args): |
| 38 | + self.ifdebug("in cmd: executing %s" % args) |
| 39 | + try: |
| 40 | + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 41 | + self.cmdpid = p.pid |
| 42 | + retcode = p.wait() |
| 43 | + self.stdout = p.communicate()[0] |
| 44 | + if retcode < 0: |
| 45 | + print("in cmd: Child was terminated ", -retcode, file=sys.stderr) |
| 46 | + except OSError as e: |
| 47 | + print("in cmd: Execution failed: ", e, file=sys.stderr) |
| 48 | + return retcode |
| 49 | + |
| 50 | + def checkauth(self): |
| 51 | + self.ifdebug("in checkauth") |
| 52 | + if not os.path.exists(self.master_socket): |
| 53 | + self.ifdebug("in checkauth: FAIL: NOFILE") |
| 54 | + return False |
| 55 | + status = self.cmd(["ssh", "-S", self.master_socket, "-O", "check", "go"]) |
| 56 | + if status != 0: |
| 57 | + self.ifdebug("in checkauth: FAIL: NOCONN") |
| 58 | + return False |
| 59 | + return True |
| 60 | + |
| 61 | + def connect(self): |
| 62 | + self.ifdebug("in connect") |
| 63 | + if self.checkauth(): |
| 64 | + return True |
| 65 | + threading.Thread( |
| 66 | + target=self.cmd, args=(["ssh", "-fNMS", self.master_socket, self.host],) |
| 67 | + ).start() |
| 68 | + print("connecting", self.host) |
| 69 | + for i in range(0, 10): |
| 70 | + if self.checkauth(): |
| 71 | + self.masterpid = self.cmdpid |
| 72 | + print("Success") |
| 73 | + return True |
| 74 | + print(".") |
| 75 | + sys.stdout.flush() |
| 76 | + time.sleep(1) |
| 77 | + print("Fail") |
| 78 | + self.masterpid = self.cmdpid |
| 79 | + print("masterpid = %s" % self.masterpid) |
| 80 | + if self.masterpid > 0: |
| 81 | + try: |
| 82 | + os.kill(self.masterpid, signal.SIGTERM) |
| 83 | + except OSError: |
| 84 | + pass |
| 85 | + return False |
| 86 | + |
| 87 | + def disconnect(self): |
| 88 | + self.ifdebug("in disconnect") |
| 89 | + if not self.checkauth(): |
| 90 | + return True |
| 91 | + status = self.cmd(["ssh", "-S", self.master_socket, "-O", "exit", "go"]) |
| 92 | + if status == 0: |
| 93 | + return True |
| 94 | + else: |
| 95 | + print("in disconnect: Fail : %s" % status) |
| 96 | + return False |
| 97 | + |
| 98 | + def put(self, src, dst): |
| 99 | + if not self.checkauth(): |
| 100 | + return False |
| 101 | + self.ifdebug("in put") |
| 102 | + status = self.cmd( |
| 103 | + ["scp", "-o controlpath=" + self.master_socket, src, "go:" + dst] |
| 104 | + ) |
| 105 | + if status == 0: |
| 106 | + return True |
| 107 | + else: |
| 108 | + print("in put: Fail : %s" % status) |
| 109 | + return False |
| 110 | + |
| 111 | + def get(self, src, dst): |
| 112 | + if not self.checkauth(): |
| 113 | + return False |
| 114 | + self.ifdebug("in get") |
| 115 | + status = self.cmd( |
| 116 | + ["scp", "-o controlpath=" + self.master_socket, "go:" + src, dst] |
| 117 | + ) |
| 118 | + if status == 0: |
| 119 | + return True |
| 120 | + else: |
| 121 | + print("in get: Fail : %s" % status) |
| 122 | + return False |
| 123 | + |
| 124 | + def rput(self): |
| 125 | + if not self.checkauth(): |
| 126 | + return False |
| 127 | + self.ifdebug("in rput") |
| 128 | + status = self.cmd( |
| 129 | + ["scp", "-r", "-o controlpath=" + self.master_socket, src, "go:" + dst] |
| 130 | + ) |
| 131 | + if status == 0: |
| 132 | + return True |
| 133 | + else: |
| 134 | + print("in rput: Fail : %s" % status) |
| 135 | + return False |
| 136 | + |
| 137 | + def rget(self): |
| 138 | + if not self.checkauth(): |
| 139 | + return False |
| 140 | + self.ifdebug("in rget") |
| 141 | + status = self.cmd( |
| 142 | + ["scp", "-r", "-o controlpath=" + self.master_socket, "go:" + src, dst] |
| 143 | + ) |
| 144 | + if status == 0: |
| 145 | + return True |
| 146 | + else: |
| 147 | + print("in rget: Fail : %s" % status) |
| 148 | + return False |
| 149 | + |
| 150 | + def exe(self, command): |
| 151 | + self.ifdebug("in exe") |
| 152 | + if not self.checkauth(): |
| 153 | + return False |
| 154 | + status = self.cmd(["ssh", "-S", self.master_socket, "go", command]) |
| 155 | + if status == 0: |
| 156 | + print(self.stdout) |
| 157 | + return status |
| 158 | + else: |
| 159 | + print("in exe: Fail : %s" % status) |
| 160 | + return False |
| 161 | + |
| 162 | + def check_control_dir(self, controldir=os.environ["HOME"] + "/.controlmaster"): |
| 163 | + self.ifdebug("in check_control_dir : controldir = %s" % controldir) |
| 164 | + if not os.path.isdir(controldir): |
| 165 | + os.makedirs(controldir) |
| 166 | + self.controldir = controldir |
| 167 | + |
| 168 | + def create_master_socket(self): |
| 169 | + self.ifdebug("in create_master_socket") |
| 170 | + |
| 171 | + |
| 172 | +__all__ = [ |
| 173 | + "ControlMaster", |
| 174 | +] |
0 commit comments