Get the process IDs that run the command in the remote machine.
(ip, port, cmd_regex)
| 135 | |
| 136 | |
| 137 | def get_remote_pids(ip, port, cmd_regex): |
| 138 | """Get the process IDs that run the command in the remote machine.""" |
| 139 | pids = [] |
| 140 | curr_pid = os.getpid() |
| 141 | # Here we want to get the python processes. We may get some ssh processes, so we should filter them out. |
| 142 | ps_cmd = ( |
| 143 | "ssh -o StrictHostKeyChecking=no -p " |
| 144 | + str(port) |
| 145 | + " " |
| 146 | + ip |
| 147 | + " 'ps -aux | grep python | grep -v StrictHostKeyChecking'" |
| 148 | ) |
| 149 | res = subprocess.run(ps_cmd, shell=True, stdout=subprocess.PIPE) |
| 150 | for p in res.stdout.decode("utf-8").split("\n"): |
| 151 | l = p.split() |
| 152 | if len(l) < 2: |
| 153 | continue |
| 154 | # We only get the processes that run the specified command. |
| 155 | res = re.search(cmd_regex, p) |
| 156 | if res is not None and int(l[1]) != curr_pid: |
| 157 | pids.append(l[1]) |
| 158 | |
| 159 | pid_str = ",".join([str(pid) for pid in pids]) |
| 160 | ps_cmd = ( |
| 161 | "ssh -o StrictHostKeyChecking=no -p " |
| 162 | + str(port) |
| 163 | + " " |
| 164 | + ip |
| 165 | + " 'pgrep -P {}'".format(pid_str) |
| 166 | ) |
| 167 | res = subprocess.run(ps_cmd, shell=True, stdout=subprocess.PIPE) |
| 168 | pids1 = res.stdout.decode("utf-8").split("\n") |
| 169 | all_pids = [] |
| 170 | for pid in set(pids + pids1): |
| 171 | if pid == "" or int(pid) == curr_pid: |
| 172 | continue |
| 173 | all_pids.append(int(pid)) |
| 174 | all_pids.sort() |
| 175 | return all_pids |
| 176 | |
| 177 | |
| 178 | def get_all_remote_pids(hosts, ssh_port, udf_command): |