from fabric import Connection

# assume you log into remote os with ssh, and can run "sudo su - oracle" without asking password
username = "your_acct"
password = "your_pswd"

cmd_str_1 = """ printf "%-30s" """
cmd_str_2 = """
mem_tot=`grep MemTotal /proc/meminfo | awk '{print $2}'`; mem_tot=$(( mem_tot / 1024 ))
page_tab=`grep PageTables /proc/meminfo | awk '{print $2}'`; page_tab=$(( page_tab / 1024 ))
asmm_shr=`echo "ipcs -m | grep oracle" | sudo su - oracle | awk '{print $5}' | awk '{ sum+=$1 } END { print sum }'`; asmm_shr=$(( asmm_shr / 1024 / 1024 )) 
amm_shr=`df -Pk /dev/shm | sed -n '2p' | awk '{print $3}'`; amm_shr=$(( amm_shr / 1024 ))
shr_tot=$(( asmm_shr + amm_shr ))
printf "%15d%15d%15d%15d%15d\n" $mem_tot $page_tab $asmm_shr $amm_shr $shr_tot
"""
def get_mem_info(p_host,p_cmd):
    try:
        c = Connection(host=username + '@' + p_host, connect_kwargs={"password": password})
        c.run(p_cmd, pty=True)
    except:
        print('...oops, something is wrong for ' + p_host)

db_inv = open("inv\os_inv.txt")
lines = db_inv.readlines()
print(f'{"HOSTNAME":<30}{"MEM_TOTAL":>15}{"PAGE_TAB":>15}{"ASMM_SHR":>15}{"AMM_SHR":>15}{"SHR_TOT":>15}')
for line in lines:
    if line[0:1] != '#':
        (v_host) = line.strip('\n')
        get_mem_info(v_host, cmd_str_1 + v_host + cmd_str_2)