msyz999明仕亚洲官网文娱怎样运用fabric模块在远端完成文件字符串交换服从?

能手请辅导下,用fabric模块+fileinput完成远端文本字符串交换,仿佛不克不及用run函数,上代码

from fabric.api import run,local,env
import sys
import os,re,time,fileinput

path = r/etc/test.conf
old = yes
new = no
r = r^[^#].*enable-cache.*

if lensys.argv != 3:
    print lensys.argv
    print Usage:python change_conf_batch.py --host hn.txt
    sys.exit0
if sys.argv[1].startswith--host:
    hn = sys.argv[2]

def readsn:

    with openhn as f:
        while True:
            line=f.readline
            if not line:
                break
            desthost = line.strip.lstrip.rstrip
            env.host_string = desthost
            runcp %s %s %path,path+time.strftimer%Y%m%d%H%M%S, time.localtime
            runchange_confpath, old, new

def change_confpath,old,new:
    f = fileinput.inputpath,backup=.bak,inplace=True
    for line in f:
        line = line.rstrip
        match = re.matchr,line
        if match:
            print line.replaceold, new
        print line
    f.close

if __name__ == __main__:
    readsn

报错如下,在外地run是没题目的

run: cp /etc/test.conf /etc/test.conf20161116085212
Traceback most recent call last:
  File "change_conf_batch.py", line 45, in <module>
    readsn
  File "change_conf_batch.py", line 32, in readsn
    runchange_confpath, 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_commandscommand, 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函数的参数是字符串(要在远端运转的下令)。
下面这句是要实行change_conf函数的前往的下令,但由于change_conf没有return默许前往None,以是会出题目。

runchange_confpath, old, new

复杂点可以直接在run中实行sed下令来做交换,runsed -i “s/xxx/yyy/g” filename。

(看完/读完)这篇文章有何感想! msyz999明仕亚洲官网娱乐的分享…

发表评论

姓名 *
电子邮件 *
站点