I wrote this script in order to generate a hosts.txt file with the IP addresses of the laptops in my school.
Each laptop is assigned a number, and their IP addresses are assigned to them with the template "192.168.0.x". X being the computer's number + 10. All these computers are running Arch Linux with XFCE4, custom tailored to aid in education. Each laptop is setup with SSH and the Teacher, (me), has the ability to ssh into any machine and execute any command I deem necessary.
Up until recently, I would SSH into the machines individually and install programs one-by-one. Now I use a program called PSSH (Parallel SSH) to connect to certain machines simultaneously and perform my commands. I only find one issue with this arrangement, all the IP addresses are stored in a file called, you guessed it, hosts.txt.
Now, as you can imagine, this gets quite cumbersome when dealing with 70 different addresses, and I would rather not have to type all the addresses by hand. So I made this script to generate a hosts file based off the computer numbers I choose. Selecting all the machines is now as easy as typing 'all'.
addresses =  nums =  run = True def rem_dupl(seq): seen = set() seen_add = seen.add return [x for x in seq if not (x in seen or seen_add(x))] while run: x = str(input("Computer numbers: ")) if x == 'done': run = False elif x == 'all': nums = [x for x in range(1, 70)] run = False elif int(x) > 0 and int(x) < 70: nums.append(x) else: print "Out of range" else: nums_finished = rem_dupl(nums) for i in nums_finished: addresses.append("192.168.0." + str((int(i) + 10))) temp = open('hosts', 'w').close() file = open('hosts.txt', 'w') for item in addresses: print>>file, item file.close
Any thoughts, constructive criticism, apparent issues, or redundancies?