主要是通过paramiko库实现SSH连接功能,并实现一些远程交互功能
推荐安装方式
pip install paramiko
# -*- coding:utf-8 -*-
import os
import paramiko
host = '192.168.10.10'
port = 22
username = 'hadoop'
password = 'hadoop'
sshClient = paramiko.SSHClient()
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
private_key_file = os.path.expanduser('C:/Program Files/VMware/VMware Share/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(private_key_file)
sshClient.connect(host, port, username, password, pkey = mykey, timeout = 300)
stdin, stdout, stderr = sshClient.exec_command('ls .')
print stdout.read()
sftpd = sshClient.open_sftp()
src = 'F:/workspace/data/esentity-0.0.1-SNAPSHOT.jar'
dest = '/home/data/esentity-0.0.1-SNAPSHOT.jar'
sftpd.put(src, dest)
src = '/home/data/esentity-0.0.1-SNAPSHOT.jar'
dest = 'F:/workspace/data/esentity-0.0.1-SNAPSHOT.jar'
sftpd.get(src, dest)
sshClient.close()