Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions pycsco/nxos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

try:
import xmltodict
import os.path
import yaml
import json
from os.path import expanduser
from nxapi import NXAPI
from error import CLIError
except ImportError as e:
print '***************************'
print e
print '***************************'
import json
import os.path
from os.path import expanduser

import xmltodict
import yaml

from pycsco.nxos.error import CLIError
from pycsco.nxos.nxapi import NXAPI


class Auth():
Expand Down Expand Up @@ -110,10 +107,14 @@ def show(self, command, fmat='xml', text=False):

data = self.sw1.send_req()

raw_data = data[1]
if isinstance(raw_data, bytes):
raw_data = raw_data.decode('utf8')

if fmat == 'xml':
data_dict = xmltodict.parse(data[1])
data_dict = xmltodict.parse(raw_data)
elif fmat == 'json':
data_dict = json.loads(data[1])
data_dict = json.loads(raw_data)

clierror = self.cli_error_check(data_dict)
if clierror:
Expand All @@ -127,11 +128,15 @@ def config(self, command, fmat='xml'):
self.sw1.set_cmd(command)

data = self.sw1.send_req()
# return self.sw1.send_req

raw_data = data[1]
if isinstance(raw_data, bytes):
raw_data = raw_data.decode('utf8')

if fmat == 'xml':
data_dict = xmltodict.parse(data[1])
data_dict = xmltodict.parse(raw_data)
elif fmat == 'json':
data_dict = json.loads(data[1])
data_dict = json.loads(raw_data)

clierror = self.cli_error_check(data_dict)
if clierror:
Expand Down
67 changes: 43 additions & 24 deletions pycsco/nxos/nxapi.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#!/usr/bin/env python
# Copyright (C) 2013 Cisco Systems Inc.
# All rights reserved
try:
import urllib2
import contextlib
import base64
import socket
import httplib
import base64
import contextlib
import socket
import ssl
from sys import version_info

PY3 = version_info >= (3, )

if PY3:
# Python 3
from urllib.request import urlopen, Request
from http.client import HTTPConnection, HTTPS_PORT
else:
# Python 2
from urllib2 import urlopen, Request
from httplib import HTTPConnection, HTTPS_PORT
import ssl
except ImportError as e:
print '***************************'
print e
print '***************************'


class HTTPSConnection(HTTPConnection):
Expand Down Expand Up @@ -67,7 +71,10 @@ def connect(self):
# line below. The entire HTTPSConnection class should be removed sometime in
# the future. // jonas@stenling.se
#
#httplib.HTTPSConnection = HTTPSConnection
# if PY3:
# http.client.HTTPSConnection = HTTPSConnection
# else:
# httplib.HTTPSConnection = HTTPSConnection


