a guest Apr 17th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- import fileinput, sys
- for line in fileinput.input(["test.txt"], inplace=True):
- line = line.replace("car", "truck")
- # sys.stdout is redirected to the file
- filename = "/etc/ipf.conf"
- text = open(filename).read()
- open(filename, "w").write(text.replace(LASTKNOWN, CURRENT))
- from __future__ import with_statement
- from contextlib import nested
- in_filename, outfilename = "/etc/ipf.conf", "/tmp/ipf.conf"
- with nested(open(in_filename), open(outfilename, "w")) as in_, out:
- for line in in_:
- out.write(line.replace(LASTKNOWN, CURRENT))
- os.rename(outfilename, in_filename)
- f = open(filename, "r")
- lines = f.readlines()
- # Assume that change_ip is a function that takes a string and returns a new one with the ip changed): example below
- ret_lines = [change_ip(lines) for line in lines]
- new_file = open(new_filename, "w")
- def change_ip(str):
- ''' Gets a string, returns a new string where the ip is changed '''
- # Add implementation, something like: return str.replace(old_ip, new_ip) or something similair.
- Simply a test file 18.104.22.168
- And two times 22.214.171.124 on this line 126.96.36.199
- Simply a test file 10.73.144.112
- And two times 10.73.144.112 on this line 10.73.144.112
- import socket
- import fileinput
- import subprocess
- import string
- import re
- CURRENT = socket.getaddrinfo(socket.gethostname(), None)
- LASTKNOWN = '188.8.131.52'
- if CURRENT == LASTKNOWN:
- print 'Nevermind.'
- cf = open("/tmp/iiiipf.conf", "r")
- lns = cf.readlines()
- # close it so that we can open for writing later
- # assumes LASTKNOWN and CURRENT are strings with dotted notation IP addresses
- lns = "".join(lns)
- lns = re.sub(LASTKNOWN, CURRENT, lns) # This replaces all occurences of LASTKNOWN with CURRENT
- cf = open("/tmp/iiiipf.conf", "w")
- import in_place
- with in_place.InPlace('data.txt') as file:
- for line in file:
- line = line.replace('test', 'testZ')
RAW Paste Data