import dsnet

def rem_cfg():
    cmdlist = []
    for i in range(100, 200):
        cmdlist.append('no access-list {}'.format(i))
    return cmdlist


def create_srcport(total, ace):
    
    cmdlist = []
    acl = 100
    seq = 1
    port = 10001
    cnt  = 0
    while cnt < total:
        cmdlist.append('access-list {0} {1} deny tcp any eq {2} any'.format(acl, seq, port))
        seq += 1
        port += 1
        cnt += 1
        if seq == ace:
            cmdlist.append('access-list {0} {1} permit any any any'.format(acl, seq))
            cnt += 1
            acl += 1
            seq = 1
            port = 10001
        if cnt+1 == total:
            cmdlist.append('access-list {0} {1} permit any any any'.format(acl, seq))
            cnt += 1
            
    return cmdlist
    
        
def create_dstport(total, ace):
    
    cmdlist = []
    acl = 100
    seq = 1
    port = 10001
    cnt  = 0
    while cnt < total:
        cmdlist.append('access-list {0} {1} deny tcp any any eq {2}'.format(acl, seq, port))
        seq += 1
        port += 1
        cnt += 1
        if seq == ace:
            cmdlist.append('access-list {0} {1} permit any any any'.format(acl, seq))
            cnt += 1
            acl += 1
            seq = 1
            port = 10001
        if cnt+1 == total:
            cmdlist.append('access-list {0} {1} permit any any any'.format(acl, seq))
            cnt += 1
            
    return cmdlist

 
def bind_vlan(count):
    
    cmdlist = []
    intf = 1001
    acl = 100

    for i in range(count):
        cmdlist.append('interface vlan1.{}'.format(intf))
        cmdlist.append('ip access-group {} in'.format(acl))
        intf += 1
        acl += 1

    return cmdlist

 
def unbind_vlan(count):
    
    cmdlist = []
    intf = 1001
    acl = 100

    for i in range(count):
        cmdlist.append('interface vlan1.{}'.format(intf))
        cmdlist.append('no ip access-group {} in'.format(acl))
        intf += 1
        acl += 1

    return cmdlist


if __name__ == '__main__':

#    ## test
#    cmdlist = create_srcport(4000, 100)
#    index = 1
#    for x in cmdlist:
#        print('{0}: {1}'.format(index, x))
#        index += 1


    ip = '10.55.192.31'
    h = dsnet.telnet(ip)
    h.cmd('en')
    h.cmd('conf t')


    cmdlist = create_srcport(100, 100)
#    cmdlist = bind_vlan(100)
#    cmdlist = rem_cfg()

    ## apply
    for x in cmdlist:
        h.cmd(x)
    h.close() 





