分享一个使用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
直接可以使用收起