Paramiko模块学习
#!/usr/bin/env python # Author:Zhangmingda import paramiko '''创建ssh对象''' ssh = paramiko.SSHClient() '''设置允许连接不在know host的主机''' ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) '''连接服务器''' ssh.connect(hostname='49.4.13.98',port=23782,username='root',password='Wyf@1314') '''执行命令获取结果''' stdin, stdout,stderr = ssh.exec_command('ls') result_in = stdin result_out =stdout.read().decode() result_err = stderr.read().decode() # print(result) # print('type:',type(stdin)) print('stdin:',result_in) print('stdout:',result_out) print('stder:',result_err) ssh.close()
#!/usr/bin/env python # Author:Zhangmingda import paramiko id_rsa = paramiko.RSAKey.from_private_key_file('id_rsa') ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) ssh.connect(hostname='49.4.13.98',port=23782,username='root',pkey=id_rsa) stdin,stdout,stderr = ssh.exec_command('ls') stdout_result = stdout.read().decode() print(stdout_result) ssh.close()
#!/usr/bin/env python # Author:Zhangmingda import paramiko '''创建私钥对象给ssh传输对象用''' id_rsa = paramiko.RSAKey.from_private_key_file('id_rsa') transobj = paramiko.Transport(('49.4.13.98',23782)) transobj.connect(username='root',pkey=id_rsa) '''将已经连接了服务器的对象给sftp客户端实例,让客户端实例进行上传下载操作''' '''创建sftp对象''' sftpobj = paramiko.SFTPClient.from_transport(transobj) # sftpobj.get('/etc/passwd','hehepasswd') sftpobj.put('hehepasswd','/tmp/from_windows_passwd') transobj.close()
#!/usr/bin/env python # Author:Zhangmingda import paramiko '''创建一个ssh传输连接对象''' trans_addr = paramiko.Transport(('49.4.13.98',23782)) trans_addr.connect(username='root',password='Wyf@1314') '''将已经连接了服务器的对象给sftp客户端实例,让客户端实例进行上传下载操作''' sftp = paramiko.SFTPClient.from_transport(trans_addr) sftp.get('/etc/passwd','from_sshserver_etc_passwd.') sftp.put('python_av.avi','/root/a.avi') sftp.close() trans_addr.close() '''关闭sftp客户端'''
posted on 2018-06-28 10:43 zhangmingda 阅读(132) 评论(0) 编辑 收藏 举报