summaryrefslogtreecommitdiffstats
path: root/abs/core/supplemental-web/grabkey.py
blob: d07960753e3d33afc2412d5fdc270fdebc6a40e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python2
#polls the hobbit server for ghost clients.  If it finds ghosts it will make a new bb-hosts file
#with the new clients.
#Will not add duplicate clients
#return code of 0 means a new file was written, anything else is an error or no inserts occured
import urllib2
import string
import sys
import os

def readkeyfile():
    global keylist
    global data_config
    try:
        f = "%s/.ssh/authorized_keys" %data_config.MYTHHOME
        infile = open(f, 'r')
    except(IOError), e:
        keylist=''
    else:
        keylist = infile.readlines()
        infile.close()


def grabkey(server):
    global numberlines
    global mbekey
    global data_config
    url="http://" + server + ":1337/serverkey.cgi"

    try:
        f = urllib2.urlopen(url)
    except urllib2.HTTPError, e:
        if e.code != 200:
            print 'error find key'
            sys.exit(1)
    else:
        mbekey = f.readlines()
        f.close()
        numberlines = len(mbekey)
        #print mbekey


def makenewkeyfile():
    global keylist
    global mbekey
    global numberlines
    global numinserts
    global data_config
    numinserts=0
    outlist = []
    for item in keylist:
        if item not in outlist:
            outlist.append(item)

    for item in mbekey:
#		line = item.rstrip()
        #nline = "%s #" % (line)
        #nline = nline + '\n'
        if item not in outlist:
            outlist.append(item)
            #print "adding: " + item
            numinserts = numinserts + 1
    if numinserts > 0:
        f = "%s/.ssh/authorized_keys" %data_config.MYTHHOME
        try:
            outfile = open(f,"w")
        except(IOError), e:
            os.system('mkdir -p %s/.ssh' %data_config.MYTHHOME)
            outfile = open(f,"w")
        for i in outlist:
            outfile.write(i + '\n' )
            outfile.close
            os.system('chown -R mythtv %s/.ssh' %data_config.MYTHHOME )
            os.system('chmod 700 %s' %f)


global infile
global keylist
global numberlines
global numinserts
global mbekey
global data_config
sys.path.append('/usr/MythVantage/bin/')
config_file = "mv_config"
data_config = __import__(config_file,  globals(),  locals(),  [])



numinserts = 0
readkeyfile()

#print numberlines
#print mbekey

#data_config.MYTHHOME


try:
    infile = open('/etc/systemconfig', 'r')
except(IOError), e:
    sys.exit(1)
else:
        keylist = infile.readlines()
        infile.close()
for  item in keylist:
    if item.startswith("dbhost"):
        itemlist=item.split("=")
        server=itemlist[1].strip()
        server=server.rstrip()
        server=server.strip('"')
        server=server.rstrip('"')
grabkey(server)


if numberlines > 0:
    makenewkeyfile()
else:
    print "nothing to add"


if numinserts < 1:
    sys.exit(1)
else:
    sys.exit(0)