Python 语言越来越火,在自动化运维的今天,Python 俨然已经成为一个运维工程师不可缺少的一个技能手段,从近两年的招聘就可以看出,Python的重要程度不言而喻。
今天我们就来聊聊,Python 在我们运维工作上在哪些方面可以帮到我们,你又用python解决了什么问题,现在开源产品中哪些python 产品可以应用到我们的企业当中来,你对使用python有哪些疑惑,欢迎探讨。
pyhton特性几乎可以使其胜任所有的运维任务,我们目前在研的云平台和分布式数据库,后台所有监控和管理任务几乎都以pyhton完成,语言只是一个工具而已,shell也可以完成很多事情,语言选择主要是考虑研发的简单易用和工作效率吧
收起分享一个使用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
直接可以使用
收起