#!/usr/bin/python
import argparse
from fabric import Connection, Config
# 您要远程操作的计算机,username@ip
HOSTS = ['root@192.168.0.1', 'root@192.168.0.2']
PASSWORDS = ['admin', 'admin']
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--command', type=str, help='execute shell command.')
args = parser.parse_args()
if not args.command:
args.command = 'uname -a'
print('➜ Execute shell command: ', args.command)
for host, password in zip(HOSTS, PASSWORDS):
print(f'➜ Host: {host}')
if password:
config = Config(overrides={'sudo':{'password':password}})
Connection(host, config=config).sudo(args.command, hide='stderr')
else:
Connection(host).run(args.command)