Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
Here is sample code linux_fabric.py:
from fabric import Connection def main(): c = Connection("ubuntu@ubunu2004", connect_kwargs={"password": "ubuntu"}) c.put('stock_data.csv', '/tmp') #c.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz') with c.cd('/tmp'): c.run("git clone https://github.com/zhuby1973/python.git") if __name__ == '__main__': main()
C:\Users\zhuby>linux_fabric.py Cloning into 'python'... Updating files: 100% (1843/1843), done. verify from Linux VM: (base) ubuntu@ubunu2004:/tmp$ ls -ltr total 3140 -rw-rw-rw- 1 ubuntu ubuntu 17 Aug 31 08:46 stock_data.csv drwxrwxr-x 12 ubuntu ubuntu 4096 Aug 31 08:47 python (base) ubuntu@ubunu2004:/tmp$
# using SerialGroup for multiple VM operation from fabric import SerialGroup as Group pool = Group('web1', 'web2', 'web3', connect_kwargs={"password": "youpassword"} ) pool.put('myfiles.tgz', '/opt/mydata') pool.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz') or >>> from fabric import Connection >>> for host in ('web1', 'web2', 'mac1'): >>> result = Connection(host).run('uname -s') ... print("{}: {}".format(host, result.stdout.strip())) ... web1: Linux web2: Linux mac1: Darwin