import paramiko

dict = {
    'host': '10.111.18.122',
    'user': 'root',
    'passwd': '123456',
    'port': '22',
    'cmd': """
        #获取CPU使用率
        cpu=`top -b -n5 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' '{split($1, vs, ","); v=vs[length(vs)]; sub(/\s+/, "", v);sub(/\s+/, "", v); printf "%d", 100-v;}'`
        #获取内存使用率
        mem=`free -m | awk -F '[ :]+' 'NR==2{printf "%d", ($3)/$2*100}'`
        #获取磁盘使用率
        disk=`df -h | grep '/dev/mapper/centos-root' |awk '{print $5}'`
        echo $cpu
        echo $mem
        echo $disk
    """
}

def ssh(parameter):
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=parameter['host'], 
                        port=parameter['port'], 
                        username=parameter['user'], 
                        password=parameter['passwd'])
    std_in, std_out, std_err = ssh_client.exec_command(parameter['cmd'])
    for line in std_out:
        print (line.strip("\n"))
    ssh_client.close()
    
if __name__ == "__main__":
    ssh(dict)