Python: Script to create Sub-Interfaces on Linux Hosts
import pexpect
import time
def
config_subintf(host,passw,intf,start_vlan,start_ip,no_intf):
ssh
=pexpect.spawn(host)
ssh.expect('.*password:', timeout=300)
print 'starting'
ssh.sendline(passw)
print
ssh.readline()
time.sleep(120)
ssh.expect('.*$',
timeout= 300)
print
ssh.readline()
ssh.sendline('sudo
su')
print
ssh.readline()
i =
ssh.expect(['.*:','.*#'], timeout= 300)
print i
if i == 0:
ssh.sendline(passw)
print ssh.readline()
ssh.expect('.*#', timeout= 300)
if i == 1:
pass
print ssh.before
print ssh.after
time.sleep(10)
ip =
start_ip.split('.')
ip = [ int(x) for
x in ip ]
for x in
range(no_intf):
ssh.sendline('vconfig add {} {}'.format(intf,start_vlan))
ssh.expect('.*#',timeout=300)
ssh.sendline('ifconfig {}.{} up'.format(intf,start_vlan))
ssh.expect('.*#',timeout=300)
ssh.sendline('ifconfig {}.{} {}.{}.{}.{}/24'.format(intf,start_vlan,ip[0],ip[1],ip[2],ip[3]))
ssh.expect('.*#',timeout=300)
start_vlan =
start_vlan + 1
if int(ip[2])
<= 255:
ip[2] =
ip[2] + 1
print ip[2]
elif ip[1]
<= 255:
ip[2] = 1
ip[1] = ip[1]+1
print ip[1]
ssh.sendline('ifconfig')
print
ssh.readline()
print ssh.before
print ssh.after
ssh.expect('.*$',timeout=300)
print
ssh.readline()
print ssh.before
print ssh.after
ssh.close()
if __name__ == "__main__":
import argparse
parser =
argparse.ArgumentParser(description='Python script to create subinterfaces on
linux hosts, plz make sure vlan module installed on host before run this')
parser.add_argument('host', action="store", type= str)
parser.add_argument('passw', action="store", type= str)
parser.add_argument('intf', action="store", type=str)
parser.add_argument('start_vlan', action = "store", type= int)
parser.add_argument('start_ip',
action="store", type= str)
parser.add_argument('no_intf', action="store", type= int)
input =
parser.parse_args()
host = input.host
passw =
input.passw
intf = input.intf
start_vlan =
input.start_vlan
start_ip =
input.start_ip
no_intf =
input.no_intf
config_subintf(host,passw,intf,start_vlan,start_ip,no_intf)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.