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 to pass a variable inside shell.run or shell.spawn #72

Closed joezersk closed 6 years ago

joezersk commented 6 years ago

Hello. First, thank you for a very useful library. I am fairly new to Python and stumbling my way around, so please forgive what might be an elementary question.

I am using Spur to SSH to a networking device and issue shell commands. In one such case, I use something like this:

result = shell.spawn(["reload", "controller", "1"]) This works, as expected.

However, I want to replace the actual number "1" with the value of a variable x. Note x is defined earlier and not shown here.

I tried:

result = shell.spawn(["reload", "controller", x])

...but nothing seems to happen..i.e the controller does not reboot, so the command must simply have been ignored.

Is there a way to pass variables within the shell.spawn process?

Many thanks in advance!

mwilliamson commented 6 years ago

What you've described should work. I'd suggest making sure that x has the value that you expect. So far as the library is concerned, passing a string literal and passing a variable with the same value is indistinguishable.

You could also try taking a look at the output of the spawned process, in case that gives any clues.

On Thu, 18 Jan 2018 13:01:24 +0000 (UTC) Joseph Ezerski notifications@github.com wrote:

Hello. First, thank you for a very useful library. I am fairly new to Python and stumbling my way around, so please forgive what might be an elementary question.

I am using Spur to SSH to a networking device and issue shell commands. In one such case, I use something like this:

result = shell.spawn(["reload", "controller", "1"]) This works, as expected.

However, I want to replace the actual number "1" with the value of a variable x. Note x is defined earlier and not shown here.

I tried:

result = shell.spawn(["reload", "controller", x])

...but nothing seems to happen..i.e the controller does not reboot, so the command must simply have been ignored.

Is there a way to pass variables within the shell.spawn process?

Many thanks in advance!

joezersk commented 6 years ago

Thanks for the quick reply. Can you give me the beginners version of how I would look at the output of the spawned process?

Also, if it helps, after I issue: shell = sshLogin(myDevice[i], myName, myPassword) print x

I do see the proper value of x as desired before moving on to the shell.spawn command.

joezersk commented 6 years ago

Scratch that, I've got it solved. Like any beginner, we try and fail and try again until we figure it out.

To explain, in this code snippet:

            result = shell.spawn(["reload", "controller", x])
            result.stdin_write("Y")

Issuing the "reload controller x" command in the network device immediately prompts "Are you sure (y/n)?" which I then answer via stdin_write. When using a lower case "y" the command never worked. Using a capital "Y" and suddenly things work and variable x is used as expected.

To be more complete, I tried issuing those commands manually on my network device via standard CLI. The odd thing is the both "y" and "Y" work. I am not sure why Spur works with only the capital Y instance. Perhaps that remains a mystery.

Thanks for taking time out to reply to my original question. We can close this issue.

Much appreciation!

mwilliamson commented 6 years ago

No worries, glad you got it sorted.

On Thu, 18 Jan 2018 14:42:17 +0000 (UTC) Joseph Ezerski notifications@github.com wrote:

Scratch that, I've got it solved. Like any beginner, we try and fail and try again until we figure it out.

To explain, in this code snippet:

            result = shell.spawn(["reload", "controller", x])
            result.stdin_write("Y")

Issuing the "reload controller x" command in the network device immediately prompts "Are you sure (y/n)?" which I then answer via stdin_write. When using a lower case "y" the command never worked. Using a capital "Y" and suddenly things work and variable x is used as expected.

To be more complete, I tried issuing those commands manually on my network device via standard CLI. The odd thing is the both "y" and "Y" work. I am not sure why Spur works with only the capital Y instance. Perhaps that remains a mystery.

Thanks for taking time out to reply to this. Much appreciation!