• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python的paramiko模块介绍

python 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论

paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。

由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台,如Linux, Solaris, BSD, MacOS X, Windows等,paramiko都可以支持,因此,如果需要使用SSH从一个平台连接到另外一个平台,进行一系列的操作时,paramiko是最佳工具之一。

一、安装paramiko模块

[root@yaoliang ~]# pip install paramiko

二、远程连接

1、方法一

import paramiko ssh = paramiko.SSHClient()                      # 创建客户端连接服务端的对象ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允许连接不在know_hosts文件中的主机ssh.connect(ip,port,username,password)          # 连接远程服务器

2、方法二

import paramiko tus = (ip, port)t = pa<strong style="color:transparent">来2源gaodaima#com搞(代@码&网</strong>ramiko.Transport(tus)                     # 创建传输对象t.connect(username=self.username, password=self.password)

三、文件传输

tus = (ip, port)t = paramiko.Transport(tus)t.connect(username=username, password=password)sftp = paramiko.SFTPClient.from_transport(t)    # 创建下载传输对象sftp.get(localpath, remotepath)                 # 下载sftp.put(localpath, remotepath)                 # 上传t.close()

四、实例

import paramikofrom switch import switch_ip, switch_pwd, switch_usernamefrom lib.read_ini import configfrom lib.logsys import LogSysimport threadingimport time class UpDate(object):     def __init__(self, dev, pname):        self.cf = config()        self.log = LogSys()                           self.pname = pname        self.ip = switch_ip(dev)              # 根据dev获取远程服务器ip        self.username = switch_username(dev)  # 获取用户名        self.password = switch_pwd(dev)       # 获取密码        self.port = int(self.cf.getvalue('server_conf', 'port'))        self.localpath = self.cf.getvalue('write_path', 'upfilepath') + self.pname + ".zip"        self.remotepath = self.cf.getvalue('write_path', 'topath') + self.pname + ".zip"     def __trans_file(self):                   # 文件传输        try:            tus = (self.ip, self.port)            t = paramiko.Transport(tus)       # 创建传输对象            t.connect(username=self.username, password=self.password)  # 连接远程服务器            sftp = paramiko.SFTPClient.from_transport(t)  # 创建下载传输对象            sftp.put(self.localpath, self.remotepath)     # 上传文件            t.close()            print "trans_ok"            return 5        except Exception, e:            print "put_file:" + str(e)            return 0     def __excuteup(self):                     # 远程操作        ssh = paramiko.SSHClient()        comm = '/root/test.sh ' + self.pname        try:            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())            ssh.connect(self.ip,self.port,self.username,self.password,timeout=10) # 连接远程服务器            stdin, stdout, stderr = ssh.exec_command(comm)  # 执行远程服务器上的脚本            out = stdout.readlines()            ssh.close()            return 5        except Exception, e:            errmsg =  "excuteup Error.__excute" + str(e)            ssh.close()            self.log.writelog(errmsg)         # 将错误信息保存到日志            return 0     def runup(self):                          # 执行上传和远程执行命令的操作        if self.__trans_file():            if self.__excuteup():                print "update success."                return 5            else:                print "trans OK. excute ERR!"                return 1        else:            return 1

以上就是 python的paramiko模块介绍的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python的paramiko模块介绍

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址