class RequestMsg:
Expand Down Expand Up @@ -123,8 +130,16 @@ def __init__(
self.username = username
self.password = password
self.url = url
self.base64_str = base64.encodestring('%s:%s' % (username,
password)).replace('\n', '')
auth_str = '%s:%s' % (username, password)
if PY3:
auth_str = auth_str.encode('utf8')

base64_str = base64.b64encode(auth_str) #.replace('\n', '')

if PY3:
base64_str = base64_str.decode('utf8')

self.base64_str = base64_str.replace('\n', '')

def get_resp(
self,
Expand All @@ -133,17 +148,19 @@ def get_resp(
timeout,
):

req = urllib2.Request(self.url, req_str)
if PY3:
req_str = req_str.encode('utf8')

req = Request(self.url, req_str)
req.add_header('Authorization', 'Basic %s' % self.base64_str)
req.add_header('Cookie', '%s' % cookie)
try:
with contextlib.closing(urllib2.urlopen(req,
timeout=timeout)) as resp:
with contextlib.closing(urlopen(req, timeout=timeout)) as resp:
resp_str = resp.read()
resp_headers = resp.info()
return (resp_headers, resp_str)
except socket.timeout, e:
print 'Req timeout'
except socket.timeout as e:
print('Req timeout')
raise


Expand All @@ -169,17 +186,19 @@ def get_resp(
timeout,
):

req = urllib2.Request(self.url, req_str)
if PY3:
req_str = req_str.encode('utf8')

req = Request(self.url, req_str)
req.add_header('Authorization', 'Basic %s' % self.base64_str)
req.add_header('Cookie', '%s' % cookie)
try:
with contextlib.closing(urllib2.urlopen(req,
timeout=timeout)) as resp:
with contextlib.closing(urlopen(req, timeout=timeout)) as resp:
resp_str = resp.read()
resp_headers = resp.info()
return (resp_headers, resp_str)
except socket.timeout, e:
print 'Req timeout'
except socket.timeout as e:
print('Req timeout')
raise


Expand Down
9 changes: 2 additions & 7 deletions pycsco/nxos/utils/aaa.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
try:
import xmltodict
import re
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict
import re

__all__ = ['get_aaa_server_info', 'config_aaa_server',
'default_aaa_server', 'get_aaa_host_info',
Expand Down
9 changes: 2 additions & 7 deletions pycsco/nxos/utils/fhrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@
Ansible modules and in addition, general development.

"""
try:
import xmltodict
from pycsco.nxos.device import Device
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict
from pycsco.nxos.device import Device

__all__ = []

Expand Down
11 changes: 3 additions & 8 deletions pycsco/nxos/utils/mcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pycsco.nxos.utils import legacy
from pycsco.nxos.error import CLIError
import xmltodict

try:
import xmltodict
except ImportError as e:
print '*' * 30
print e
print '*' * 30
from pycsco.nxos.error import CLIError
from pycsco.nxos.utils import legacy

__all__ = ['get_igmp_defaults', 'get_igmp_global', 'get_igmp_snooping',
'get_igmp_snooping_defaults', 'get_igmp_interface',
Expand Down
9 changes: 2 additions & 7 deletions pycsco/nxos/utils/ntp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
try:
import xmltodict
from pycsco.nxos.utils import legacy
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict
from pycsco.nxos.utils import legacy

__all__ = ['get_ntp_existing', 'config_ntp', 'disable_ntp_server_peer',
'get_ntp_auth_info', 'set_ntp_auth_key', 'remove_ntp_auth_key',
Expand Down
11 changes: 3 additions & 8 deletions pycsco/nxos/utils/nxapi_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@
Ansible modules and in addition, general development.

"""
try:
import xmltodict
import re
from pycsco.nxos.error import CLIError
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict
import re
from pycsco.nxos.error import CLIError

__all__ = ['cmd_list_to_string', 'create_dir', 'feature_enabled',
'get_active_vpc_peer_link', 'get_interface_running_config',
Expand Down
7 changes: 1 addition & 6 deletions pycsco/nxos/utils/routing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from pycsco.nxos.error import InputError
from pycsco.lib import ipaddr

try:
import xmltodict
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict


__all__ = ['normalize_prefix', 'get_static_routes']
Expand Down
13 changes: 4 additions & 9 deletions pycsco/nxos/utils/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

try:
import xmltodict
import collections
from pycsco.nxos.error import CLIError
from pycsco.nxos.utils import legacy
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict
import collections
from pycsco.nxos.error import CLIError
from pycsco.nxos.utils import legacy

__all__ = []

Expand Down
7 changes: 1 addition & 6 deletions pycsco/nxos/utils/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@

import json

try:
import xmltodict
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict

__all__ = []

Expand Down
7 changes: 1 addition & 6 deletions pycsco/nxos/utils/vtp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
try:
import xmltodict
except ImportError as e:
print '*' * 30
print e
print '*' * 30
import xmltodict

__all__ = ['get_vtp_current_cfg', 'get_vtp_password']

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'xmltodict>=0.9.2',
'gtextfsm==0.2.1',
'scp',
'paramiko'
'paramiko',
'pyyaml==3.12'
],
)