so I have text file file.txt e.g
daemon
nserver 1111
nserver 11111
nscache 65536
timeouts 1 5 30 60 180 1800 15 60
log /var/log/3proxy/log D
logformat "- _L%t.%. %N.%p %E %U %C:%c %R:%r %O %I %h %T"
logformat "L%t.%. %N.%p %E %U %C:%c %R:%r %Q:%q %O %I %h %T %n "
rotate 360
#external 0.0.0.0
#internal 0.0.0.0
auth strong
########################################################################## Ttest-for-dev ##########################################################################
users ttest-for-dev:CL:d4ec7bf6
allow login,ttest-for-dev
proxy -n -a -p8989 -i194.150.75.50 -e194.150.75.50
socks -n -a -p43434 -i194.150.75.50 -e194.150.75.50
flush
########################################################################## specuser ##########################################################################
users specuser:CL:d4ec7bf6
allow login,specuser
proxy -n -a -p8989 -i194.150.75.50 -e194.150.75.50
socks -n -a -p43434 -i194.150.75.50 -e194.150.75.50
flush
deny *
maxconn 254
setgid 65534
setuid 65534
now how can i remove the lines so that the following is left
So the output file will be
daemon
nserver 1111
nserver 11111
nscache 65536
timeouts 1 5 30 60 180 1800 15 60
log /var/log/3proxy/log D
logformat "- _L%t.%. %N.%p %E %U %C:%c %R:%r %O %I %h %T"
logformat "L%t.%. %N.%p %E %U %C:%c %R:%r %Q:%q %O %I %h %T %n "
rotate 360
#external 0.0.0.0
#internal 0.0.0.0
auth strong
########################################################################## Ttest-for-dev ##########################################################################
users ttest-for-dev:CL:d4ec7bf6
allow login,ttest-for-dev
proxy -n -a -p8989 -i194.150.75.50 -e194.150.75.50
socks -n -a -p43434 -i194.150.75.50 -e194.150.75.50
flush
deny *
maxconn 254
setgid 65534
setuid 65534
Thank you
I have a script that adds lines, but I have no idea how to remake it to fit the conditions in the question
import sys
import fileinput
nameuser = "user1"
passuser = "pass1"
file_name = '/usr/local/etc/3proxy.cfg'
for line in fileinput.FileInput(file_name,inplace=1):
if 'auth strong' in line:
line = line.strip()
line = line.replace(line, line '\n########################################################################## ' nameuser ' ##########################################################################' '\nusers ' nameuser ':CL:' psuser '\nallow login,' nameuser '\nflush')
print (line, end='')
CodePudding user response:
Read the file line by line. When you get to the ###... specuser
line, start a loop that keeps reading until the flush
line. Otherwise, print the line.
import re
with fileinput.FileInput(file_name,inplace=1) as infile:
for line in infile:
if re.match('# specuser # $', line):
for nextline in infile:
if nextline.strip() == 'flush':
break
continue
print(line, end='')