Skip to content

Instantly share code, notes, and snippets.

@chapter09
Last active August 29, 2015 14:06
Show Gist options
  • Save chapter09/0e7dab614588d54a1029 to your computer and use it in GitHub Desktop.
Save chapter09/0e7dab614588d54a1029 to your computer and use it in GitHub Desktop.
Change IP and Routing Table in parallel
#!/usr/bin/python
import os, sys
import subprocess
procs = []
with open(sys.argv[1], 'r') as f:
for line in f.readlines():
if line.find("host1") > -1:
ip = line.split(" ")[0]
host = line.split(" ")[1].strip()
host_id = int(host[4:])
if host_id > 100 and host_id < 113:
new_ip = "192.168.%d.%d"%(15, host_id-100)
route = "/sbin/route add -net 192.168.0.0/16 gw 192.168.15.254"
elif host_id > 112 and host_id < 125:
new_ip = "192.168.%d.%d"%(16, host_id-112)
route = "/sbin/route add -net 192.168.0.0/16 gw 192.168.16.254"
else:
new_ip = "192.168.%d.%d"%(17, host_id-124)
route = "/sbin/route add -net 192.168.0.0/16 gw 192.168.17.254"
print host, new_ip
cmd = "ssh -o StrictHostKeyChecking=no root@%s ifconfig eth1 %s;%s"%(host, new_ip, route)
proc = subprocess.Popen(cmd, shell=True)
procs.append(proc)
for proc in procs:
proc.wait()
print "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment