
-
Re: Steelscript SteelHead : how to validate connection before using the object ...
Mike Garabedian May 20, 2015 6:56 AM (in response to fservais)Hi Fabien,
The short answer is no - in order to detect whether there is an issue establishing the connection, it needs to be tried first, we don't have a method to test it, but that would be a good addition to the SteelHead package.
The docs are also a little bit inaccurate, the connection doesn't get attempted when the SteelHead object is created, but after the first command gets requested, perhaps that has been part of the confusion. The following code can be used to catch a connection error for SteelHead:
from steelscript.steelhead.core import steelhead
from steelscript.cmdline.exceptions import ConnectionError
from socket import gaierror
host = '<steelhead_hostname>'
user = {'username': '<user>', 'password': '<pass>'}
auth = steelhead.CLIAuth(**user)
sh = steelhead.SteelHead(host=host, auth=auth)
try:
print sh.cli.exec_command('show version')
except gaierror as e:
print 'Error connecting to SteelHead %s' % sh.host
except ConnectionError as e:
print 'Error authenticating to SteelHead %s' % sh.host
The first exception would be for something like an invalid hostname where the error occurs before our code takes over. We should actually be catching that socket error, but until we are able to add a fix for that manually catching it would be required.
Hope that helps,
Mike
-
Re: Steelscript SteelHead : how to validate connection before using the object ...
fservais May 21, 2015 3:24 AM (in response to Mike Garabedian)Mike
Thanks for the prompt reply; I gave it a try today :
- I can now deal with Authentication error with except ConnectionError
- while trying to connect to a host not reachable, I'm not getting the except gaierror ; below the ouput received
ERROR:steelscript.cmdline.sshprocess:Could not connect to <IP Removed>
Traceback (most recent call last):
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/steelscript/cmdline/sshprocess.py", line 58, in connect
self.transport = paramiko.Transport((self._host, self._port))
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/paramiko/transport.py", line 224, in __init__
'Unable to connect to %s: %s' % (hostname, reason))
SSHException: Unable to connect to <IP Removed>: [Errno 110] Connection timed out
Also FYI, I did try script in section 7.2.2.2.2 API Interface show_product_info() with the following result :
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-76-c9dce3cae179> in <module>()
----> 1 pprint(action.show_product_info())
/home/vagrant/virtualenv/local/lib/python2.7/site-packages/steelscript/common/interaction/uidelegation.pyc in __getattr__(self, name)
36 # TODO: Delegate to all of CLI, REST, and Web. Only CLI is currently
37 # supported.
---> 38 return getattr(self.cli_delegatee, name)
39
40 def _get_ui_delegatee(self, class_hint, module_name=None):
AttributeError: 'CLI' object has no attribute 'show_product_info'Fabien
-
Re: Re: Steelscript SteelHead : how to validate connection before using the object ...
Mike Garabedian May 21, 2015 5:22 AM (in response to fservais)Good catch, there are likely other types of exceptions that could occur as well here. Use this snippet instead to better categorize the error types (and catch them all too):
try:
print sh.cli.exec_command('show version')
except ConnectionError as e:
print 'Error authenticating to SteelHead %s' % sh.host
print e
except Exception as e:
print 'Error connecting to SteelHead %s' % sh.host
print e
By catching 'Exception' as a fallback, any exception that gets raised will be caught and printed out. This also avoids having to import the ugly socket error, 'gaierror'.
-
Re: Re: Steelscript SteelHead : how to validate connection before using the object ...
fservais May 21, 2015 5:59 AM (in response to Mike Garabedian)Thanks working better now
-
-
Re: Re: Steelscript SteelHead : how to validate connection before using the object ...
Mike Garabedian May 21, 2015 5:24 AM (in response to fservais)You also caught an error in our tutorial, thanks!
That line should be updated as follows:
FROM:
pprint(action.show_product_info())
TO:
pprint(action.get_product_info())
-
Re: Re: Steelscript SteelHead : how to validate connection before using the object ...
fservais May 21, 2015 6:02 AM (in response to Mike Garabedian)print(action.get_product_info())
{u'release': u'3.1.3b', u'model': u'EX760', u'name': u'SteelHead'}
but with pprint, I'm getting "Pretty printing has been turned OFF" ; I don't think this is linked to my environment as I've been using pprint eariler ( but I'm far from a python expert)
-
Re: Re: Steelscript SteelHead : how to validate connection before using the object ...
Mike Garabedian May 21, 2015 6:33 AM (in response to fservais)This seems like a different issue. A quick google makes it seem like it might be something with IPython?
Maybe typing '%%pprint' would help ...
python - How to make Ipython output a list without line breaks after elements? - Stack Overflow
-
Re: Re: Steelscript SteelHead : how to validate connection before using the object ...
fservais May 21, 2015 9:08 AM (in response to Mike Garabedian)Mike,
Indeed I was working with iPython ; however, I cannot reproduce the pprint 'behavior' anymore ...
No worries & Thanks for your help
Fabien,
-
-
-
-
-