Quantcast
Channel: Howtos & more …– LANbugs
Viewing all articles
Browse latest Browse all 144

Python: Snippet – Suchen und ersetzen in Dateien

$
0
0

Der Titel des Posts sagt eigentlich schon alles 😉

Python 3:

#!/usr/bin/env python3

import fileinput
import re

file = fileinput.FileInput("/etc/ssh/sshd_config", inplace=True, backup=".bak")

for line in file:
    line = re.sub(r".*Banner.*","Banner /etc/issue.net", line)
    print(line, end='')

file.close()

Python 2:

#!/usr/bin/env python

import fileinput
import re
import sys

file = fileinput.FileInput("/etc/ssh/sshd_config", inplace=True, backup=".bak")

for line in file:
    line = re.sub(r".*Banner.*","Banner /etc/issue.net", line)
    sys.stdout.write(line)

file.close()

 


Viewing all articles
Browse latest Browse all 144