paramiko模块
1. 安装paramiko(windows和linux一样,执行执行即可)
python3自带了pip3。直接使用pip3为python3安装paramiko。
如果使用pip install则是安装到python 2.7上。
pip3 install paramiko
2. SSHClient:
用于连接远程服务器并执行基本命令:
1>. 基于用户密码的连接:
命令执行:
import paramiko
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname = "", port=22, username="", password="")
# 执行命令, stdout, stderr不会同时有结果
stdin, stdout, strerr = ssh.exec_command("df")
# 获取命令结果
result = stdout.read()
print(result.decode())
# 关闭连接
ssh.close()
三元输出:
out, err = stdout.read(),stderr.read()
res = out if out else err
文件传输:
import paramiko
transport = paramiko.Transport(("hostname", "port"))
transport.connect(username="", password="")
sftp = paramiko.SSHClient.from_transport(transport)
sftp.put("local file", "Destination")
sftp.get("Destination", "local_file")
transport.close()
2> 基于密钥的方式连接:
密钥:
RSA -非对称密钥验证;
公钥: public key
私钥: private key
computer1 连接 computer2
192.168.137.10 => 192.168.137.11
私钥 公钥
生成密码对:
[brace@localhost ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/brace/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/brace/.ssh/id_rsa.
Your public key has been saved in /home/brace/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:BjFv66HsycKgluMwQCYmPdaGLnLcgslc2MkoPQGMflI brace@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|+.. o |
|.+=E. + |
|=*O=o . o |
|XO.* o . |
|*oB . S |
|oo.. . + . |
|o..o o . |
|o= oo . |
|o.. .+ |
+----[SHA256]-----+
[brace@localhost ~]$
python3自带了pip3。直接使用pip3为python3安装paramiko。
如果使用pip install则是安装到python 2.7上。
pip3 install paramiko
2. SSHClient:
用于连接远程服务器并执行基本命令:
1>. 基于用户密码的连接:
命令执行:
import paramiko
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname = "", port=22, username="", password="")
# 执行命令, stdout, stderr不会同时有结果
stdin, stdout, strerr = ssh.exec_command("df")
# 获取命令结果
result = stdout.read()
print(result.decode())
# 关闭连接
ssh.close()
三元输出:
out, err = stdout.read(),stderr.read()
res = out if out else err
文件传输:
import paramiko
transport = paramiko.Transport(("hostname", "port"))
transport.connect(username="", password="")
sftp = paramiko.SSHClient.from_transport(transport)
sftp.put("local file", "Destination")
sftp.get("Destination", "local_file")
transport.close()
2> 基于密钥的方式连接:
密钥:
RSA -非对称密钥验证;
公钥: public key
私钥: private key
computer1 连接 computer2
192.168.137.10 => 192.168.137.11
私钥 公钥
生成密码对:
[brace@localhost ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/brace/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/brace/.ssh/id_rsa.
Your public key has been saved in /home/brace/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:BjFv66HsycKgluMwQCYmPdaGLnLcgslc2MkoPQGMflI brace@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|+.. o |
|.+=E. + |
|=*O=o . o |
|XO.* o . |
|*oB . S |
|oo.. . + . |
|o..o o . |
|o= oo . |
|o.. .+ |
+----[SHA256]-----+
[brace@localhost ~]$
将公钥拷贝到远程服务器上 ssh-copy-id "root@192.168.137.11"
[brace@localhost .ssh]$ ssh-copy-id -i id_rsa.pub brace@192.168.137.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.137.11 (192.168.137.11)' can't be established.
ECDSA key fingerprint is SHA256:w1CGC6ZjemS46PaMLtRzLFhE+Zw5Ckwv0PKcMYAOJis.
ECDSA key fingerprint is MD5:51:07:dc:99:cc:65:75:59:d1:1d:d4:82:b1:6d:b8:82.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
brace@192.168.137.11's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'brace@192.168.137.11'"
and check to make sure that only the key(s) you wanted were added.
命令执行:
import paramiko
private_key =paramiko.RSAKey.from_private_key_file("/home/brace/.ssh/id_rsa")
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname="192.168.137.10", port=22, username="brace", pkey=private_key)
#ssh.connect(hostname="192.168.137.10", port=22, username="brace", password="123456")
# 执行命令,
stdin, stdout, stderr = ssh.exec_command("df")
# 获取命令结果
result = stdout.read()
print(result.decode())
# 关闭连接
ssh.close()
文件传输:
import paramiko
private_key =paramiko.RSAKey.from_private_key_file("/home/brace/.ssh/id_rsa")
transport = paramiko.Transport(("hostname", "port"))
transport.connect(username="", pkey=private_key)
sftp = paramiko.SSHClient.from_transport(transport)
sftp.put("local file", "Destination")
sftp.get("Destination", "local_file")
transport.close()
[brace@localhost .ssh]$ ssh-copy-id -i id_rsa.pub brace@192.168.137.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.137.11 (192.168.137.11)' can't be established.
ECDSA key fingerprint is SHA256:w1CGC6ZjemS46PaMLtRzLFhE+Zw5Ckwv0PKcMYAOJis.
ECDSA key fingerprint is MD5:51:07:dc:99:cc:65:75:59:d1:1d:d4:82:b1:6d:b8:82.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
brace@192.168.137.11's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'brace@192.168.137.11'"
and check to make sure that only the key(s) you wanted were added.
命令执行:
import paramiko
private_key =paramiko.RSAKey.from_private_key_file("/home/brace/.ssh/id_rsa")
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname="192.168.137.10", port=22, username="brace", pkey=private_key)
#ssh.connect(hostname="192.168.137.10", port=22, username="brace", password="123456")
# 执行命令,
stdin, stdout, stderr = ssh.exec_command("df")
# 获取命令结果
result = stdout.read()
print(result.decode())
# 关闭连接
ssh.close()
文件传输:
import paramiko
private_key =paramiko.RSAKey.from_private_key_file("/home/brace/.ssh/id_rsa")
transport = paramiko.Transport(("hostname", "port"))
transport.connect(username="", pkey=private_key)
sftp = paramiko.SSHClient.from_transport(transport)
sftp.put("local file", "Destination")
sftp.get("Destination", "local_file")
transport.close()