I've used a similar script for the last couple of years but have abandoned it because I found a tool that will do all of this for you. If you go to antpool.com > tools. It's called APMinerTool. the current version is V1.0.7.
Open the program and click on the IP configuration tab. Enter the ip info you want to use, then hit the "listening" button. Next go to your first S9 and hold down the button on the front for 3-5 seconds then let go. It normally takes a few seconds for the program to respond. Then repeat for all new miners. Now you have all your miners ip addresses in the exact order you want. Be careful not to hold the button for too long as it will cause a reset. I think its something like 10 seconds.
If you want to set all your pools go to settings>ip editor and setup the ip block of the miners you want to setup. Then go back to the miner manager tab, enter your pool information, and hit start scan. once the scan finishes, hit config to change the pool settings. It also remembers your pool settings after you reopen the program so that's a nice feature.