script to assign hostname and ip address to machine


In [63]:
import os
import random
import json
import shutil

In [37]:
colordir = os.listdir('/home/wcmckee/colortest/')

In [42]:
alcolrz = []

In [43]:
for cold in colordir:
    #print cold
    if '.json' in cold:
        print cold
        alcolrz.append(cold)


sandwashed.json
fire-in-the-rain.json
lake-moss.json

In [47]:
alranza = random.choice(alcolrz)

In [48]:
alranza


Out[48]:
'fire-in-the-rain.json'

In [71]:
checkexist = os.listdir('/home/wcmckee/colortest/assignedhosts/')
assignedh = []

In [72]:
for chex in checkexist:
    print chex
    assignedh.append(chex)
    #os.remove('/home/wcmckee/colortest/' + chex)


a2b1bb.json
fire-in-the-rain.json
lake-moss.json

In [73]:
colordir


Out[73]:
['Sandwashed.png',
 'Fire-in-the-Rain.png',
 'sandwashed.json',
 'Lake-Moss.png',
 'freeip.txt',
 'fire-in-the-rain.json',
 'lake-moss.json']

In [80]:
assignedh


Out[80]:
['a2b1bb.json', 'fire-in-the-rain.json', 'lake-moss.json']

In [83]:
toremvz = set(colordir) & set(assignedh)

In [88]:
tolidt = list(toremvz)

In [91]:
for tolid in tolidt:
    print tolid
    os.remove('/home/wcmckee/colortest/' + tolid)


fire-in-the-rain.json
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-91-2cf3bcc621b4> in <module>()
      1 for tolid in tolidt:
      2     print tolid
----> 3     os.remove('/home/wcmckee/colortest/' + tolid)

OSError: [Errno 2] No such file or directory: '/home/wcmckee/colortest/fire-in-the-rain.json'

In [ ]:


In [ ]:


In [89]:
tolidt


Out[89]:
['fire-in-the-rain.json', 'lake-moss.json']

In [50]:
opjsflz = open('/home/wcmckee/colortest/' + alranza, 'r')

In [51]:
nerza = opjsflz.read()

In [53]:
nerza


Out[53]:
'{"midsens-num": 2, "description": null, "intense-num": 1, "title": "Fire in the Rain", "ftitle": "fire-in-the-rain", "locurl": "/home/wcmckee/colortest/Fire-in-the-Rain.png", "hex": "#9f3700", "absens-num": 3, "generalfeel": "absense"}'

In [55]:
neadict = json.loads(nerza)

In [8]:
opipdy = open('/home/wcmckee/colortest/freeip.txt', 'r')
iprdy = opipdy.read()

In [12]:
ipclean = iprdy.replace('\n', ', ')

In [13]:
ipclean


Out[13]:
'192.168.1.130, 192.168.1.131, 192.168.1.132, 192.168.1.133, 192.168.1.134, 192.168.1.135, 192.168.1.136, 192.168.1.137, 192.168.1.138, 192.168.1.139, 192.168.1.140, 192.168.1.141, 192.168.1.142, 192.168.1.143, 192.168.1.144, 192.168.1.145, 192.168.1.146, 192.168.1.147, 192.168.1.148, 192.168.1.149, 192.168.1.150, 192.168.1.151, 192.168.1.152, 192.168.1.153, 192.168.1.154, 192.168.1.155, 192.168.1.156, 192.168.1.157, 192.168.1.158, 192.168.1.159, 192.168.1.160, 192.168.1.161, 192.168.1.162, 192.168.1.163, 192.168.1.164, 192.168.1.165, 192.168.1.166, 192.168.1.167, 192.168.1.168, 192.168.1.169, 192.168.1.170, 192.168.1.171, 192.168.1.172, 192.168.1.173, 192.168.1.174, 192.168.1.175, 192.168.1.176, 192.168.1.177, 192.168.1.178, 192.168.1.179, 192.168.1.130, 192.168.1.131, 192.168.1.132, 192.168.1.133, 192.168.1.134, 192.168.1.135, 192.168.1.136, 192.168.1.137, 192.168.1.138, 192.168.1.139, 192.168.1.140, 192.168.1.141, 192.168.1.142, 192.168.1.143, 192.168.1.144, 192.168.1.145, 192.168.1.146, 192.168.1.147, 192.168.1.148, 192.168.1.149, 192.168.1.150, 192.168.1.151, 192.168.1.152, 192.168.1.153, 192.168.1.154, 192.168.1.155, 192.168.1.156, 192.168.1.157, 192.168.1.158, 192.168.1.159, 192.168.1.160, 192.168.1.161, 192.168.1.162, 192.168.1.163, 192.168.1.164, 192.168.1.165, 192.168.1.166, 192.168.1.167, 192.168.1.168, 192.168.1.169, 192.168.1.170, 192.168.1.171, 192.168.1.172, 192.168.1.173, 192.168.1.174, 192.168.1.175, 192.168.1.176, 192.168.1.177, 192.168.1.178, 192.168.1.179, '

