Python 连接Linux服务器完成上传下载和执行命令及查询目录下的文件

Python 连接Linux服务器完成上传下载和执行命令及查询目录下的文件

记录一些用于连接linux获取远端文件或者上传文件的小工具,另外还有执行shell命令和查找linux目录下文件是否存在

# -*- coding: utf-8 -*-
"""
@Time    : 2021/7/22 10:31
@Author  : chenhj
@File    : sshclient.py
@Software: PyCharm 
@Comment : 
"""
import osfrom stat 
import S_ISDIR
import re
import paramikofrom paramiko.ssh_exception 
import NoValidConnectionsError, AuthenticationException, SSHException

class SshRemoteConnection:
	def __init__(self, hostname, port, user, password):
		self.hostname = hostname
		self.port = port
		self.user = user
		self.password = password
	def do_connect(self, conn):    
		"""创建连接"""    
		if conn == "SSHClient":        
			client = paramiko.SSHClient()        
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy())        
			try:            
				client.connect(hostname=self.hostname,port=self.port,username=self.user,password=self.password)
				print("正在连接 {}".format(self.hostname).center(100, '*'))       
			except NoValidConnectionsError as e:            
				print(f"连接失败:{
     e}".center(100, '*'))        
			except AuthenticationException as e:            
				print(f"密码错误:{
     e}".center(100, '*'))        
			return client    
		elif conn == "Transport":        
			# 获取Transport实例        
			tran = paramiko.Transport(self.hostname, int(self.port))        
			try:            
			# 连接SSH服务端            
				tran.connect(username=self.user, password=self.password)        
			except SSHException as e:            
				print(f'连接失败:{
     e}'.center(100, '*'))        
			return tran
	def 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值