2

I am trying to run a script over SSH on a remote computer (the script is located on the remote computer). However, when I run Paramiko, all I'm doing is this:

ssh = paramiko.SSHClient()
ssh.connect(-----blacked out-----)
ssh.exec_command("python script.py")

But it's not even executing the command. The script just runs a couple command line commands. The script.py file works just fine if I run it on the remote computer through the remote computer's terminal, but it won't when I try to use ssh to do it like above with paramiko.

Jeffrey Li
  • 41
  • 1
  • 3

3 Answers3

1

You might need to pass the full path to python and/or to the script, sometimes when not executing in terminal/interactive (tty) mode the path is not found as it does not load the profile scripts you would load during an interactive shell.

Totoro
  • 867
  • 9
  • 10
1

Without any info I'd guess that it outputs info which you don't read so it blocks and waits until you do... It's like echoing into the pipe when there is nothing on the other side...

I'd recommend looking into http://stackoverflow.com/a/32758464

MacHala
  • 2,159
  • 1
  • 15
  • 18
0

I had faced a similar problem. In my case, I was executing another process from the ps1 file and the ps1 file was given in the ssh.exec_command() function. my code flow was

ssh = paramiko.SSHClient() 
ssh.connect(Host, Port, Username, Passsword)    
ssh.exec_command(run.ps1) 

# ----------------------
# As per the Expectation : 
#  -> run.ps1 will execute on remote 
#  -> run.ps1 contains "start-process notepad.exe" 
#  -> So, it should spawn a  fresh notepad process
# ----------------------

But, notepad.exe was not started on the remote system

I made the following changes referring to other solutions :

  • Converted all single file paths to absolute paths.
  • Added a wait in the run.ps1 file until the child completes its execution
  • Passed argument in exec_command as "powershell.exe -File Absolute/path/of/file.ps1"
  • maintained log file into paramiko code as paramiko.util.log_to_file('sssh.log')

This time I was able to see that Notepad.exe was running in the background

I hope it will help with the above question

Yuvraj Takey
  • 87
  • 1
  • 11