Windows系统下安装Python的SSH模块教程
Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是:pycrypto -> ecdsa -> paramiko
1、安装pyCrypto
安装这个比较麻烦,需要本地编译,要装vs或gcc还有一堆配置,还不一定能编译成功。(网上能搜到安装步骤)
建议直接下载已编译版: http://www.voidspace.org.uk/python/modules.shtml#pycrypto
直接下载并安装既可。
(注:crypto有java和C++版)
2、安装ecdsa
看很多博客没有提到这个库,但我执行paramiko时,提示找不到ecdsa模块。
下载:https://pypi.python.org/pypi/ecdsa/0.9 ,解压到一个目录,目录中有一个setup.py。
安装比较简单,windows下直接在刚才解压后的目录执行:python setup.py install
3.安装paramiko
与安装ecdsa类型,只是打开下载页面很慢。。。
下载: https://github.com/paramiko/paramiko#,
安装步骤同ecdsa
注:1、所有另外安装的第三方库,如果不特指定安装后库目录的话,将默认保存到 %PYTHON_HOME%\Lib\site-packages下。
2、python大小写敏感,对模块名也是。
3、Crypto可以提供常见的加解密算法,如:RSA、RC4、DSA、DES
测试代码:
代码如下:
#!/usr/bin/python
# -*- coding:utf-8 -*-
# cp@chenpeng.info
import paramiko
def MAIN():
host = “10.1.1.1″
port = 22
user = “root”
pswd = “111222333″
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, user, pswd)
stdin, stdout, stderr = ssh.exec_command(‘ifconfig')
print stdout.read()
ssh.close()
#
if __name__=='__main__':
try:
MAIN()
except Exception,e:
print e
输出如下:
目前主要用于批量执行多个服务器的ssh命令,批量维护比较方便。
下面是两种使用paramiko连接到linux服务器的代码
方式一:
代码如下:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("某IP地址",22,"用户名", "口令")
上面的第二行代码的作用是允许连接不在know_hosts文件中的主机
方式二:
代码如下:
t = paramiko.Transport(("主机","端口"))
t.connect(username = "用户名", password = "口令")
如果连接远程主机需要提供密钥,上面第二行代码可改成:
代码如下:
t.connect(username = "用户名", password = "口令", hostkey="密钥")
例子:
代码如下:
#!/usr/bin/python
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()
下载linux服务器上的文件
代码如下:
#!/usr/bin/python
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()
上传文件到linux服务器
代码如下:
#!/usr/bin/python
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()

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Windows 11 is capable of running a large number of file types with or without external third-party applications. Not only does it allow you to perform numerous tasks from the comfort of your PC, but it also ensures that you can take advantage of your PC's raw capabilities. Today, we'll take a look at a complex file type—jar—and tell you how to open it on your Windows 11 or Windows 10 PC. What is a jar file? jar is an archive package format that may or may not contain an executable Java program. These archives can contain Java applications or source code, which can then be used to compile and run applications without having to write separate code for each application. You can use various methods

Microsoft has built several security features into Windows to ensure your PC remains secure. One of them is driver signature enforcement in Windows 11. When this feature is enabled, it ensures that only drivers digitally signed by Microsoft can be installed on the system. This helps most of the users to a great extent as it protects them. But there is a downside to enabling driver signature enforcement. Suppose you find a driver that works for your device, but it is not signed by Microsoft, although it is completely safe. But you won't be able to install it. Therefore, you must know how to disable driver signing in Windows 11

Here are some possible reasons why you need to disable Microsoft Store: Stop unwanted notifications. Preserves data by limiting the background processes of the Microsoft Store to improve security or privacy Addresses some issues related to the Store or the apps it installs. Restrict children, family members, or other users from downloading applications without permission. Steps to Disable or Enable Windows Store Before following this tutorial, disabling the Microsoft Store will also stop the installation of any apps that require its services. To use the store, users need to enable its service in the same way as blocking it. 1. Block WindowsStore background service Let us from restricting Microsoft Store as

Why am I encountering Windows 11 activation error 0xc004c060? First make sure you are using genuine Windows and that the license key is valid. Also, check if it was obtained from an official source and if the key is suitable for the installed OS version. If there is an issue with any of these, you may encounter Windows 11 activation error 0xc004c060. So be sure to verify these and if you find everything is in order, move on to the next section. If you obtained the key through unreliable means or believe that the installed copy is a pirated version, you will need to purchase a valid key from Microsoft. In addition to this, misconfigured settings, missing

There can be several reasons why runtime error 339 occurs when running a program. Some of them may be that some ocx or dll files are missing, damaged or the dll or ocx is not registered. This would be an annoying experience for the smooth execution of the application. Here are some of the possible error messages you may see for Runtime Error 339 Error – Runtime Error 339: Component DUZOCX32.OCX is not registered correctly or the file is missing. Error – Runtime Error 339. Component MSMASK32.ocx or one of its dependencies is not registered correctly; the file is missing or invalid. Error – Runtime Error '339': Component 'FM20.DLL' or one of its dependencies was not correctly noted

Microsoft regularly releases Windows updates to improve functionality or increase the security of the operating system. You can ignore some of these updates, but it's important to always install security updates. While installing these updates, you may encounter error code; 0x80010105. An unstable connection usually causes most update errors, and once the connection is reestablished you're good to go. However, some require more technical troubleshooting, such as the 0x80010105 error, which is what we will see in this article. What causes WindowsUpdate error 0x80010105? This error may occur if your computer has not been updated in a while. Some users may have permanently disabled W for their reasons

If you are a Windows user and want to disable the core parking functionality in your system, this article will guide you through the process. What is core parking? The core parking function is a power saving mechanism. It puts some of your processors into a sleep mode that doesn't perform any tasks and consumes very little or no power. This helps reduce energy consumption and therefore heat in the system. These cores are unparked when needed. Few users need to disable this feature, especially gamers. Disabling this feature will improve your system performance. Disable Windows Core Parking using Windows Registry Editor Step 1: Use Windows + R keys simultaneously to open the run prompt. Step 2: At the run prompt

There are many reasons why you might want to disable the Delivery Optimization service on your Windows computer. However, our readers complained about not knowing the correct steps to follow. This guide discusses how to disable the Delivery Optimization service in a few steps. To learn more about services, you may want to check out our How to open services.msc guide for more information. What does Delivery Optimization Service do? Delivery Optimization Service is an HTTP downloader with cloud hosting solution. It allows Windows devices to download Windows updates, upgrades, applications and other large package files from alternative sources. Additionally, it helps reduce bandwidth consumption by allowing multiple devices in a deployment to download these packages. In addition, Windo
