如何通过远程服务器上的结构替换文件字符串?
How to replace file string via fabric at remote server?
我想用fabric
来替换远程服务器文件字符串。下面的代码。
def readsn():
with open(hn) as f:
while True:
line=f.readline()
if not line:
break
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
f.close()
我收到以下错误:
Traceback (most recent call last):
File "change_conf_batch.py", line 45, in <module>
readsn()
File "change_conf_batch.py", line 32, in readsn
run(change_conf(path, old, new))
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/network.py", line 677, in host_prompting_wrapper
return func(*args, **kwargs)
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/operations.py", line 1088, in run
shell_escape=shell_escape, capture_buffer_size=capture_buffer_size,
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/operations.py", line 914, in _run_command
_prefix_env_vars(_prefix_commands(command, 'remote')),
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/operations.py", line 670, in _prefix_commands
return prefix + command
TypeError: cannot concatenate 'str' and 'NoneType' objects
run()
需要带有命令的字符串。它只能 运行 远程服务器上的 programs/scripts - 它不能 运行 你的功能。
顺便说一句:现在 Python 首先执行你的函数 returns None
然后 run()
使用这个结果作为命令在服务器上执行。
如果您在远程服务器上有 Linux,那么您可以使用 sed
命令。即
run('sed "s/old_text/new_text/g" old_file > new_file')
或者您必须在远程服务器上复制您的脚本,然后 运行 它
您也可以从服务器下载文件(get()), change it locally (using your function) and send back on server (put())
我想用fabric
来替换远程服务器文件字符串。下面的代码。
def readsn():
with open(hn) as f:
while True:
line=f.readline()
if not line:
break
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
f.close()
我收到以下错误:
Traceback (most recent call last):
File "change_conf_batch.py", line 45, in <module>
readsn()
File "change_conf_batch.py", line 32, in readsn
run(change_conf(path, old, new))
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/network.py", line 677, in host_prompting_wrapper
return func(*args, **kwargs)
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/operations.py", line 1088, in run
shell_escape=shell_escape, capture_buffer_size=capture_buffer_size,
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/operations.py", line 914, in _run_command
_prefix_env_vars(_prefix_commands(command, 'remote')),
File "/root/.pythonbrew/venvs/Python-2.7.10/flask/lib/python2.7/site-packages/fabric/operations.py", line 670, in _prefix_commands
return prefix + command
TypeError: cannot concatenate 'str' and 'NoneType' objects
run()
需要带有命令的字符串。它只能 运行 远程服务器上的 programs/scripts - 它不能 运行 你的功能。
顺便说一句:现在 Python 首先执行你的函数 returns None
然后 run()
使用这个结果作为命令在服务器上执行。
如果您在远程服务器上有 Linux,那么您可以使用 sed
命令。即
run('sed "s/old_text/new_text/g" old_file > new_file')
或者您必须在远程服务器上复制您的脚本,然后 运行 它
您也可以从服务器下载文件(get()), change it locally (using your function) and send back on server (put())