forked from Hydrosys4/Master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysconfigfilemod.py
executable file
·141 lines (99 loc) · 3.79 KB
/
sysconfigfilemod.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
# -*- coding: utf-8 -*-
"""
file storage utility
"""
from __future__ import print_function
from builtins import str
from builtins import range
import logging
import os
import os.path
import filestoragemod
# to change the static IP address it is necessary to modify several system configuration files in the RPI system:
# 1) dhcpcd config file -> this is the DHCP client
# 2) dnsmasq -> here the dhcp server the range of IP addresses provided should be in the same subnet of the static IP
def modifydhcpcdconfigfile(oldIP, newIP):
# change the relevant section in the dhcpcd file
afterkeyword="START HYDROSYS4 SECTION"
beforekeyword="END HYDROSYS4 SECTION"
#conffilepath="/home/anona/env/prova/dhcpcd.conf" #fake path
conffilepath="/etc/dhcpcd.conf" #real path
keyword="static ip_address"
separator="="
isok=modifyfilestring(conffilepath, oldIP, newIP, afterkeyword, beforekeyword, keyword, separator)
return isok
def modifydnsmasqconfigfile(oldIP, newIP):
# change the relevant section in the dnsmasq file
oldstring=calculaterange(oldIP)
newstring=calculaterange(newIP)
if not(oldstring and newstring):
return False
# implement
afterkeyword="START HYDROSYS4 SECTION"
beforekeyword="END HYDROSYS4 SECTION"
#conffilepath="/home/anona/env/prova/dnsmasq.conf" #fake path
conffilepath="/etc/dnsmasq.conf" #real path
keyword="dhcp-range"
separator="="
isok=modifyfilestring(conffilepath, oldstring, newstring, afterkeyword, beforekeyword, keyword, separator)
return isok
def calculaterange(IPaddress):
# the IP range if made using the followinf formula:
b=1
c=9
IPlist=IPaddress.split(".")
if len(IPlist)==4:
IPSTART=IPlist[:]
IPEND=IPlist[:]
if int(IPlist[3])>244:
IPSTART[3]=str(int(IPlist[3])-c)
IPEND[3]=str(int(IPlist[3])-b)
else:
IPSTART[3]=str(int(IPlist[3])+b)
IPEND[3]=str(int(IPlist[3])+c)
IPSTARTstring=".".join(IPSTART)
IPENDstring=".".join(IPEND)
print(" result ", IPSTARTstring+","+IPENDstring)
return IPSTARTstring+","+IPENDstring
else:
return ""
def modifyfilestring(filename, oldstring, newstring, afterkeyword, beforekeyword, keyword, separator):
filedata=[]
filestoragemod.readfiledata_plaintext(filename,filedata)
#print "text File /n" , filedata
can_modify=False
for i in range(len(filedata)):
line=filedata[i]
if afterkeyword in line:
can_modify=True
if (beforekeyword!="") and (beforekeyword in line):
can_modify=False
if (keyword in line) and (can_modify):
print(" row found ------------ !!!!!!!!! " , line)
if oldstring in line:
# isolate and change the string
#print " oldstring ", oldstring, " new ", newstring
filedata[i]=line.replace(oldstring,newstring)
print(" new row ------------ !!!!!!!!! " , filedata[i])
filestoragemod.savefiledata_plaintext(filename,filedata)
return True
else:
print("String value not found ", oldstring)
return False
def hostapdsavechangerow(searchkey,newvalue):
BASICDATAFILENAME="/etc/hostapd/hostapd.conf"
newrow=searchkey+"="+newvalue
filestoragemod.savechangerow_plaintext(BASICDATAFILENAME,searchkey,newrow)
def hostapdsavechangerow_spec(data):
BASICDATAFILENAME="/etc/hostapd/hostapd.conf"
identifier="# HERE->"
datastring=filestoragemod.disct2text(data[0])
newrow=identifier+datastring
filestoragemod.savechangerow_plaintext(BASICDATAFILENAME,identifier,newrow)
#-- start DB utility--------////////////////////////////////////////////////////////////////////////////////////
if __name__ == '__main__':
oldstring="192.168.1.1"
newstring="192.168.10.10"
modifydhcpcdconfigfile(oldstring, newstring)
modifydnsmasqconfigfile("192.168.1.172", newstring)
#calculaterange("192.168.1.245")