def _init_rgos_remote(self):
        # init the RGOS remote conf
        # rgos switch
        h_allconfig = switch_driver.get_sshserver_hostinfo()
        for x in h_allconfig:
            ssh_host = x.ip_address
            ssh_port = int(x.port_id)
            retry_maxtimes = int(x.retry_times)
            reconnect_interval = int(x.reconnect_time)
            # get user info from db by host
            username = switch_driver.get_sshserver_username(ssh_host)
            passwd = switch_driver.get_sshserver_password(ssh_host)

            LOG.debug("Init remote ssh_host ip: %s" % ssh_host)
            LOG.debug("Init retry_maxtimes: %s" % retry_maxtimes)
            LOG.debug("Init reconnect_interval: %s" % reconnect_interval)
            LOG.debug("Init ssh_port: %s" % ssh_port)
            LOG.debug("Init remote_switch_user: %s" % username)
            LOG.debug("Init remote_switch_pass: %s" % passwd)
            hostinfo_t = (ssh_host, ssh_port, retry_maxtimes, reconnect_interval)
            t = sshclient.ssh_connect(username, passwd, ssh_host, ssh_port)
            if t == -1:
                LOG.debug("Ssh connect failed t == -1 ")
            else:
                chan = sshclient.ssh_channel(t)
                if chan == -1:
                    LOG.debug("Ssh channel create failed chan == -1 ")
                else:
                    # pass
                    switch_driver.scan_server_lldp(chan, hostinfo_t)
                sshclient.ssh_close(chan, t)
            LOG.debug("Ssh connect end ! ")
def update_server_lldp():
    ret = -1
    
    h_allconfig = get_sshserver_hostinfo()
    if h_allconfig == []:
        ret = -1
        return ret
    
    for x in h_allconfig:
        ssh_host = x.ip_address
        ssh_port = int(x.port_id)
        retry_maxtimes = int(x.retry_times)
        reconnect_interval = int(x.reconnect_time)
        # get user info from db by host
        username = get_sshserver_username(ssh_host)
        passwd = get_sshserver_password(ssh_host)

        LOG.debug("scan remote ssh_host ip: %s" % ssh_host)
        LOG.debug("scan retry_maxtimes: %s" % retry_maxtimes)
        LOG.debug("scan reconnect_interval: %s" % reconnect_interval)
        LOG.debug("scan ssh_port: %s" % ssh_port)
        LOG.debug("scan remote_switch_user: %s" % username)
        LOG.debug("scan remote_switch_pass: %s" % passwd)
        hostinfo_t = (ssh_host, ssh_port, retry_maxtimes, reconnect_interval)
        t = sshclient.ssh_connect(username, passwd, ssh_host, ssh_port)
        if t == -1:
            LOG.debug("Ssh connect failed t == -1 " )
        else:
            chan = sshclient.ssh_channel(t)
            if chan == -1:
                LOG.debug("Ssh channel create failed chan == -1 " )
            else:
                #pass
                scan_server_lldp(chan, hostinfo_t)
            sshclient.ssh_close(chan, t)
            LOG.debug("Ssh connect end ! " )
        
    ret = 0
    return ret
