首页
学习
活动
专区
圈层
工具
发布

Windows下安装Python SSH

在Python中没有专用的SSH模块,这需要手动的安装模块才行。Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,需要安装模块的顺序是pycrypto-〉paramiko。

安装OpenSSH

OpenSSH下载网址:http://sourceforge.net/projects/sshwindows/,下载安装即可。

安装Pycrypto模块

Pycrypto模块下载地址:http://pypi.python.org/pypi/pycrypto/,下载安装时缺少vcvarsall.bat,提示需要VisualStudio,网上解决办法大部分是安装MingW32。

在网上找到已经编译好的Windows中使用的Pycrypto版本,下载网址为:

http://www.voidspace.org.uk/python/modules.shtml#pycrypto

         下载Python版本和操作系统对应的版本,直接安装即可。

         注:如果是Win32bit + Python 2.7,则下载pycrypto-2.6.win32-py2.7.exe。

安装Paramiko模块

http://pypi.python.org/pypi/paramiko网址中下载最新版本的paramiko模块,解压缩后,进入到解压缩的目录中执行python setup.py install进行安装。

使用示例

使用SSH登陆到远程主机执行命令。

代码语言:javascript
代码运行次数:0
复制
import paramiko
def ssh_cmd(ip,port, cmd, user, passwd):
    result = ""
    try:
        ssh = paramiko.SSHClient()
       ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip, port, user, passwd,timeout=3)
        stdin, stdout, stderr =ssh.exec_command(cmd)
        result = stdout.read()
        ssh.close()
    except:
        print("ssh_cmd err.")
    return result
下一篇
举报
领券