3

I am using pxssh to log in and issue some commands on a remote server.

My code looks like:

def login(self):
  try:
     self.connection = pxssh.pxssh()
     self.password = getpass.getpass('Password: ')
     self.connection.login(self.host, self.username, self.password)
  except pxssh.ExceptionPxssh:
     raise("Failed to login.")

def send_command(self, command):
  try:
     self.connection.sendline(command)
     print(self.connection.before)
  except pxssh.ExceptionPxssh:
     print("Failed to execute command.")

def logout(self):
  try:
    self.connection.logout()
  except pxssh.ExceptionPxssh:
    print("Failed to logout.")

The output of running some commands is:

Downloading Terraform package.
b" unset PROMPT_COMMAND\r\n\x1b]0;test_user@d01c1ad4ad1c: ~\x07\x1b[01;32mtest_user@d01c1ad4ad1c\x1b[00m:\x1b[01;34m~\x1b[00m$ PS1='[PEXPECT]\\$ '\r\n"
Unzipping Terraform package
b" unset PROMPT_COMMAND\r\n\x1b]0;test_user@d01c1ad4ad1c: ~\x07\x1b[01;32mtest_user@d01c1ad4ad1c\x1b[00m:\x1b[01;34m~\x1b[00m$ PS1='[PEXPECT]\\$ '\r\n"
Cleaning up the terraform_0.11.2_linux_amd64.zip package
b" unset PROMPT_COMMAND\r\n\x1b]0;test_user@d01c1ad4ad1c: ~\x07\x1b[01;32mtest_user@d01c1ad4ad1c\x1b[00m:\x1b[01;34m~\x1b[00m$ PS1='[PEXPECT]\\$ '\r\n"
Move Terraform file
b" unset PROMPT_COMMAND\r\n\x1b]0;test_user@d01c1ad4ad1c: ~\x07\x1b[01;32mtest_user@d01c1ad4ad1c\x1b[00m:\x1b[01;34m~\x1b[00m$ PS1='[PEXPECT]\\$ '\r\n"

I would like to completely remove all lines in the output that contains unset. Is there any attribute that does that or is there any workaround for that?

accdias
  • 5,160
  • 3
  • 19
  • 31
Albert
  • 191
  • 1
  • 3
  • 23
  • If possible then could you share the details code? – Sabil Aug 26 '21 at 20:51
  • @Sabil: What causes this extra line that I would like to remove is because of the pexpect library or the Linux OS. Any suggestions are welcomed. – Albert Aug 30 '21 at 21:58

1 Answers1

0

From the output of your commands I assume you are running linux.

As described here and here, it seems the PROMPT_COMMAND affects your prompt. To disable it try to run:

$unset PROMPT_COMMAND

and for the permanent effect add this to .bashrc.

R. Marolahy
  • 1,325
  • 7
  • 17
  • Thanks for your suggestion. Unfortunatelly, it doesn't work. I've been added this line in .bashrc or /etc/profile, but still I get the extra lines when I run the script. Any other workaround that can get the job done? – Albert Aug 30 '21 at 21:59
  • Make sure you didn't forget to `source .bashrc`. – R. Marolahy Aug 31 '21 at 04:52