python运维自动化是做什么_【核心议题】Python 在自动化运维工作方面能帮你做什么,有哪些经验值得分享?...

该博客分享了一个使用Python Fabric库进行远程批量操作的例子,具体是读取shell脚本并执行,在'was100'角色的服务器上运行'errpt-dH'命令,收集错误报告并保存到文件中。这展示了Fabric在自动化运维和部署中的应用。
摘要由CSDN通过智能技术生成

分享一个使用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

直接可以使用收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值