Kailash Prabhu
Kailash Prabhu

Reputation: 5

Seeing multiple connection error I am seeing with Pexpect

Describe the bug and reproduction steps

Getting multiple different errors while trying to log into a remote server server (From MacOS or Linux Rocky8).

could not set shell prompt (received: b"unset PROMPT_COMMAND\r\nPS1='[PEXPECT]\\$ '\r\nset prompt='[PEXPECT]\\$ '\r\nprompt restore;\r\nPS1='[PEXPECT]%(!.#.$) '\r\n", expected: '\\[PEXPECT\\][\\$\\#] ').


could not synchronize with original prompt

could not set shell prompt (received: b"unset PROMPT_COMMAND\r\nPS1='[PEXPECT]\\$ '\r\nLast login: Tue Oct 29 14:22:38 2024\r\r\n[user@remoteserver ~]$ \r\n[user@remoteserver ~]$ \r\n[user@remoteserver ~]$ \r\n[user@remoteserver ~]$ \r\n[user@remoteserver ~]$ unset PROMPT_COMMAND\r\n", expected: '\\[PEXPECT\\][\\$\\#] ').

Script I am using to login,

  attempt = 0
  while attempt < 5:
          try:
              s = pxssh.pxssh(options={"UserKnownHostsFile": "/dev/null", "StrictHostKeyChecking": "no"},
                              timeout=60)
              s.login(host, 'user', ssh_key=get_ssh_key(), sync_multiplier=10, port=22)
              s.sendline('sudo systemctl restart servicename')
              s.prompt()
          except Exception as e:
              logging.info(f"Attempt {attempt + 1}: {e}")
              if 'could not synchronize with original prompt' in str(e):
                  # Optionally unset the prompt or adjust as needed
                  print("Adjusting prompt...")
                  s.prompt_command = None  # Unset the prompt_command
                  # Optionally modify the prompt pattern
                  s.prompt = pexpect.EOF
              attempt += 1

Help me on how to get this resolved.

Operating System: MacOS

Upvotes: 0

Views: 22

Answers (0)

Related Questions