In [15]:
ipliszq = ipclean.split(',')

In [23]:
#New hostname is zero on the list. Need to remove this now

newhos = ipliszq[0]

In [24]:
newhos


Out[24]:
'192.168.1.130'

In [57]:
neadict.update({'ipaddress' : newhos})

In [58]:
neadict


Out[58]:
{u'absens-num': 3,
 u'description': None,
 u'ftitle': u'fire-in-the-rain',
 u'generalfeel': u'absense',
 u'hex': u'#9f3700',
 u'intense-num': 1,
 'ipaddress': '192.168.1.130',
 u'locurl': u'/home/wcmckee/colortest/Fire-in-the-Rain.png',
 u'midsens-num': 2,
 u'title': u'Fire in the Rain'}

In [60]:
convjsn = json.dumps(neadict)

In [62]:
convjsn
convipdy = open('/home/wcmckee/colortest/' + alranza, 'w')
convipdy.write(convjsn)
convipdy.close()

In [64]:
shutil.move('/home/wcmckee/colortest/' + alranza, '/home/wcmckee/colortest/assignedhosts/')

In [ ]:
#shutil.move('/home/wcmckee/colortest/' + alranza, '/home/wcmckee/colortest/assignedhosts/')

In [29]:
newlisav = ipliszq[1:]

In [35]:
newlisav

newfre = open('/home/wcmckee/colortest/freeip.txt', 'w')

for lisaw in newlisav:
    #print lispo
    newfre.write(str(lisaw))
    newfre.write('\n')

newfre.close()

In [19]:
for ipzq in ipliszq:
    print ipzq


192.168.1.130
 192.168.1.131
 192.168.1.132
 192.168.1.133
 192.168.1.134
 192.168.1.135
 192.168.1.136
 192.168.1.137
 192.168.1.138
 192.168.1.139
 192.168.1.140
 192.168.1.141
 192.168.1.142
 192.168.1.143
 192.168.1.144
 192.168.1.145
 192.168.1.146
 192.168.1.147
 192.168.1.148
 192.168.1.149
 192.168.1.150
 192.168.1.151
 192.168.1.152
 192.168.1.153
 192.168.1.154
 192.168.1.155
 192.168.1.156
 192.168.1.157
 192.168.1.158
 192.168.1.159
 192.168.1.160
 192.168.1.161
 192.168.1.162
 192.168.1.163
 192.168.1.164
 192.168.1.165
 192.168.1.166
 192.168.1.167
 192.168.1.168
 192.168.1.169
 192.168.1.170
 192.168.1.171
 192.168.1.172
 192.168.1.173
 192.168.1.174
 192.168.1.175
 192.168.1.176
 192.168.1.177
 192.168.1.178
 192.168.1.179
 192.168.1.130
 192.168.1.131
 192.168.1.132
 192.168.1.133
 192.168.1.134
 192.168.1.135
 192.168.1.136
 192.168.1.137
 192.168.1.138
 192.168.1.139
 192.168.1.140
 192.168.1.141
 192.168.1.142
 192.168.1.143
 192.168.1.144
 192.168.1.145
 192.168.1.146
 192.168.1.147
 192.168.1.148
 192.168.1.149
 192.168.1.150
 192.168.1.151
 192.168.1.152
 192.168.1.153
 192.168.1.154
 192.168.1.155
 192.168.1.156
 192.168.1.157
 192.168.1.158
 192.168.1.159
 192.168.1.160
 192.168.1.161
 192.168.1.162
 192.168.1.163
 192.168.1.164
 192.168.1.165
 192.168.1.166
 192.168.1.167
 192.168.1.168
 192.168.1.169
 192.168.1.170
 192.168.1.171
 192.168.1.172
 192.168.1.173
 192.168.1.174
 192.168.1.175
 192.168.1.176
 192.168.1.177
 192.168.1.178
 192.168.1.179
 

In [ ]: