analitics

Pages

Friday, June 5, 2020

Python 3.8.3 : Using the fabric python module - part 001.

The tutorial for today is about fabric python module.
You can read about this python module on the official webpage.
The team comes with this intro:
Fabric is a high level Python (2.7, 3.4+) library designed to execute shell commands remotely over SSH, yielding useful Python objects in return
...
It builds on top of Invoke (subprocess command execution and command-line features) and Paramiko (SSH protocol implementation), extending their APIs to complement one another and provide additional functionality.
I used python version 3.8.3rc1 to install with the pip3 tool.
pip3 install fabric
Collecting fabric
...
Installing collected packages: invoke, fabric
Successfully installed fabric-2.5.0 invoke-1.4.1
from fabric import Connection
import getpass
host = "catafest@tty.sdf.org"
password = getpass.getpass('Password for SDF account:')
with Connection("catafest@tty.sdf.org", connect_kwargs={"password":password}) as con:
    print("I will run command: id -u -n")
    con.run("id -u -n")
    print("I will run command: df -h")
    con.run("df -h")
    print("I will run command for SDF help: help")
    con.run("help")
    out = con.run("free")
    print("output for dir(out)")
    print(dir(out))
    print("output for out.shell")
    print(out.shell)
    print("output for out.connection")
    print(out.connection)
    print("output for out.stdout")
    print(out.stdout)
    print("output for out.env")
    print(out.env)
This is a simple example tested on my SDF account.
About the SDF:
The Super Dimension Fortress is a networked community of free software
authors, teachers, librarians, students, researchers, hobbyists,
computer enthusiasts, the aural and visually impaired. It is operated
as a recognized non-profit 501(c)(7) and is supported by its members.

Our mission is to provide remotely accessible computing facilities for
the advancement of public education, cultural enrichment, scientific
research and recreation. Members can interact electronically with each
other regardless of their location using passive or interactive forums.
Further purposes include the recreational exchange of information
concerning the Liberal and Fine Arts.

Members have UNIX shell access to games, email, usenet, chat, bboard,
webspace, gopherspace, programming utilities, archivers, browsers, and
more. The SDF community is made up of caring, highly skilled people who
operate behind the scenes to maintain a non-commercial INTERNET.

For information about membership levels, click on 'join' above.
Let see my example:
python fabric_001.py
Password for SDF account:
I will run command: id -u -n
catafest
I will run command: df -h
Filesystem         Size       Used      Avail %Cap Mounted on
/dev/wd0a           35G       2.0G        31G   6% /
nol1:/sdf           23T       5.1T        17T  23% /sdf
ptyfs              1.0K       1.0K         0B 100% /dev/pts
I will run command for SDF help: help
tput: No terminal type specified and no TERM variable set in the environment.
SDF Help System - v8, 1993 - sdf!smj

 [a] Directory and File Utilities
 [b] Email Information
 [c] USENET Information
 [d] User and Process Information
 [e] Tutorials and Very Useful Commands
 [f] ARPA Services (internetworking)
 [g] Homepage, VHOST and MetaARPA Utilities

 [q] Quit SDF Help System

Your Choice? q
q
For more help, type 'faq'
For a list of basic UNIX commands, type 'unix'
For live help, type 'helpdesk'

             total       used       free    buffers
Mem:      16274580   15098664    1175916   13433820
Swap:      1049292          0    1049292
output for dir(out)
['__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', 
'__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', '__weakref__', 'command', 'connection', 'encoding', 'env', 'exited', 'failed', 
'hide', 'ok', 'pty', 'return_code', 'shell', 'stderr', 'stdout', 'tail']
output for out.shell
C:\Windows\system32\cmd.exe
output for out.connection
<Connection host=tty.sdf.org user=catafest>
output for out.stdout
             total       used       free    buffers
Mem:      16274580   15098664    1175916   13433820
Swap:      1049292          0    1049292

output for out.env
{}