-
Notifications
You must be signed in to change notification settings - Fork 4
/
BDC.py
241 lines (132 loc) · 5.83 KB
/
BDC.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/python3.6
import os,time,subprocess,fileinput
with fileinput.FileInput('/etc/selinux/config', inplace=True,backup='.bak') as f1:
for line in f1:
print(line.replace('SELINUX=enforcing','SELINUX=disabled'),end='')
f1.close()
print('*'*40)
print('\nInfo Primary Domain Controller')
print('\n'+'*'*40)
ip_dc2 = input('Enter ip Backup DC : ')
print('\nExample Enter Netmask: 8 16 24')
netmask = input('Enter Netmask : ')
#host_n = input('\nEnter hostname Backup DC: ')
print('\nExample domain : domain.local\n')
domain = input('Enter domain : ')
print('\n'+'*'*40)
############ tach chuoi
a = domain.split('.')[0]
print('\nInfo Primary Domain Controller')
print('\n'+'*'*40)
ip_dc1 = input('\nEnter ip Primary DC : ')
host_pdc = input('Enter hostname Primary DC: ')
print('*'*40)
host_n = subprocess.check_output('cat /etc/hostname',shell=True,universal_newlines=True)
host = host_n.rstrip('\n')
with open('/etc/hosts','a+') as f:
f.write('\n' + ip_dc1 +' ' + host_pdc+'.' + domain + ' '+host_pdc)
f.write('\n' + ip_dc2 + ' ' + host + '.' + domain + ' ' + host)
f.close()
########### transfer file hosts
print('\nCopy file hosts to Primary DC\n')
time.sleep(3)
os.system('scp /etc/hosts root@'+host_pdc+'.'+domain+':/etc/')
gw = os.popen("ip route |grep default | awk '{print $3}'").read()
################################## config network interface
if os.path.exists('/sys/class/net/')== True:
inf = subprocess.check_output('ls /sys/class/net/',shell=True,universal_newlines=True)
print(str(inf))
b = inf.split()
c = (b[0])
with fileinput.FileInput('/etc/sysconfig/network-scripts/ifcfg-'+str(c), inplace=True) as f:
for line in f:
print(line.replace('BOOTPROTO="dhcp"','BOOTPROTO=static'),end='')
#print(line.replace('ONBOOT="no"', 'ONBOOT=yes'))
f.close()
with open('/etc/sysconfig/network-scripts/ifcfg-'+str(c), 'a+') as f1:
f1.write('\nIPADDR=' + ip_dc2)
f1.write('\nFREFIX=' + netmask)
f1.write('\nGATEWAY=' + gw)
f1.write('\nDNS1=127.0.0.1')
f1.write('\nDNS2=8.8.8.8')
f1.close()
else:
print("ERROR!!! Dont't have interface\n")
err = subprocess.check_output('ls /sys/class/net/',shell=True,universal_newlines=True)
print(str(err))
exit(0)
########### restart network
os.system('systemctl restart network')
print('\nPrepare install.......\n')
time.sleep(5)
os.system('yum –y install epel-release && yum –y update')
print('update done!!!!')
#### install packet basic
os.system('yum -y install perl gcc libacl-devel libblkid-devel gnutls-devel readline-devel python-devel gdb pkgconfig krb5-workstation zlib-devel setroubleshoot-server libaio-devel setroubleshoot-plugins policycoreutils-python libsemanage-python setools-libs-python setools-libs popt-devel libpcap-devel sqlite-devel libidn-devel libxml2-devel libacl-devel libsepol-devel libattr-devel keyutils-libs-devel cyrus-sasl-devel cups-devel bind-utils libxslt docbook-style-xsl openldap-devel pam-devel bzip2 wget')
print('\nInstall krb5')
time.sleep(3)
os.system('yum -y install authconfig krb5-workstation')
###### dowload samba4
os.system('wget https://download.samba.org/pub/samba/stable/samba-4.6.0.tar.gz')
#### extract
os.system('tar -zxvf samba-4.6.0.tar.gz')
### buil
print('\n Begin compiling')
time.sleep(3)
os.system('cd /root/samba-4.6.0 && ./configure --enable-debug --enable-selftest --with-ads --with-systemd --with-winbind')
############ make and install
os.system('cd samba-4.6.0 && make && make install ')
with open('/etc/resolv.conf','w') as f2:
f2.write('# Generated by NetworkManager')
f2.write('\nsearch '+ domain )
f2.write('\nnameserver ' + ip_dc1)
f2.close()
## remove file created when install samba
os.system('rm -rf /etc/krb5.conf')
os.system('rm -rf /etc/samba/smb.conf')
##domain = input('Enter domain name : ')
## copy file krb5.conf to etc
os.system('cd domain/ && cp krb5.conf /etc/')
with open('/etc/krb5.conf','a+') as f3:
f3.write('\n default_realm = '+domain.upper())
f3.close()
##### get the kerberos key from PDC
os.system('kinit administrator@'+domain.upper())
os.system('klist')
###add the server to the existing domain
os.system('cp /usr/local/samba/bin/samba-tool /usr/sbin/')
os.system('samba-tool domain join '+domain+' DC -U"'+a+'\\administrator" --dns-backend=SAMBA_INTERNAL')
### create samba service
os.system('cp domain/samba.service /etc/systemd/system/samba.service')
####################################################################
print('\n2: Switch Primary DC press Enter()')
time.sleep(30)
input('Enter to continue.....')
#################################################################
for line in fileinput.FileInput('/usr/local/samba/etc/smb.conf',inplace=1):
if "workgroup = "+a.upper() in line:
line=line.replace(line,line+"\tallow dns updates = nonsecure and secure\n")
print (line,end='')
os.system('systemctl enable samba && systemctl start samba')
##################################################################
print('\nCopy file krb5.conf to Primary DC\n')
time.sleep(3)
os.system('scp /etc/krb5.conf root@'+host_pdc+'.'+domain+':/etc/ ')
###################################################################
print('\nInstall done!!!!!')
time.sleep(2)
print('\n4: Switch Primary DC press Enter')
time.sleep(30)
input('Enter to continue.....\n')
###################################################################
os.system('samba-tool drs showrepl')
###################################################################
print('showrepl done!!!!')
time.sleep(3)
print('\n6: Switch Primary DC press Enter')
time.sleep(30)
input('Enter to continue.....')
###################################################################
print('Install and config done!!!! reboot after 5s')
time.sleep(5)
os.system('reboot now')