当前位置: 首页>>代码示例>>Python>>正文


Python FS.cat方法代码示例

本文整理汇总了Python中jnpr.junos.utils.fs.FS.cat方法的典型用法代码示例。如果您正苦于以下问题:Python FS.cat方法的具体用法?Python FS.cat怎么用?Python FS.cat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jnpr.junos.utils.fs.FS的用法示例。


在下文中一共展示了FS.cat方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: facts_domain

# 需要导入模块: from jnpr.junos.utils.fs import FS [as 别名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 别名]
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']
开发者ID:GIC-de,项目名称:py-junos-eznc,代码行数:34,代码来源:domain.py

示例2: get_facts

# 需要导入模块: from jnpr.junos.utils.fs import FS [as 别名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 别名]
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, }
开发者ID:GIC-de,项目名称:py-junos-eznc,代码行数:45,代码来源:domain.py

示例3: facts_domain

# 需要导入模块: from jnpr.junos.utils.fs import FS [as 别名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 别名]
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']
开发者ID:RobWC,项目名称:py-junos-eznc,代码行数:20,代码来源:domain.py

示例4: TestFS

# 需要导入模块: from jnpr.junos.utils.fs import FS [as 别名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 别名]
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",
#.........这里部分代码省略.........
开发者ID:pklimai,项目名称:py-junos-eznc,代码行数:103,代码来源:test_fs.py

示例5: TestFS

# 需要导入模块: from jnpr.junos.utils.fs import FS [as 别名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 别名]
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',
#.........这里部分代码省略.........
开发者ID:ydnath,项目名称:py-junos-eznc,代码行数:103,代码来源:test_fs.py


注:本文中的jnpr.junos.utils.fs.FS.cat方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。