|
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
|
#!/usr/bin/env python
# coding:utf8
import
tempfile
import
os
import
sys
import
re
file
=
tempfile.TemporaryFile()
oldFile
=
'sshd_config'
if
os.path.exists(oldFile):
openOldFile
=
open
(oldFile,
'r'
)
else
:
print
"file %s not found"
%
oldFile
sys.exit()
for
line
in
openOldFile:
line
=
re.sub(r
'^#(PubkeyAuthentication)'
,
'\g<1>'
, line, flags
=
re.M)
line
=
re.sub(r
'^#(AuthorizedKeysFile)'
,
'\g<1>'
, line, flags
=
re.M)
line
=
re.sub(r
'(^PasswordAuthentication) yes'
,
'\g<1> no'
, line, flags
=
re.M)
file
.write(line)
openOldFile.close()
file
.seek(
0
)
tmpFile
=
file
.read()
if
os.path.exists(oldFile):
os.remove(oldFile)
writeFile
=
open
(oldFile,
'w'
)
writeFile.write(tmpFile)
file
.close()
|
本文转自 Art_Hero 51CTO博客,原文链接:http://blog.51cto.com/curran/1541904,如需转载请自行联系原作者