Flasking Flasking - 1 year ago 115
Python Question

How to replace file string via fabric at remote server?

I want to use

to replace remote server file string. Code below.

def readsn():

with open(hn) as f:
while True:
if not line:
desthost = line.strip().lstrip().rstrip()
env.host_string = desthost
run('cp %s %s' %(path,path+time.strftime(r'%Y%m%d%H%M%S', time.localtime())))
run(change_conf(path, old, new))

def change_conf(path,old,new):
f = fileinput.input(path,backup='.bak',inplace=True)
for line in f:
line = line.rstrip()
match = re.match(r,line)
if match:
print line.replace(old, new)
print line

It returns

TypeError: cannot concatenate 'str' and 'NoneType' objects

Answer Source

run() expects string with command. It can run only programs/scripts which are on remote server - it can't run your function.

BTW: now Python first executes your function which returns None and then run() use this result as command to execute on server.

If you have Linux on remote server then you could use sed command. ie.

sed "s/old_text/new_text/g" old_file > new_file

Or you have to copy your script on remote server and then run it

You can also download file from server (get()), change it locally (using your function) and send back on server (put())

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download