mwilliamson / spur.py

Run commands and manipulate files locally or over SSH using the same interface
BSD 2-Clause "Simplified" License
267 stars 37 forks source link

How do execute more than one command in one session? #34

Closed nishantrangrej8 closed 9 years ago

nishantrangrej8 commented 9 years ago

I am trying to log on to machine. And I want to maintain the same session and execute multiple commands, one after another. Here's what I am trying to do in linux cd /tmp/ java -jar cd /tmp/results ls -lrt

and from the examples you have mentioned, I am trying this: shell = spur.SshShell(hostname="xx.xx.xxx.xxx", username="xxxxx", password="xxxxx", shell_type=spur.ssh.ShellTypes.minimal, missing_host_key=spur.ssh.MissingHostKey.accept) with shell: result = shell.run(["sh", "-c", "cd /tmp/"]) result = shell.run(["sh", "-c", "java -jar "]) result = shell.run(["sh", "-c", "cd /tmp/results"]) result = shell.run(["sh", "-c", "ls -lrt"])
print result.output

It is printing to all files in my directory when i log in. It is like I am logging in to shell everytime when i do shell.run. How can i just log on shell once, maintain a session and run multiple commands and close the session?

mwilliamson commented 9 years ago

You're creating a new shell each time, so the cd command won't have any effect. You can just use the cwd argument instead:

result = shell.run(["sh", "-c", "java -jar "], cwd="/tmp/")
result = shell.run(["sh", "-c", "ls -lrt"], cwd="/tmp/results")

However, cwd isn't supported on minimal shells. Do you need to use a minimal shell type? If so, then you'll need to include the appropriate cd command in the same program as the actual command you want to run:

result = shell.run(["sh", "-c", "cd /tmp/; java -jar "])
result = shell.run(["sh", "-c", "cd /tmp/results; ls -lrt"])
nishantrangrej8 commented 9 years ago

It is working for both. Thanks, mate.