python paramiko的使用介绍,,一:使用parami
python paramiko的使用介绍,,一:使用parami
一:使用paramiko
#设置ssh连接的远程主机地址和端口
t=paramiko.Transport((ip,port))
#设置登录名和密码
t.connect(username=username,password=password)
#连接成功后打开一个channel
chan=t.open_session()
#设置会话超时时间
chan.settimeout(session_timeout)
#打开远程的terminal
chan.get_pty()
#激活terminal
chan.invoke_shell()
然后就可以通过chan.send(‘command‘)和chan.recv(recv_buffer)来远程执行命令以及本地获取反馈。
二: paramiko的两个模块介绍
paramiko有两个模块SSHClient()和SFTPClient()
SSHClient()的使用代码:
import paramiko
ssh = paramiko.SSHClient()# 创建SSH对象
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname=‘192.168.2.103‘, port=22, username=‘root‘, password=‘123456‘)
stdin, stdout, stderr = ssh.exec_command(‘ls‘)# 执行命令
result = stdout.read()# 获取命令结果
print (str(result,encoding=‘utf-8‘))
ssh.close()# 关闭连接
SSHClient()里有个transport变量,是用于获取连接,我们也可单独的获取到transport变量,然后执行连接操作
import paramiko
transport = paramiko.Transport((‘192.168.2.103‘, 22))
transport.connect(username=‘root‘, password=‘123456‘)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command(‘df‘)
print (str(stdout.read(),encoding=‘utf-8‘))
transport.close()
用transport实现上传下载以及命令的执行:
#coding:utf-8
import paramiko
import uuid
class SSHConnection(object):
def __init__(self, host=‘192.168.2.103‘, port=22, username=‘root‘,pwd=‘123456‘):
self.host = host
self.port = port
self.username = username
self.pwd = pwd
self.__k = None
def connect(self):
transport = paramiko.Transport((self.host,self.port))
transport.connect(username=self.username,password=self.pwd)
self.__transport = transport
def close(self):
self.__transport.close()
def upload(self,local_path,target_path):
# 连接,上传
# file_name = self.create_file()
sftp = paramiko.SFTPClient.from_transport(self.__transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(local_path, target_path)
def download(self,remote_path,local_path):
sftp = paramiko.SFTPClient.from_transport(self.__transport)
sftp.get(remote_path,local_path)
def cmd(self, command):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
result = stdout.read()
print (str(result,encoding=‘utf-8‘))
return result
ssh = SSHConnection()
ssh.connect()
ssh.cmd("ls")
ssh.upload(‘s1.py‘,‘/tmp/ks77.py‘)
ssh.download(‘/tmp/test.py‘,‘kkkk‘,)
ssh.cmd("df")
ssh.close()
1 #coding:utf-8 2 import paramiko 3 import uuid 4 5 class SSHConnection(object): 6 7 def __init__(self, host=‘192.168.2.103‘, port=22, username=‘root‘,pwd=‘123456‘): 8 self.host = host 9 self.port = port10 self.username = username11 self.pwd = pwd12 self.__k = None13 14 def connect(self):15 transport = paramiko.Transport((self.host,self.port))16 transport.connect(username=self.username,password=self.pwd)17 self.__transport = transport18 19 def close(self):20 self.__transport.close()21 22 def upload(self,local_path,target_path):23 # 连接,上传24 # file_name = self.create_file()25 sftp = paramiko.SFTPClient.from_transport(self.__transport)26 # 将location.py 上传至服务器 /tmp/test.py27 sftp.put(local_path, target_path)28 29 def download(self,remote_path,local_path):30 sftp = paramiko.SFTPClient.from_transport(self.__transport)31 sftp.get(remote_path,local_path)32 33 def cmd(self, command):34 ssh = paramiko.SSHClient()35 ssh._transport = self.__transport36 # 执行命令37 stdin, stdout, stderr = ssh.exec_command(command)38 # 获取命令结果39 result = stdout.read()40 print (str(result,encoding=‘utf-8‘))41 return result42 43 ssh = SSHConnection()44 ssh.connect()45 ssh.cmd("ls")46 ssh.upload(‘s1.py‘,‘/tmp/ks77.py‘)47 ssh.download(‘/tmp/test.py‘,‘kkkk‘,)48 ssh.cmd("df")49 ssh.close()
python paramiko的使用介绍
相关内容
- Python3 数据结构,,Python数据结构
- 如何美观地打印 Python 对象?这个标准库可以简单实现
- python--多线程多进程,,一、进程 对于操作
- python将整数均分成N等分,,在python中,需
- python学习笔记—,,Microsoft
- Python打包分发工具setuptools,,import sys
- Python DataAgent,,一. DataAge
- typeError:The value of a feed cannot be a tf.Tensor object.Acceptable
- appium+python自动化62-webview元素click失效问题解决,,前言
- 安装homeassistant+python3.6,,python3.6×
评论关闭