paramiko有密码和rsa方式无密码ssh、ftp

JasonMing 9年前

来自: http://my.oschina.net/yangting880815/blog/608143


  首先导入paramiko模块

import paramiko

1,有密码ssh

s=paramiko.SSHClient()  s.set_missing_host_key_policy(paramiko.AutoAddPolicy())  s.connect(hostname = ssh_ip,username=ssh_username, password=ssh_password)  stdin,stdout,stderr=s.exec_command('[ ! -d "/jenkins" ]&& mkdir -p /jenkins;echo $?;')  s.close()

2,有密码ftp

t=paramiko.Transport((ssh_ip,22))  t.connect(username=ssh_username,password=ssh_password)  sftp=paramiko.SFTPClient.from_transport(t)  sftp.put("/jenkins/lib/jenkins-cpp.sh","/tmp/jenkins-war.sh")  t.close()

3,rsa无密码ssh

PKEY='/root/.ssh/id_rsa'  s=paramiko.SSHClient()  s.load_system_host_keys()  s.connect(SSH_IP,SSH_PORT,SSH_HOST_NAME,pkey=key)  stdin,stdout,stderr=s.exec_command('[ ! -d "/jenkins" ]&& mkdir -p /jenkins;echo $?;')  s.close()

4,rsa无密码ftp

PKEY='/root/.ssh/id_rsa'  t=paramiko.Transport((ssh_ip,SSH_PORT))  t.connect(username=SSH_HOST_NAME, pkey=key)  sftp=paramiko.SFTPClient.from_transport(t)  sftp.put(CONSTANT+"/jenkins-war.sh","/tmp/jenkins-war.sh")  t.close()