Comments (8)
What happens if you try using shell_type=spur.ssh.ShellTypes.minimal
in the call to SshShell()
?
from spur.py.
I used shell_type=spur.ssh.ShellTypes.minimal
and added the following line in my code:
except spur.RunProcessError as e: print e.stderr_output
On debugging, it shows
e: return code: 3\noutput: b'apache-tomcat is stopped\n'\nstderr output: b''
However, I am not sure how to get it as an output in the terminal.
from spur.py.
Ah, it's raising an exception because of the return code -- was another part of your code swallowing the exception?
In any case, if you want to always return a result (rather than raising) regardless of the return code, use allow_error=True
in the call to run()
.
from spur.py.
Runs perfectly now. Thanks Mike !
P.S. What do you mean by swallowing an exception. I'm 2 months old to this and would definitely like to learn more about it. Can you point me to a link/page I could refer to get in depth and avoid such mistakes ?
Thanks again !
from spur.py.
Glad you got it working.
The code you originally was raising an exception. Normally, unless you have an except
block, Python should exit with the exception printed out. For instance:
$ python -c 'raise Exception()'
Traceback (most recent call last):
File "<string>", line 1, in <module>
Exception
I was curious whether you saw such a problem, and if not, why not.
from spur.py.
In fact, there's a try
in your code snippet, so being able to see the rest of the actual code would probably help.
from spur.py.
This is my code:
def heathcheck(username, password, server_address, service_name):
try:
shell = spur.SshShell(hostname=server_address,
username=username,
password=password, missing_host_key=spur.ssh.MissingHostKey.accept)
with shell:
result = shell.run(['service', service_name, 'status'], allow_error=True)
if result.output.__contains__('stopped'):
print result.output
result_new = shell.run(['service', service_name, 'start'])
return result_new.output + 'on' + server_address
else:
return result.output + 'on' + server_address
except spur.RunProcessError as e:
print e.stderr_output
except IOError as e:
print e.message
except Exception as e:
failure_list.append(e.message + ' ' + server_address)
print e.message
from spur.py.
Presumably that's the new code though, given that allow_error=True
is in there? I would guess the problem you had before was that the output is in .output
rather than stderror_output
when you were catching the RunProcessError
, which is why you weren't seeing any output, but I could be mistaken.
By "swallowing an exception", I mean behaviour where an exception is caught, but not handled, for instance:
try:
something_that_might_raise_an_exception()
except Exception as error:
pass
I've no idea what your code is meant to do, so can't comment on whether just printing the message is appropriate, but I generally prefer to either allow the exception (wrapped, if appropriate), or return an object that contains the error, so the caller can decide how best to handle the problem.
On an unrelated note: you shouldn't normally need to call magic methods such as __contains__
directly. In this case, "stopped" in result.output
would be idiomatic. Similarly, I think the convention is to call str(error)
rather than error.message
for exceptions (I think message
is deprecated in Python 2 and removed in Python 3).
from spur.py.
Related Issues (20)
- Add ability to specify pty width/height/term
- check_call and check_output HOT 4
- get/put/read_text/read_bytes/write_text/write_bytes/mkdir HOT 10
- send_signal() only applies to parent process HOT 2
- Concrete Minimal Shell Implementation HOT 10
- Add spur to awesome-python HOT 1
- Shell.run() Errors on sudo sed command HOT 2
- Spur fails with message "no handlers could be found for logger "paramiko.transport" HOT 3
- spur fails to connect but with same configuration paramiko works HOT 3
- test failures with python 3.8 HOT 4
- spur.results.RunProcessError: return code: 127 HOT 3
- Add spur to conda-forge HOT 1
- paramiko.ssh_exception.SSHException: Channel closed.
- ~/.ssh/config is being ignored
- stderr output: b'sudo: sorry, you must have a tty to run sudo\n' HOT 1
- Unimplemented method HOT 8
- 0.3.22: test suite uses outdated `nose` HOT 1
- Support for later paramiko HOT 2
- Can't run windows commands HOT 2
- [Question] SSH keepalive
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spur.py.