python 远程执行代码 paramiko

installtion:

    yum -y install python-devel

    pip install paramiko

    problem:

    1.1 error: command ‘gcc’ failed with exit status 1

        这是缺少python-devel软件包,安装即可

    1.2 导入paramiko时报错: 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:

        use:

    1.3 执行命令并将结果输出

        import paramiko

        ssh = paramiko.SSHClient()

        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        ssh.connect("某IP地址",22,"用户名", "口令")

        stdin, stdout, stderr = ssh.exec_command("你的命令")

        print stdout.readlines()

        ssh.close()

    1.4 下载远程文件

        import paramiko

        t = paramiko.Transport((“主机”,”端口”))

        t.connect(username = “用户名”, password = “口令”)

        sftp = paramiko.SFTPClient.from_transport(t)

        remotepath=’/var/log/system.log’

        localpath=’/tmp/system.log’

        sftp.get(remotepath, localpath)

        t.close()

    1.5 上传文件

        import paramiko

        t = paramiko.Transport((“主机”,”端口”))

        t.connect(username = “用户名”, password = “口令”)

        sftp = paramiko.SFTPClient.from_transport(t)

        remotepath=’/var/log/system.log’

        localpath=’/tmp/system.log’

        sftp.put(localpath,remotepath)

        t.close()



你可能感兴趣的:(python,paramiko)