简体   繁体   English

Python-使用定义和env.hosts创建结构日志文件

[英]Python - fabric log file creation using definitions and env.hosts

Been trying to solve this but can't seem to make it work. 一直试图解决此问题,但似乎无法使其正常工作。 I want to create a log file that looks like $HOSTNAME-timestamp. 我想创建一个看起来像$ HOSTNAME-timestamp的日志文件。 For example I have this: 例如我有这个:

def test_servers():
    env.user = getpass.getuser()
    env.hosts = ['servernumber1', 'servernumber2']


def logname():
        timestamp = time.strftime("%b_%d_%Y_%H:%M:%S")
        'env.hosts' + timestamp

def audit():
        name = logname()
        sys.stdout = open('/home/path/to/audit/directory/%s' % name, 'w')
        run('hostname -i')
        print 'Checking the uptime of: ', env.host
        if run('uptime') < '0':
                print(red("it worked for less"))
        elif run('uptime') > '0':
                print(green("it worked for greater"))
        else:
                print "WTF?!"

When I run fabric on my fabfile.py to perform "audit" it works just fine but it's not creating the log file with the appended host name at the beginning of the file. 当我在fabfile.py上运行fabric以执行“审核”时,它可以正常工作,但它没有使用在文件开头附加主机名的日志文件来创建。 It does create a log file for each host defined in test_servers with the timestamp though. 确实会为带有时间戳的test_servers中定义的每个主机创建一个日志文件。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Looks like env.hosts cannot be defined in function, only env.host_string does. 看起来env.hosts不能在函数中定义,只有env.host_string可以。

So I guess maybe you got some error messages like: 所以我想也许您收到一些错误消息,例如:

No hosts found. Please specify (single) host string for connection:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM