python 第三方模块

python  第三方模块

paramiko 模块

一、paramiko 模块介绍

  参考链接:https://www.cnblogs.com/qianyuliang/p/6433250.html

  paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台,如Linux, Solaris, BSD, MacOS X, Windows等,paramiko都可以支持,因此,如果需要使用SSH从一个平台连接到另外一个平台,进行一系列的操作时,paramiko是最佳工具之一。

二、安装

1、安装paramiko有两个先决条件,python和另外一个名为PyCrypto的模块

2、PyCrypto安装

wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz

python setup.py build && python setup.py install


(编译时报错:error: command 'gcc' failed with exit status 1;这是因为缺少python-dev的软件包,所yum -y install python-devel)

3、paramiko安装

wget http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz

python setup.py build && python setup.py install


Crypto error:
'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC' (Crypto error: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'   找到 /usr/lib/python2.7/site-packages/Crypto/Util/number.py   把if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:   注释了   #if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:   )  

 win 模块

一、介绍

  参考链接:https://www.cnblogs.com/nulige/p/7802236.html

  win模块  为windows下API开发   可以监控windows下各个硬件信息级个服务级进程管理等

二、API接口说明

  接口链接地址:https://docs.microsoft.com/zh-cn/windows/desktop/CIMWin32Prov/win32-provider

三、代码样例

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import wmi
import os
import sys
import platform
import time 
def sys_version(): 
    c = wmi.WMI ()
    #获取操作系统版本
    for sys in c.Win32_OperatingSystem():
        print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber
        print  sys.OSArchitecture.encode("UTF8")#系统是32位还是64位的
        print sys.NumberOfProcesses #当前系统运行的进程总数

def cpu_mem():
    c = wmi.WMI ()       
    #CPU类型和内存
    for processor in c.Win32_Processor():
        #print "Processor ID: %s" % processor.DeviceID
        print "Process Name: %s" % processor.Name.strip()
    for Memory in c.Win32_PhysicalMemory():
        print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576)

def cpu_use():
    #5s取一次CPU的使用率
    c = wmi.WMI()
    while True:
        for cpu in c.Win32_Processor():
             timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
             print '%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage)
             time.sleep(5)    

def disk():
    c = wmi.WMI ()   
    #获取硬盘分区
    for physical_disk in c.Win32_DiskDrive ():
        for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
            for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
                print physical_disk.Caption.encode("UTF8"), partition.Caption.encode("UTF8"), logical_disk.Caption

    #获取硬盘使用百分情况
    for disk in c.Win32_LogicalDisk (DriveType=3):
        print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))

def network():
    c = wmi.WMI ()    
    #获取MAC和IP地址
    for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
        print "MAC: %s" % interface.MACAddress
    for ip_address in interface.IPAddress:
        print "ip_add: %s" % ip_address
    print

    #获取自启动程序的位置
    for s in c.Win32_StartupCommand ():
        print "[%s] %s <%s>" % (s.Location.encode("UTF8"), s.Caption.encode("UTF8"), s.Command.encode("UTF8")) 
    
    #获取当前运行的进程
    for process in c.Win32_Process ():
        print process.ProcessId, process.Name

def main():
    sys_version()
    #cpu_mem()
    #disk()
    #network()
    #cpu_use()

if __name__ == '__main__':
    main()
    print platform.system()
    print platform.release()
    print platform.version()
    print platform.platform()
    print platform.machine() 

 pexpect 模块

一、介绍

  参考链接:http://blog.51cto.com/superleedo/2119076

  Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块,它可以用来和像 ssh、ftp、passwd、telnet 等命令行程序进行自动交互。

二、样例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pexpect
import sys
#通过spawn类启动和控制子应用程序
child = pexpect.spawn('ssh root@192.168.1.124')
#将pexpect的输入输出信息写到mylog.txt文件中
fout = file('mylog.txt','w')
child.logfile = fout
#将pexpect的输入输出信息输出到标准输出
#child.logfile = sys.stdout
#expect方法用来判断子程序产生的输出,判断是否匹配相应字符串
child.expect('password:')
#字符串匹配则使用sendline进行回应-----send:发送命令,不回车、sendline:发送命令,回车、sendcontrol:发送控制符,如:sendctrol('c')等价于‘ctrl+c'、sendeof:发送eof
child.sendline('123456')
child.expect('#')
child.sendline('ls -lh')
child.expect('#')
psutil
psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(CPU,内存,磁盘,网络等)信息,主要应用于系统监控,分析和限制系统资源及进程的管理,它实现了同等命令行工具提供的功能,如ps,top,lsof,netstat,ifconfig,who,df,kill,free,nice等.支持32位,和64位的Linux,Windows,OS X,FreeBSD等操作系统。


https://www.cnblogs.com/Missowalker/p/7921888.html

  

  

原文地址:https://www.cnblogs.com/jiejunwang/p/9010707.html