#!/usr/bin/python3
import paramiko
import os
import sys
import subprocess
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
from src.logutils import logger
log=logger("root",rootstdout=True,handlerList=['I','E'])
class SshConnect(object): def __init__(self,ip,user,pwd,port):
self.port=port
self.pwd=pwd
self.user=user
self.ip=ip
def get_connected_client(self):
client=paramiko.SSHClient()
try:
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(self.ip,self.port,self.user,self.pwd)
if client:
return client
except ConnectionError as e:
log.error(e)
def exe_cmd(self,shell):
client=self.get_connected_client()
try:
input, out, err =client.exec_command(shell)
res,err = out.read(),err.read()
result = res if res else err
if type(result).__name__ == 'bytes':
return result.decode('utf-8')
elif type(result).__name__ == 'str':
return result except Exception as e:
log.error(e)
finally:
if client:
client.close()
else:
log.info("ssh client is null not need close") def run():
ip='192.168.81.129'
port=22
pwd='admin'
user='root'
client=SshConnect(ip,user,pwd,port)
res=client.exe_cmd('ps -ef|grep python&&df -m') #type should be str
log.info(res) # def deal_res(res):
# std=''
# for i in res:
# output=i.decode("utf-8")
# std +=output
# return std if __name__ == '__main__':
run()

  

"""
注意点:remote_path 和 local_path ,
路径必须为带文件名全路径只到目录路径会报:
[Errno 13] Permission denied,若路径怕出错可以在最前面加r """ def downlodfile_from_linux(self,remote_path,local_path):
try:
transport = paramiko.Transport(self.host ,self.port)
transport.connect(username=self.user,password=self.pwd)
sftp=paramiko.SFTPClient.from_transport(transport)
sftp.get(remotepath=remote_path,localpath=local_path)
transport.close()
except Exception as e:
log.error(e)
else:
log.info(" download ok")
def upload_file_to_linux(self,local_path,remote_path):
try:
transport = paramiko.Transport(self.host ,self.port)
transport.connect(username=self.user,password=self.pwd)
sftp=paramiko.SFTPClient.from_transport(transport)
sftp.put(localpath=local_path,remotepath=remote_path)
transport.close()
except Exception as e:
log.error(e)
else:
log.info("upload ok")

  

C:\Python37\python.exe C:/Users/Administrator/PycharmProjects/checkTest/src/sshutils.py
[INFO]2019-05-19 22:41:35 Sun --paramiko.transport-- transport.py>>_log function ::at line 1746 :
Connected (version 2.0, client OpenSSH_7.4)
[INFO]2019-05-19 22:41:35 Sun --paramiko.transport-- transport.py>>_log function ::at line 1746 :
Authentication (publickey) failed.
[INFO]2019-05-19 22:41:35 Sun --paramiko.transport-- transport.py>>_log function ::at line 1746 :
Authentication (password) successful!
[INFO]2019-05-19 22:41:36 Sun --root-- sshutils.py>>run function ::at line 53 :
root 6650 1 0 16:33 ? 00:00:56 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
root 7294 1 0 16:33 ? 00:00:11 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root 64041 64030 0 22:41 ? 00:00:00 bash -c ps -ef|grep python&&df -m
root 64057 64041 0 22:41 ? 00:00:00 grep python
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 27627 8654 18974 32% /
devtmpfs 894 0 894 0% /dev
tmpfs 910 1 910 1% /dev/shm
tmpfs 910 11 900 2% /run
tmpfs 910 0 910 0% /sys/fs/cgroup
/dev/sda1 1014 232 783 23% /boot
tmpfs 182 1 182 1% /run/user/42
tmpfs 182 0 182 0% /run/user/0

 

Python 自动化paramiko操作linux使用shell命令,以及文件上传下载linux与windows之间的实现的更多相关文章

  1. Linux下安装rz、sz命令(文件上传下载)

    yum install -y lrzsz 说明:rz命令本地上传文件到服务器:sz命令发送文件到本地.

  2. 每天一个linux命令(文件上传下载文件操作):【转载】gzip命令

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...

  3. linux常用命令(二)文件上传下载及软件安装

    1.上传下载工具安装 (1)WINDOWS 到linux的文件上传及下载: windows下打开secureCRT,通过SSH连到⾄至远程linux主机:上传下载工具安装命令:yum -y insta ...

  4. linux CentOS 安装rz和sz命令 lrzsz 实现windows和linux之间的文件上传 下载

    https://blog.nbhao.org/1902.html https://bbs.csdn.net/topics/391989523 https://www.cnblogs.com/zhoul ...

  5. Selenium2学习-039-WebUI自动化实战实例-文件上传下载

    通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...

  6. 将windows下的文件上传到Linux服务器上

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lx_Frolf/article/deta ...

  7. xshell如何将Windows文件上传到linux

    1.      首先先将你xshell配置好用户名及密码等,必须使用有权限下载的账号进行操作. 使用 yum provides */rz 这条命令,查看你系统自带的软件包的信息. 2.在输出的信息中可 ...

  8. 《如何将windows上的软件包或文件上传到linux服务上》

    昨天晚上朋友让我帮他简单的搭建个环境,他公司让他做款软件测试温度的,他自己搞的是嵌入式,在公司担任的是软件工程师,应届毕业生.也可能他们搞嵌入式的对这个linux系统不太熟,不会把windows上的软 ...

  9. 将Windows上的文件上传到Linux上

    下载一个SSH Secure Shell Client即可. SSHSecureShellClient-3.2.9下载地址: 免费下载地址在 http://linux.linuxidc.com/ 用户 ...

随机推荐

  1. Groovy内存机制详解

    groovy每执行一次脚本,都会生成一个脚本的class对象,并new一个InnerLoader去加载这个对象. 所有的脚本都是由GroovyClassLoader加载的,每次加载脚本都会生成一个新的 ...

  2. poj 2992 Divisors (素数打表+阶乘因子求解)

    Divisors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9617   Accepted: 2821 Descript ...

  3. POJ 1789(最小生成树)

    这题要把给的字符串变成边的权值 #include <cstdio> #include <iostream> #include <queue> #include &l ...

  4. OI中的一些模板

    线性筛 #include"cstdio" #include"cstring" #include"iostream" #include&quo ...

  5. <Android 应用 之路> MPAndroidChart~PieChart

    简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...

  6. input button 与 submit 的区别

    在表单中,我们会经常提交数据,通常使用<input type="submit" value="提交"/>进行提交数据, 另一种方式是使用<bu ...

  7. php中ip转int 并存储在mysql数据库

    遇到一个问题,于是百度一下. 得到最佳答案 http://blog.163.com/metlive@126/blog/static/1026327120104232330131/     如何将四个字 ...

  8. mac下 IDEA 的pom下 出现 Cannot access in offline mode 问题

    在mac下 配置完maven后发现总是不能引入最新的jar包,google了好久总算找到解决办法: 默认带有work offline ,不清楚这个是干嘛用的.勾选掉 了就行了.

  9. RAP, 高效前后端联调框架,接口文档管理工具

    RAP通过GUI工具帮助WEB工程师更高效的管理接口文档,同时通过分析接口结构自动生成Mock数据.校验真实接口的正确性,使接口文档成为开发流程中的强依赖.有了结构化的API数据,RAP可以做的更多, ...

  10. 微信小程序开发常见之坑

    https://www.cnblogs.com/shunxing/articles/6971648.html input里的value会在浮层上面的,要解决这一问题还是很简单的,在小程序中input有 ...