Skip to content

Commit

Permalink
Merge 2c75240 into c681261
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoutz committed Jun 17, 2014
2 parents c681261 + 2c75240 commit e64306d
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def _in_path(dir):


class Device(object):
ON_JUNOS = platform.system().upper() == 'JUNOS'
ON_JUNOS = platform.system().upper() == 'JUNOS'
auto_probe = 0 # default is no auto-probe

# -------------------------------------------------------------------------
# PROPERTIES
# -------------------------------------------------------------------------

# ------------------------------------------------------------------------
# property: hostname
# ------------------------------------------------------------------------
Expand Down Expand Up @@ -102,6 +102,13 @@ def password(self):
def password(self, value):
self._password = value

@property
def ssh_key(self):
"""
The ssh key to access the Junos device
"""
return self._ssh_key

# ------------------------------------------------------------------------
# property: logfile
# ------------------------------------------------------------------------
Expand Down Expand Up @@ -195,6 +202,7 @@ def _sshconf_lkup(self):
self._hostname = found.get('hostname', self._hostname)
self._port = found.get('port', self._port)
self._auth_user = found.get('user')
self._ssh_key = found.get('ssh_key')

def __init__(self, *vargs, **kvargs):
"""
Expand Down Expand Up @@ -234,12 +242,12 @@ def __init__(self, *vargs, **kvargs):
if self.__class__.ON_JUNOS is True and hostname is None:
# ---------------------------------
# running on a Junos device locally
# ---------------------------------
# ---------------------------------
self._auth_user = None
self._auth_password = None
self._hostname = 'localhost'
else:
# --------------------------
# --------------------------
# making a remote connection
# --------------------------
if hostname is None:
Expand All @@ -252,7 +260,8 @@ def __init__(self, *vargs, **kvargs):
# but if user is explit from call, then use it.
self._auth_user = kvargs.get('user') or self._auth_user
self._auth_password = kvargs.get('password') or kvargs.get('passwd')

self._ssh_key = kvargs.get('ssh_key')

# -----------------------------
# initialize instance variables
# ------------------------------
Expand Down Expand Up @@ -281,7 +290,7 @@ def open(self, *vargs, **kvargs):
for only this open process
kvargs['auto_probe']:
if non-zero then this enables auto_probe and defines the amount
if non-zero then this enables auto_probe and defines the amount
of time/seconds for the probe timeout
"""

Expand All @@ -294,25 +303,34 @@ def open(self, *vargs, **kvargs):
ts_start = datetime.datetime.now()

# open connection using ncclient transport
self._conn = netconf_ssh.connect(
host=self._hostname,
port=self._port,
username=self._auth_user,
password=self._auth_password,
hostkey_verify=False,
device_params={'name': 'junos'})
if self._ssh_key:
self._conn = netconf_ssh.connect(
host=self._hostname,
port=self._port,
username=self._auth_user,
key_filename=self._ssh_key,
hostkey_verify=False,
device_params={'name': 'junos'})
else:
self._conn = netconf_ssh.connect(
host=self._hostname,
port=self._port,
username=self._auth_user,
password=self._auth_password,
hostkey_verify=False,
device_params={'name': 'junos'})

except NcErrors.AuthenticationError as err:
# bad authentication credentials
raise EzErrors.ConnectAuthError(self)

except NcErrors.SSHError as err:
# this is a bit of a hack for now, since we want to
# this is a bit of a hack for now, since we want to
# know if the connection was refused or we simply could
# not open a connection due to reachability. so using
# a timestamp to differentiate the two conditions for now
# if the diff is < 3 sec, then assume the host is
# reachable, but NETCONF connection is refushed.
# if the diff is < 3 sec, then assume the host is
# reachable, but NETCONF connection is refushed.

ts_err = datetime.datetime.now()
diff_ts = ts_err - ts_start
Expand Down Expand Up @@ -340,7 +358,7 @@ def open(self, *vargs, **kvargs):
# anything else, we will re-raise as a
# generic ConnectError
cnx_err = EzErrors.ConnectError(self)
cnx_err._orig = err
cnx_err._orig = err
raise cnx_err

self.connected = True
Expand Down

0 comments on commit e64306d

Please sign in to comment.