def unset_switch_vlan(ssh_host, ifx, vlan):
    # send cli to switch by ssh connet

    # paramater check is here

    # create cli command

    sshhost = ssh_host
    switch_ifx = ifx
    switch_vlan = str(vlan)

    # first step into vlan mode 
    cli_confmode = 'configure\r\n'
    cli_vlanmode = 'vlan ' + switch_vlan + '\r\n'

    cli_vlan_noaddifx = 'no add interface ' + switch_ifx + '\r\n'

    cli_exit = 'exit\r\n'
    cli_conf_exit = 'exit\r\n'
	
    # create ssh connect by host
    host_t = get_sshserver_hostinfo_byhost( ssh_host )
    sshport = int(host_t[1])
    user = get_sshserver_username(sshhost)
    password = get_sshserver_password(sshhost)
    
    t= sshclient.ssh_connect(user, password, sshhost, sshport)
    if t == -1:
        LOG.error("unset_switch_vlan ssh connect failed t == -1")
        return -1
    
    chan = sshclient.ssh_channel(t)
    if chan == -1:
        LOG.error("unset_switch_vlan ssh channel create failed chan == -1")
        return -1
    
    # send the cli to switch
    
    try:
        chan.settimeout(5.0)
        # get switch cli mode info via ssh
        switch_mode_info = ''
        switch_mode_info = switch_api.get_switchinfo_climode(chan, switch_mode_info)
        
        switch_api.send_switch_cli(chan, cli_confmode)
        switch_cli_return = ''
        switch_mode_info = switch_api.get_switchinfo_climode(chan, switch_cli_return)
        LOG.debug("set_switch_vlan cli_confmode return = %s ", switch_mode_info)
        
        # Check interface mode
        cli_show_ifx_mode = 'show interfaces ' + switch_ifx + ' switchport \r\n'
        switch_api.send_switch_cli(chan, cli_show_ifx_mode)
        # Get the interface mode info
        return_switchport_info = switch_api.get_switchinfo_cliexecut(chan,switch_mode_info)
        LOG.debug("set_switch_vlan cli_confmode return_switchport_info = \r\n%s \r\n", return_switchport_info)
        # Parse the interface mode info
        switch_port_mode = switch_api.get_switchport_mode(return_switchport_info, switch_ifx)
        LOG.debug("set_switch_vlan cli_confmode switch_port_mode = %s ", switch_port_mode)
        
        if switch_port_mode == 'ACCESS':
            # send cli command into vlan mode
            switch_api.send_switch_cli(chan, cli_vlanmode)
            # send cli command to switch via ssh
            switch_api.send_switch_cli(chan, cli_vlan_noaddifx)
            # send cli command exit vlan mode and config mode
            switch_api.send_switch_cli(chan, cli_exit)
            switch_api.send_switch_cli(chan, cli_conf_exit)
        elif switch_port_mode == 'TRUNK':
            #into interface mode
            cli_ifx_mode = 'interface ' + switch_ifx +  '\r\n'
            switch_api.send_switch_cli(chan, cli_ifx_mode)

            # remova vlan from vlan list
            cli_ifx_remove_vlan = 'switchport trunk allowed vlan rem '+ switch_vlan + '\r\n'
            switch_api.send_switch_cli(chan, cli_ifx_remove_vlan)
            # send cli command exit vlan mode and config mode
            switch_api.send_switch_cli(chan, cli_exit)
            switch_api.send_switch_cli(chan, cli_conf_exit)
        elif switch_port_mode == 'UPLINK':
            #into interface mode
            cli_ifx_mode = 'interface ' + switch_ifx +  '\r\n'
            switch_api.send_switch_cli(chan, cli_ifx_mode)

            # remova vlan from vlan list
            cli_ifx_remove_vlan = 'switchport trunk allowed vlan rem '+ switch_vlan + '\r\n'

            switch_api.send_switch_cli(chan, cli_ifx_remove_vlan)
            # send cli command exit vlan mode and config mode
            switch_api.send_switch_cli(chan, cli_exit)
            switch_api.send_switch_cli(chan, cli_conf_exit)
        else:
            LOG.error("unset_switch_vlan switch_port_mode is error!")
        LOG.debug("unset_switch_vlan send_switch_cli SUCCESS! ")

    finally:
        LOG.debug("unset_switch_vlan end !")

    # close the ssh conect with switch
    sshclient.ssh_close(chan, t)
    
    return 0
