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


Python Expect.telnet_login方法代码示例

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


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

示例1: VMware

# 需要导入模块: from expect import Expect [as 别名]
# 或者: from expect.Expect import telnet_login [as 别名]
class VMware(object):
    def __init__(self):
        self.connect = Expect()
        if self.connect.mode == 'ssh':
            self.connect.ssh_login()
        elif self.connect.mode == 'telnet':
            self.connect.telnet_login()
        self._data()

    def __del__(self):
        self.connect.basic_logout()

    def add_vswitch_portgroup(self, vswitch, portgroup):
        # use esxcli, don't need to enter correct path
        cli = '''esxcli network vswitch standard portgroup add --portgroup-name=%s --vswitch-name=%s''' % (portgroup, vswitch)
        self._exec(cli, head='ADD_PG')
        
    def del_vswitch_portgroup(self, vswitch, portgroup):
        cli = '''esxcli network vswitch standard portgroup remove --portgroup-name=%s --vswitch-name=%s''' % (portgroup, vswitch)
        self._exec(cli, head='DEL_PG')

    def del_vswitch_all_portgroup(self, vswitch):
        cli = 'esxcli network vswitch standard portgroup list'
        self._exec(cli, head='DEL_ALLPG')
        v_p = self.connect.child.before
        # use /n instead of ^, ^ means begin of the file or variable, not lines
        v_p_list = re.findall(r'\n(\w+) +%s +\d+' % vswitch, v_p)
        for p in v_p_list:
            self.del_vswitch_portgroup(vswitch, p)
    
    def bind_portgroup_vlan(self, portgroup, vlan):
        cli = '''esxcli network vswitch standard portgroup set -p %s --vlan-id %s''' % (portgroup, vlan)
        self._exec(cli, head='BIND_VLAN')

    def unbind_portgroup_vlan(self, portgroup, vlan):
        cli = '''esxcli network vswitch standard portgroup set --vlan-id %s -p %s''' % (vlan, portgroup)
        self._exec(cli, head='UNBIND_VLAN')

    def copy_vm(self, folder_path, src, dst):
        cli = 'cd %s' % folder_path
        self._exec(cli, head='COPY_VM')
        cli = 'rm -fr %s' % dst
        self._exec(cli, head='COPY_VM')
        cli = 'cp -fr %s %s' % (src, dst)
        self._exec(cli, timeout=1200, head='COPY_VM')
        vmx_s = dst + '/' + src + '.vmx'
        vmx_d = dst + '/' + dst + '.vmx'
        cli = 'cp -f %s %s' % (vmx_s, vmx_d)
        self._exec(cli, head='COPY_VM')
        #sub display name
        #Cannot use cat v | ... >v, we should use cat v.bak | .. > v instead, else , we can get a null v, include nothing 
        display_name_sub = '''displayName = "%s"''' % dst
        cli = '''cat %s | sed \
                 -e 's/displayName.*".*"/%s/' \
                 > %s''' % (vmx_s, display_name_sub, vmx_d)
        self._exec(cli, head='COPY_VM')
        cli = 'cat %s' % vmx_d
        self._exec(cli, head='COPY_VM')
        copy_attribute = self.connect.child.before
        is_c_a = re.search(r'answer.msg.uuid.altered', copy_attribute)
        if is_c_a:
            info('''[COPY_VM]The answer attribute has been already configured, do nothing''', self.connect.is_info)
        else:
            # add it
            info('''[COPY_VM]No answer attribute, add it''', self.connect.is_info)
            cli = '''echo 'answer.msg.uuid.altered = "I copied it"' >> %s''' % vmx_d
            self._exec(cli, head='COPY_VM')

    def sub_vm(self, vmx, sernum='', eth0net='', eth1net=''):
        vmx_s = vmx
        vmx_b = vmx + '.bak'
        if sernum:
            cli = 'cp -f %s %s' % (vmx_s, vmx_b)
            self._exec(cli, head='COPY_VM')
            ser_num_sub = '''telnet:\/\/:%s''' % sernum
            cli = '''cat %s | sed \
                     -e 's/telnet:..:[0-9]\{1,5\}/%s/' \
                     > %s''' % (vmx_b, ser_num_sub, vmx_s)
            self._exec(cli, head='SUB_VM')
        if eth0net:
            cli = 'cp -f %s %s' % (vmx_s, vmx_b)
            self._exec(cli, head='COPY_VM')
            eth0_net_sub = '''ethernet0.networkName = "%s"''' % eth0net
            cli = '''cat %s | sed \
                     -e 's/ethernet0.networkName = ".*"/%s/' \
                     > %s''' % (vmx_b, ser_num_sub, eth0_net_sub  , vmx_s)
            self._exec(cli, head='SUB_VM')
        if eth1net:
            cli = 'cp -f %s %s' % (vmx_s, vmx_b)
            self._exec(cli, head='COPY_VM')
            eth1_net_sub = '''ethernet1.networkName = "%s"''' % eth1net
            cli = '''cat %s | sed \
                     -e 's/ethernet1.networkName = ".*"/%s/' \
                     > %s''' % (vmx_b, eth1_net_sub , vmx_s)
            self._exec(cli, head='SUB_VM')


    def del_vm(self, folder_path, folder_name):
        cli = 'rm -fr %s' % (folder_path + '/' + folder_name).replace('//', '/')
        self._exec(cli, head='DEL_VM')
#.........这里部分代码省略.........
开发者ID:4v4t4r,项目名称:VMware,代码行数:103,代码来源:__init__.py


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