Python 语言越来越火,在自动化运维的今天,Python 俨然已经成为一个运维工程师不可缺少的一个技能手段,从近两年的招聘就可以看出,Python的重要程度不言而喻。
今天我们就来聊聊,Python 在我们运维工作上在哪些方面可以帮到我们,你又用python解决了什么问题,现在开源产品中哪些python 产品可以应用到我们的企业当中来,你对使用python有哪些疑惑,欢迎探讨。
分享一个使用Python Fabric 的小例子:
#!/usr/bin/python
from fabric.api import *
from fabric.colors import *
env.shell=\"/bin/sh -c\"
env.roledefs={
\'was100\':[\'10.1.9.169\',\'10.1.9.170\'],
}
env.user=\'username\'
env.password=\'password\'
errinfo=\'err.info\'
get_info=open( errinfo, \'w\')
get_info.truncate()
get_info.close()
get_info=open( errinfo, \'a\')
def exec_file(shell_file):
shell=\"\"
try:
fd=open(shell_file,\"r\")
shell=fd.read()
except:
print \"can\'t open file\"
exit()
fs=run(shell)
if fs.strip()!=\'\':
get_info.write(env.host+\' the system error report : \\n \' + fs + \'\\n\' )
get_info.flush()
else:
pass
cmd_fd=\'script/exe_cmd.sh\'
@roles(\'was100\')
def deploy():
exec_file(cmd_fd)
[root@monitor script]# cat exe_cmd.sh
errpt -dH
直接可以使用
收起