def set_switch_vlan(ssh_host, ifx, vlan):
    
    # paramater check is here
    
    # create cli command
    sshhost = ssh_host
    host_t = ''
    switch_port_mode = 'access'
    switch_ifx = ifx
    switch_vlan = str(vlan)
    vlanall =  '1-4094'
    vlanlist = vlanall
    access = 'ACCESS'
    trunk = 'TRUNK'
    uplink = 'UPLINK'
    portmode = ''
    return_switchport_info = ''
    
    # first step into vlan mode 
    cli_confmode = 'configure\r\n'
    cli_show_ifx_mode = 'show interfaces ' + switch_ifx + ' switchport\r\n'
    cli_vlanmode = 'vlan ' + switch_vlan + '\r\n'
    #print 'set_switch_vlan cli_vlanmode = %s \r\n' % cli_vlanmode,
    cli_ifx_mode = 'interface ' + switch_ifx +  '\r\n'
    # bug for eth1 native 1 will not get dhcp ip
    portmode = trunk
    cli_ifx_set_trunkmode = 'switchport mode '+ portmode +'\r\n'
    portmode = uplink
    cli_ifx_set_uplinkmode = 'switchport mode '+ portmode +'\r\n'
    cli_ifx_remove_vlan = 'switchport tru allow vlan r '+ vlanlist + '\r\n'
    vlanlist = switch_vlan
    cli_ifx_add_vlan = 'switchport tru allow vlan add ' + vlanlist + '\r\n'
    cli_exit = 'exit\r\n'
    cli_conf_exit = 'exit\r\n'
    
    # create ssh connect by host
    host_t = get_sshserver_hostinfo_byhost( ssh_host )
    sshport = int(host_t[1])
    user = get_sshserver_username(sshhost)
    password = get_sshserver_password(sshhost)
    
    t= sshclient.ssh_connect(user, password, sshhost, sshport)
    if t == -1:
        LOG.error("Set_switch_vlan ssh connect failed t == -1")
        return -1
    
    chan = sshclient.ssh_channel(t)
    if chan == -1:
        LOG.error("Set_switch_vlan ssh channel create failed chan == -1")
        return -1
    
    # send the cli to switch
    
    try:
        chan.settimeout(5.0)
        # get switch cli mode info via ssh
        switch_mode_info = ''
        switch_mode_info = switch_api.get_switchinfo_climode(chan, switch_mode_info)
        
        # send 'conf' cli command to switch via ssh
        switch_api.send_switch_cli(chan, cli_confmode)
        switch_cli_return = ''
        switch_mode_info = switch_api.get_switchinfo_climode(chan, switch_cli_return)
        LOG.debug("set_switch_vlan cli_confmode return = %s \r\n" ,switch_mode_info)
        # send 'vlan xx ' cli command to switch via ssh Create or Set vlan
        switch_api.send_switch_cli(chan, cli_vlanmode)
        # send cli command exit vlan mode
        switch_api.send_switch_cli(chan, cli_exit)
        
        # Check interface mode
        switch_api.send_switch_cli(chan, cli_show_ifx_mode)
        # Get the interface mode info
        return_switchport_info = switch_api.get_switchinfo_cliexecut(chan,switch_mode_info)
        LOG.debug("set_switch_vlan cli_confmode return_switchport_info = \r\n%s \r\n" ,return_switchport_info)
        # Parse the interface mode info
        switch_port_mode = switch_api.get_switchport_mode(return_switchport_info, switch_ifx)
        LOG.debug("set_switch_vlan cli_confmode switch_port_mode = %s \r\n" ,switch_port_mode)
        #into interface mode
        switch_api.send_switch_cli(chan, cli_ifx_mode)
        #if the ifx is access mode should switch to trunnk then remova all vlan ,finally set vlan 
        if switch_port_mode == access:
            LOG.debug("set_switch_vlan switch_port_mode is access!")
            # change portmode into trunk
            #send_switch_cli(chan, cli_ifx_set_trunkmode)
            #print 'set_switch_vlan cli_ifx_set_trunkmode send! \r\n'
            # change portmode into uplink
            switch_api.send_switch_cli(chan, cli_ifx_set_uplinkmode)

            # remova all vlan list
            switch_api.send_switch_cli(chan, cli_ifx_remove_vlan)
            LOG.debug("set_switch_vlan cli_ifx_remove_vlan send!")

            # add vlan into allow vlan list
            switch_api.send_switch_cli(chan, cli_ifx_add_vlan)
            LOG.debug("set_switch_vlan cli_ifx_add_vlan send!")
        elif switch_port_mode == trunk:
            LOG.debug("set_switch_vlan switch_port_mode is trunk!")
            # add vlan into trunk allow vlan list
            switch_api.send_switch_cli(chan, cli_ifx_add_vlan)
        elif switch_port_mode == uplink:
            LOG.debug("set_switch_vlan switch_port_mode is uplink!")
            # add vlan into trunk allow vlan list
            switch_api.send_switch_cli(chan, cli_ifx_add_vlan)
        else:
            LOG.error("set_switch_vlan err : port mode error")

        # send cli command exit interface mode and config mode
        switch_api.send_switch_cli(chan, cli_exit)
        switch_api.send_switch_cli(chan, cli_conf_exit)

    finally:
        LOG.debug("set_switch_vlan end !")

    # close the ssh conect with switch
    sshclient.ssh_close(chan, t)

    return 0