本文整理汇总了Python中jnpr.junos.utils.fs.FS类的典型用法代码示例。如果您正苦于以下问题:Python FS类的具体用法?Python FS怎么用?Python FS使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FS类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: facts_domain
def facts_domain(junos, facts):
"""
The following facts are required:
facts['hostname']
The following facts are assigned:
facts['domain']
facts['fqdn']
"""
try:
domain_filter_xml = E('configuration', E('system', E('domain-name')))
domain = junos.rpc.get_config(filter_xml=domain_filter_xml,
options=INHERIT)
domain_name = domain.xpath('.//domain-name')
if len(domain_name) > 0:
facts['domain'] = domain_name[0].text
facts['fqdn'] = facts['hostname'] + '.' + facts['domain']
return
except RpcError:
pass
fs = FS(junos)
file_content = fs.cat('/etc/resolv.conf') or fs.cat('/var/etc/resolv.conf')
words = file_content.split() if file_content is not None else ''
if 'domain' not in words:
facts['domain'] = None
facts['fqdn'] = facts['hostname']
else:
idx = words.index('domain') + 1
facts['domain'] = words[idx]
facts['fqdn'] = facts['hostname'] + '.' + facts['domain']
示例2: inventory
def inventory(self):
"""
Returns dictionary of file listing information for current and rollback
Junos install packages. This information comes from the /packages
directory.
"""
from jnpr.junos.utils.fs import FS
fs = FS(self.dev)
pkgs = fs.ls('/packages')
return dict(current=pkgs['files'].get(
'junos'), rollback=pkgs['files'].get('junos.old'))
示例3: inventory
def inventory(self):
"""
Returns dictionary of file listing information for current and rollback
Junos install packages. This information comes from the /packages
directory.
.. warning:: Experimental method; may not work on all platforms. If
you find this not working, please report issue.
"""
from jnpr.junos.utils.fs import FS
fs = FS(self.dev)
pkgs = fs.ls('/packages')
return dict(current=pkgs['files'].get(
'junos'), rollback=pkgs['files'].get('junos.old'))
示例4: facts_domain
def facts_domain(junos, facts):
"""
The following facts are required:
facts['hostname']
The following facts are assigned:
facts['domain']
facts['fqdn']
"""
fs = FS(junos)
words = fs.cat('/etc/resolv.conf').split()
if 'domain' not in words:
facts['domain'] = None
facts['fqdn'] = facts['hostname']
else:
idx = words.index('domain') + 1
facts['domain'] = words[idx]
facts['fqdn'] = facts['hostname'] + '.' + facts['domain']
示例5: get_facts
def get_facts(device):
"""
Gathers domain facts from the configuration or /etc/resolv.conf.
"""
domain_config = """
<configuration>
<system>
<domain-name/>
</system>
</configuration>
"""
domain = None
fqdn = None
# Try to read the domain-name from the config.
# This might fail due to lack of permissions.
try:
rsp = device.rpc.get_config(filter_xml=etree.XML(domain_config),
options={'database': 'committed',
'inherit': 'inherit',
'commit-scripts': 'apply', })
domain = rsp.findtext('.//domain-name')
# Ignore if user can't view the configuration.
except PermissionError:
pass
# Try to read the domain from the resolv.conf file. This only requires
# view permissions.
if domain is None:
fs = FS(device)
file_content = (fs.cat('/etc/resolv.conf') or
fs.cat('/var/etc/resolv.conf'))
words = file_content.split() if file_content is not None else []
if 'domain' in words:
idx = words.index('domain') + 1
domain = words[idx]
# Set the fqdn
fqdn = device.facts['hostname']
if fqdn is not None and domain is not None:
fqdn = fqdn + '.' + domain
return {'domain': domain,
'fqdn': fqdn, }
示例6: __init__
def __init__(self, host, username, password, *args, **kwargs):
super(JunosDevice, self).__init__(host,
username,
password,
*args,
vendor='juniper',
device_type=JNPR_DEVICE_TYPE,
**kwargs)
self.native = JunosNativeDevice(*args, host=host, user=username, passwd=password, **kwargs)
self.open()
self.cu = JunosNativeConfig(self.native)
self.fs = JunosNativeFS(self.native)
self.sw = JunosNativdSW(self.native)
示例7: setUp
def setUp(self, mock_connect):
mock_connect.side_effect = self._mock_manager
self.dev = Device(host="1.1.1.1", user="rick", password="password123", gather_facts=False)
self.dev.open()
self.fs = FS(self.dev)
示例8: TestFS
class TestFS(unittest.TestCase):
@patch("ncclient.manager.connect")
def setUp(self, mock_connect):
mock_connect.side_effect = self._mock_manager
self.dev = Device(host="1.1.1.1", user="rick", password="password123", gather_facts=False)
self.dev.open()
self.fs = FS(self.dev)
def test_cat_wrong_path_return_none(self):
path = "test/report"
self.assertEqual(self.fs.cat(path), None)
def test_cat(self):
self.fs._dev.rpc.file_show = MagicMock(side_effect=self._mock_manager)
path = "test/cat.txt"
self.assertTrue("testing cat functionality" in self.fs.cat(path))
self.fs._dev.rpc.file_show.assert_called_with(filename="test/cat.txt")
def test_cwd(self):
self.fs._dev.rpc.set_cli_working_directory = MagicMock(side_effect=self._mock_manager)
folder = "change/directory"
self.assertEqual("change/directory", self.fs.cwd(folder))
self.fs._dev.rpc.set_cli_working_directory.assert_called_with(directory="change/directory")
@patch("jnpr.junos.Device.execute")
def test_pwd(self, mock_execute):
mock_execute.side_effect = MagicMock(side_effect=self._mock_manager)
self.fs.pwd()
self.assertEqual(self.fs.pwd(), "/cf/var/home/rick")
def test_checksum_return_none(self):
path = "test/report"
self.assertEqual(self.fs.checksum(path), None)
def test_checksum_unknown_calc(self):
path = "test/report"
self.assertRaises(ValueError, self.fs.checksum, path=path, calc="abc")
def test_checksum_return_rsp(self):
self.fs.dev.rpc.get_sha256_checksum_information = MagicMock(side_effect=self._mock_manager)
path = "test/checksum"
self.assertEqual(self.fs.checksum(path, "sha256"), "xxxx")
self.fs.dev.rpc.get_sha256_checksum_information.assert_called_with(path="test/checksum")
def test_stat_calling___decode_file(self):
path = "test/stat/decode_file"
self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
self.assertEqual(
self.fs.stat(path),
{
"owner": "pqr",
"path": "/var/abc.sh",
"permissions": 755,
"permissions_text": "-rwxr-xr-x",
"size": 2,
"ts_date": "Mar 13 06:54",
"ts_epoc": "1394693680",
"type": "file",
},
)
def test_stat_calling___decode_dir(self):
path = "test/stat/decode_dir"
self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
self.assertEqual(self.fs.stat(path), {"path": "/var", "type": "dir", "file_count": 1, "size": 2})
def test_stat_return_none(self):
path = "test/abc"
self.fs.dev.rpc.file_list = MagicMock()
self.fs.dev.rpc.file_list.find.return_value = "output"
self.assertEqual(self.fs.stat(path), None)
def test_ls_calling___decode_file(self):
path = "test/stat/decode_file"
self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
self.assertEqual(
self.fs.ls(path),
{
"owner": "pqr",
"path": "/var/abc.sh",
"permissions": 755,
"permissions_text": "-rwxr-xr-x",
"size": 2,
"ts_date": "Mar 13 06:54",
"ts_epoc": "1394693680",
"type": "file",
},
)
def test_ls_calling___decode_dir(self):
path = "test/stat/decode_dir"
self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
self.assertEqual(
self.fs.ls(path),
{
"files": {
"abc": {
"permissions_text": "drwxr-xr-x",
"ts_date": "Feb 17 15:30",
"ts_epoc": "1392651039",
#.........这里部分代码省略.........
示例9: setUp
def setUp(self, mock_connect):
mock_connect.side_effect = self._mock_manager
self.dev = Device(host='1.1.1.1', user='rick', password='password123',
gather_facts=False)
self.dev.open()
self.fs = FS(self.dev)
示例10: TestFS
class TestFS(unittest.TestCase):
@patch('ncclient.manager.connect')
def setUp(self, mock_connect):
mock_connect.side_effect = self._mock_manager
self.dev = Device(host='1.1.1.1', user='rick', password='password123',
gather_facts=False)
self.dev.open()
self.fs = FS(self.dev)
@patch('jnpr.junos.device.warnings')
def test_cat_wrong_path_return_none(self, mock_warnings):
path = 'test/report'
self.assertEqual(self.fs.cat(path), None)
def test_cat(self):
self.fs._dev.rpc.file_show = MagicMock(side_effect=self._mock_manager)
path = 'test/cat.txt'
self.assertTrue('testing cat functionality' in self.fs.cat(path))
self.fs._dev.rpc.file_show.assert_called_with(filename='test/cat.txt')
def test_cwd(self):
self.fs._dev.rpc.set_cli_working_directory = MagicMock(
side_effect=self._mock_manager)
folder = 'change/directory'
self.assertEqual('change/directory', self.fs.cwd(folder))
self.fs._dev.rpc.set_cli_working_directory.\
assert_called_with(directory='change/directory')
@patch('jnpr.junos.Device.execute')
def test_pwd(self, mock_execute):
mock_execute.side_effect = MagicMock(side_effect=self._mock_manager)
self.fs.pwd()
self.assertEqual(self.fs.pwd(), '/cf/var/home/rick')
@patch('jnpr.junos.device.warnings')
def test_checksum_return_none(self, mock_warnings):
path = 'test/report'
self.assertEqual(self.fs.checksum(path), None)
def test_checksum_unknown_calc(self):
path = 'test/report'
self.assertRaises(ValueError, self.fs.checksum, path=path, calc='abc')
def test_checksum_return_rsp(self):
self.fs.dev.rpc.get_sha256_checksum_information = \
MagicMock(side_effect=self._mock_manager)
path = 'test/checksum'
self.assertEqual(self.fs.checksum(path, 'sha256'), 'xxxx')
self.fs.dev.rpc.get_sha256_checksum_information.\
assert_called_with(path='test/checksum')
def test_stat_calling___decode_file(self):
path = 'test/stat/decode_file'
self.fs.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(self.fs.stat(path),
{'owner': 'pqr', 'path': '/var/abc.sh',
'permissions': 755,
'permissions_text': '-rwxr-xr-x', 'size': 2,
'ts_date': 'Mar 13 06:54',
'ts_epoc': '1394693680',
'type': 'file'})
def test_stat_calling___decode_dir(self):
path = 'test/stat/decode_dir'
self.fs.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(self.fs.stat(path),
{'path': '/var', 'type': 'dir', 'file_count': 1,
'size': 2})
def test_stat_return_none(self):
path = 'test/abc'
self.fs.dev.rpc.file_list = MagicMock()
self.fs.dev.rpc.file_list.find.return_value = 'output'
self.assertEqual(self.fs.stat(path), None)
def test_ls_calling___decode_file(self):
path = 'test/stat/decode_file'
self.fs.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(self.fs.ls(path),
{'owner': 'pqr', 'path': '/var/abc.sh',
'permissions': 755,
'permissions_text': '-rwxr-xr-x', 'size': 2,
'ts_date': 'Mar 13 06:54',
'ts_epoc': '1394693680',
'type': 'file'})
def test_ls_calling___decode_dir(self):
path = 'test/stat/decode_dir'
self.fs.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(self.fs.ls(path),
{'files':
{'abc': {'permissions_text': 'drwxr-xr-x',
'ts_date': 'Feb 17 15:30',
'ts_epoc': '1392651039',
'owner': 'root', 'path': 'abc',
#.........这里部分代码省略.........
示例11: Device
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.utils.fs import FS
from pprint import pprint
import sys
dev = Device('host', user='user', password='pwd')
dev.open()
fs = FS(dev)
pprint (fs.ls(path="/var/tmp/", brief=True))
print fs.pwd()
示例12: JunosDevice
class JunosDevice(BaseDevice):
def __init__(self, host, username, password, *args, **kwargs):
super(JunosDevice, self).__init__(host,
username,
password,
*args,
vendor='juniper',
device_type=JNPR_DEVICE_TYPE,
**kwargs)
self.native = JunosNativeDevice(*args, host=host, user=username, passwd=password, **kwargs)
self.connected = False
self.open()
self.cu = JunosNativeConfig(self.native)
self.fs = JunosNativeFS(self.native)
self.sw = JunosNativdSW(self.native)
def open(self):
if not self.connected:
self.native.open()
self.connected = True
def close(self):
if self.connected:
self.native.close()
self.connected = False
def show(self, command, raw_text=True):
if not raw_text:
raise ValueError('Juniper only supports raw text output. \
Append " | display xml" to your commands for a structured string.')
if not command.startswith('show'):
raise CommandError(command, 'Juniper "show" commands must begin with "show".')
return self.native.cli(command, warning=False)
def show_list(self, commands, raw_text=True):
responses = []
for command in commands:
responses.append(self.show(command, raw_text=raw_text))
return responses
def backup_running_config(self, filename):
with open(filename, 'w') as f:
f.write(self.running_config)
def config(self, command, format='set'):
try:
self.cu.load(command, format=format)
self.cu.commit()
except ConfigLoadError as e:
raise CommandError(command, e.message)
def config_list(self, commands, format='set'):
try:
for command in commands:
self.cu.load(command, format=format)
self.cu.commit()
except ConfigLoadError as e:
raise CommandListError(commands, command, e.message)
def _uptime_components(self, uptime_full_string):
match_days = re.search(r'(\d+) days?', uptime_full_string)
match_hours = re.search(r'(\d+) hours?', uptime_full_string)
match_minutes = re.search(r'(\d+) minutes?', uptime_full_string)
match_seconds = re.search(r'(\d+) seconds?', uptime_full_string)
days = int(match_days.group(1)) if match_days else 0
hours = int(match_hours.group(1)) if match_hours else 0
minutes = int(match_minutes.group(1)) if match_minutes else 0
seconds = int(match_seconds.group(1)) if match_seconds else 0
return days, hours, minutes, seconds
def _uptime_to_string(self, uptime_full_string):
days, hours, minutes, seconds = self._uptime_components(uptime_full_string)
return '%02d:%02d:%02d:%02d' % (days, hours, minutes, seconds)
def _uptime_to_seconds(self, uptime_full_string):
days, hours, minutes, seconds = self._uptime_components(uptime_full_string)
seconds += days * 24 * 60 * 60
seconds += hours * 60 * 60
seconds += minutes * 60
return seconds
def _get_interfaces(self):
eth_ifaces = EthPortTable(self.native)
eth_ifaces.get()
loop_ifaces = LoopbackTable(self.native)
#.........这里部分代码省略.........
示例13: Device
from jnpr.junos.utils.fs import FS
from jnpr.junos import Device
dev = Device(host='xxxx', user='demo', password='demo123')
dev.open()
fs = FS(dev)
pprint(fs.ls('/var/tmp'))
dev.close()