0

I am trying to automate an application restarts with Python which is currently implemented with shell scripts. Before running the restart commands, we need to source a .sh environment file which is done in the shell script with . ./opt///**.sh. I am using paramiko and exec_command to run the restart command which throws the error that libraries are missing. So I have tried several ways to source this environment file in the python program but have not been successful.

Things I have tried:

  1. stdin1,stdout1,stderr1 = ssh_client1.exec_command(". ./opt///**.sh")

  2. stdin1,stdout1,stderr1 = ssh_client1.exec_command("source /opt///**.sh")

  3. subprocess.call("/opt///**.sh", shell=True)

  4. os.system(". /opt///**.sh")

  5. I picked the below from stack overflow if I remember correctly enter code herecommand = shlex.split("bash -c 'source /opt///**.sh'") enter code hereproc = subprocess.Popen(command, stdout = subprocess.PIPE) enter code herefor line in proc.stdout: enter code here(key, _, value) = line.partition("=") enter code hereos.environ[key] = value enter code hereproc.communicate()

I still get the same error missing libraries which I am supposed to get when I do not source the environment file when I run the application restart commands using exec_command("/opt///start")

Any help is really appreciated.

Thanks.

coder_3476
  • 93
  • 1
  • 1
  • 5

1 Answers1

0

@Martin Prikryl: Execute multiple commands in Paramiko so that commands are affected by their predecessors

I love you man!! I am writing a py script to remote ssh with paramiko that sources a local environment file before running application restart commands. I was breaking my head over this for over 2 weeks, until I read this post. It works like a charm. Thank you!!

Solution: Each exec_command multiple times is executed in its own "shell". So the previous commands have no effect on an environment of the following commands.

Use && to combine the commands. In this way, the previous command, which in my case is the environment file, has an effect on the following command.

stdin3,stdout3,stderr3 = ssh_client1.exec_command(". /opt///env.sh && STOP")

coder_3476
  • 93
  • 1
  • 1
  • 5