forked from pbernasconi/pi-lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editDNS.py
193 lines (149 loc) · 6.21 KB
/
editDNS.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File which uses shell commands to:
# edit IP, DNS, WIFI numbers
# reboot RPI
# install python libraries
# create the stating script
import subprocess
from time import *
import sqlite3
import os
########################################################################################################################
DOOR_NUMBER = 101
NETWORK = "Airport"
PASSOWRD = "passpass"
IP_ADDRESS = "192.168.0.53"
SUB_MASK = "255.255.255.0"
ROUTER = "192.168.0.12"
DNS= "212.40.0.10"
########################################################################################################################
current_dir = os.path.dirname(os.path.abspath(__file__))
DBpath = os.path.join(current_dir, 'RFID_Lock.sqlite')
con = sqlite3.connect(DBpath)
########################################################################################################################
def readSqlite():
with con:
global personID, personName, personPIN, currentDate, currentTime, tagRecognized, dateIsSunday, doorNumber
c = con.cursor()
c.execute("SELECT * FROM seguridad_puerta WHERE (SEQ=:x)", {"x":DOOR_NUMBER})
row = c.fetchone()
ipNumber = row[5]
dnsNumber = row[9]
subnetNumber = row[10]
routerNumber = row[11]
wifiNetwork = row[12]
wifiPassword = row[13]
webPassword = row[14]
########################################################################################################################
def changeDNS(DNS):
textDNS = "sudo echo 'nameserver %s' > '/etc/resolv.conf'" % (DNS)
r = subprocess.Popen(textDNS, stdout=subprocess.PIPE, shell=True)
for line in r.stdout.readlines():
print line
r.communicate()
########################################################################################################################
def changeIP():
textIP = ["auto lo",
"",
"iface lo inet loopback",
"iface etho inet dhcp",
"",
"allow-hotplug wlan0",
"iface wlan0 inet static",
"wpa-ssid %s" % NETWORK,
"wpa-psk %s" % PASSOWRD,
"address %s" % IP_ADDRESS,
"netmask %s" % SUB_MASK,
"network 192.168.0.0",
"gateway %s" % ROUTER,
"",
"iface default inet dhcp"]
joinedString = '\n'.join(textIP)
print joinedString
file = current_dir + "/interfaces"
textFile = open(file, "w")
textFile.write(joinedString)
textFile.close()
shellCall = "sudo mv %s/interfaces /etc/network" %(current_dir)
r = subprocess.Popen(shellCall, stdout=subprocess.PIPE, shell=True)
r.communicate()
if r == "okay":
subprocess.Popen("reboot", stdout=subprocess.PIPE, shell=True)
########################################################################################################################
def installLib():
shellCalls = ["sudo apt-get update",
"sudo apt-get install python-setuptools",
"sudo apt-get install git-core",
"sudo apt-get install python-dev",
"sudo apt-get install python-pip",
"sudo easy_install -U pyserial",
"sudo easy_install -U RPIO",
"sudo easy_install -U cherrypy",
"sudo easy_install psutil"]
testCall = ["sudo easy_install -U pyserial"]
for i in range(len(testCall)):
r = subprocess.Popen(testCall[i], stdout=subprocess.PIPE, shell=True)
print testCall[i]
for line in r.stdout.readlines():
print line.strip()
r.communicate()
########################################################################################################################
def installGitCode():
textGit = ["cd ~",
"git clone https://github.com/pbernasconi/RFID-raspberryPi.git",
"cd /RFID-raspberryPi"]
for i in range(len(textGit)):
r = subprocess.Popen(textGit[i], stdout=subprocess.PIPE, shell=True)
########################################################################################################################
def createStartScript():
textSS = ["### BEGIN INIT INFO",
"# Provides: startScript",
"# Required-Start: $remote_fs $syslog",
"# Required-Stop: $remote_fs $syslog",
"# Default-Start: 2 3 4 5",
"# Default-Stop: 0 1 6",
"# Short-Description: Simple script to start a program at boot",
"# Description: A simple script from www.stuffaboutcode.com which will",
"### END INIT INFO",
"",
"case '$1' in",
" start)",
" echo 'Starting Script'",
" python %s/readRFID" % current_dir,
" bash %s/modDNS.sh" % current_dir,
" python %s/systemStats.py" % current_dir,
" ;;",
" stop)",
" echo 'stopping Script'",
" killall python",
" ;;",
" *)",
" echo 'usage ...'",
" exit 1",
" ;;",
"esac",
"",
"exit 0"]
joinedString = '\n'.join(textSS)
fileName = "startScript.sh"
file = current_dir + "/" + fileName
textFile = open(file, "w")
textFile.write(joinedString)
textFile.close()
commands = ["sudo mv %s/%s /etc/init.d/"% (current_dir, fileName),
"sudo chmod 755 /etc/init.d/%s" % (fileName),
"sudo update-rc.d %s defaults" % (fileName)]
for i in range(len(commands)):
r = subprocess.Popen(commands[i], stdout=subprocess.PIPE, shell=True)
print commands[i]
########################################################################################################################
def rebootPi():
command = "sudo reboot"
subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
########################################################################################################################
if __name__ == '__main__':
#readSqlite()
#changeDNS(DNS)
#changeIP()
#installLib()
#installGitCode()
createStartScript()