0

Sorry if this post may be a dupplicate for others . I searched other posts and didn't seem to find my answer.

It's my first time working with python paramiko.

I want to use it to execute a python script on a specific filepath on a virtual machine in Windows Server 2018.

The file path is : "C:\Users\mirel.voicu\Desktop\script.py". When i connect with Putty and paramiko the ssh connection works as expected.

enter image description here

When using paramiko and sending the command "cd" it first returns "C:\Users\mirel.voicu"

enter image description here

If instead of "cd" i want to change the path and send command for "C:\Users\mirel.voicu\Desktop" it returns a blank string or bytecode.

enter image description here

I guess my mistake is the way I pass the filepath. How should i write the filepath string to be accepted by paramiko?

Voicu Mirel
  • 113
  • 6
  • *"If instead of "cd" i want to change the path and send command for "C:\Users\mirel.voicu\Desktop" it returns a blank string or bytecode."* – That's correct. Windows (nor *nix) `cd` command does not print anything. Though it makes no sense to execute `cd` command this way. So I guess this is [XY problem](https://meta.stackexchange.com/q/66377/218578). You might be looking for this: [Execute multiple commands in Paramiko so that commands are affected by their predecessors](https://stackoverflow.com/q/49492621/850848). – Martin Prikryl Dec 07 '20 at 16:03
  • hi, i read about this method of pasing one single string of commands separated by ; and i understood it but lest's say for the beginning i only want to change the working directory. What my problem seems to be is when i pass a "cd path/to/file" to paramiko and targeted for windows CLI instead of a linux CLI. Everywhere on the internet i found mostly examples with linux commands that work ok but if i say "cd path/to/file" to a windows terminal in paramiko it doesn't take it. The same command "cd path/to/file executed well in directly in putty CLI for windows. Will keep searching – Voicu Mirel Dec 07 '20 at 17:35
  • *"it doesn't take it"* – What do you mean by that? How you detect that? Once again, `cd` command (both on Windows and Linux) **does not have any output at all**. So it's perfectly ok and expected that `out.read()` returns an empty string. – Martin Prikryl Dec 07 '20 at 17:38
  • if you see the screenshots above where command is simply "cd" stdout.read().decode() returns a string. – Voicu Mirel Dec 07 '20 at 18:02
  • yes, i usually use cd path to change the working directory in CLI. I guess i was looking for the same approach with paramiko exec_command. But as i saw it works differently here – Voicu Mirel Dec 07 '20 at 18:15
  • Anyway, appreciate your help just to let you know, it is clearer now for me than it was before. – Voicu Mirel Dec 07 '20 at 18:20
  • maybe if i want the command to output from c:\users\mirel\ to string c:\users\mirel\desktop i should pass command "cd c:\users\mirel\desktop; cd" (taking you advice from earlier link) – Voicu Mirel Dec 07 '20 at 18:27
  • My previous comment was lost, so once again: In Windows, `cd` without an argument prints the current working directory. But `cd path` does not print anything! + *"But as i saw it works differently here"*: No it not. It behaves exactly the same. + *"i should pass command "cd c:\users\mirel\desktop; cd""*: But why would you do that? What point does this code have? It does not do anything. – Martin Prikryl Dec 08 '20 at 06:52
  • yes, it does. Instead of ";" I used "&&" to tie the windows commands and it worked – Voicu Mirel Dec 08 '20 at 10:37

1 Answers1

0

after looking at the link recommended above and performing some tests i finally understood and found the correct syntax for what i was looking for.

Sorry i was maybe not too clear.

I will try to describe first my purpose and then post the code :

  • change the CWD remotely through paramiko on a remote Windows CLI
  • See the output string of the new changed path of the remote CWD in the local console of my computer .
  • after changing the remote cwd i wanted also to execute some python script and return the result of the execution in my local console window through paramiko.

Code for changing the filepath of the remote cwd and viewing the result string:

command = "cd C:/Users/mirel.voicu/Desktop&&cd"
stdin, out, err = ssh.exec_command(command)
print (out.read().decode())

Command to change the filepath remotely and execute python script:

command = "cd C:/Users/mirel.voicu/Desktop&&python script.py"
stdin, out, err = ssh.exec_command(command)
print (out.read().decode())

It also works pretty good if you have a virtual environment to activate first before running the script:

command = "cd C:/Users/mirel.voicu/Desktop&&conda activate v_env&&python script.py&&conda deactivate"
stdin, out, err = ssh.exec_command(command)
print (out.read().decode())
Voicu Mirel
  • 113
  • 6