Execute a shell command via VT. This is blocking and assumes that ssh is being run
(self, cmd, key_accept=False, passwd_retries=3)
| 404 | return re.sub(r"\b" + re.escape(sanitize_text) + r"\b", replace_str, text) |
| 405 | |
| 406 | def _run_cmd(self, cmd, key_accept=False, passwd_retries=3): |
| 407 | """ |
| 408 | Execute a shell command via VT. This is blocking and assumes that ssh |
| 409 | is being run |
| 410 | """ |
| 411 | if not cmd: |
| 412 | return "", "No command or passphrase", 245 |
| 413 | |
| 414 | log_sanitize = None |
| 415 | if self.passwd: |
| 416 | log_sanitize = self.passwd |
| 417 | term = salt.utils.vt.Terminal( |
| 418 | self._split_cmd(cmd), |
| 419 | log_stdout=True, |
| 420 | log_stdout_level="trace", |
| 421 | log_stderr=True, |
| 422 | log_stderr_level="trace", |
| 423 | log_sanitize=log_sanitize, |
| 424 | stream_stdout=False, |
| 425 | stream_stderr=False, |
| 426 | ) |
| 427 | sent_passwd = 0 |
| 428 | send_password = True |
| 429 | ret_stdout = "" |
| 430 | ret_stderr = "" |
| 431 | old_stdout = "" |
| 432 | |
| 433 | try: |
| 434 | while term.has_unread_data: |
| 435 | stdout, stderr = term.recv() |
| 436 | if stdout: |
| 437 | ret_stdout += stdout |
| 438 | buff = old_stdout + stdout |
| 439 | else: |
| 440 | buff = stdout |
| 441 | if stderr: |
| 442 | ret_stderr += stderr |
| 443 | if buff and RSTR_RE.search(buff): |
| 444 | # We're getting results back, don't try to send passwords |
| 445 | send_password = False |
| 446 | if buff and SSH_PRIVATE_KEY_PASSWORD_PROMPT_RE.search(buff): |
| 447 | if not self.priv_passwd: |
| 448 | return "", "Private key file need passphrase", 254 |
| 449 | term.sendline(self.priv_passwd) |
| 450 | continue |
| 451 | if buff and SSH_PASSWORD_PROMPT_RE.search(buff) and send_password: |
| 452 | if not self.passwd: |
| 453 | return ( |
| 454 | "", |
| 455 | "Permission denied, no authentication information", |
| 456 | 254, |
| 457 | ) |
| 458 | if sent_passwd < passwd_retries: |
| 459 | term.sendline(self.passwd) |
| 460 | sent_passwd += 1 |
| 461 | continue |
| 462 | else: |
| 463 | # asking for a password, and we can't seem to send it |