码迷,mamicode.com
首页 > 编程语言 > 详细

python3之paramiko模块

时间:2019-10-04 11:35:07      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:目录   需要   远程登录   tde   读取   介绍   exe   exec   close   

阅读目录

paramiko模块介绍

paramiko模块提供了基于ssh连接,进行远程登录服务器执行命令和上传下载文件的功能。这是一个第三方的软件包,使用之前需要安装

paramiko的使用方法

以kali为实验对象,ip:192.168.41.147

1)基于用户名和密码的sshclient方式登陆

#!/usr/bin/env python
#coding:utf8

import paramiko
#创建sshclient对象
ssh = paramiko.SSHClient()
#允许将信任的主机自动加入到host_allow 列表,此方法必须放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#调用connect方法连接服务器
ssh.connect(hostname=172.16.32.129,port=2323,username=root,password=123.com)
while True:
    input_command = input(>>>:)
    if input_command == quit:
        break
    #执行命令,输出结果在stdout中,如果是错误则放在stderr中
    stdin,stdout,stderr = ssh.exec_command(input_command)
    result = stdout.read() #read方法读取输出结果
    if len(result) == 0:  #判断如果输出结果长度等于0表示为错误输出
        print(stderr.read())
    else:
        print(str(result,utf-8))
ssh.close()

 

  

 

python3之paramiko模块

标签:目录   需要   远程登录   tde   读取   介绍   exe   exec   close   

原文地址:https://www.cnblogs.com/BOHB-yunying/p/11621449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!