#!/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. springboot之内嵌tomcat修改端口号

    第一种: 在配置文件中直接修改 server.port=8089 第二种: 直接定义 TomcatEmbeddedServletContainerFactory,注册bean: @Configurat ...

  2. spring 事务管理机制

    1. spring 事务管理抽象 spring 的事务策略机制的核心就是 org.springframework.transaction.PlatformTransactionManager 接口. ...

  3. windows emacs 中拷贝文件

    cp d:/workspace/LoginWeb/target/LoginWeb.war D:/Program\ Files/apache-tomcat-7.0.78/webapps/LoginWeb ...

  4. LeetCode GrayCode

    class Solution { public: vector<int> grayCode(int n) { vector<int> res; res.push_back(); ...

  5. 如何面试Web前端开发

    分享一篇HR前端面试心得: 面试前端工程师对我来说是一件非常有意思的事,因为面试过程很大程度上也是自我提升的过程.无论大公司还是小公司,之所以在如何招聘到真正有能力的,前端工程师方面会遇到同样的问题. ...

  6. 13 Reasons Why You Should Pay Attention to Mobile Web Performance

    Mobile is no longer on the sidelines. If you’re not already thinking mobile first, you should at lea ...

  7. drupal的权限设置

    通过hook_menu()设置url的权限,有两种方式: 方式一:定义函数,通过 access callback 'access callback' => 'fun()', function f ...

  8. Python爬虫教程-11-proxy代理IP,隐藏地址(猫眼电影)

    Python爬虫教程-11-proxy代理IP,隐藏地址(猫眼电影) ProxyHandler处理(代理服务器),使用代理IP,是爬虫的常用手段,通常使用UserAgent 伪装浏览器爬取仍然可能被网 ...

  9. 结合java的反射和泛型性质简化JDBC和相应的同步等服务器数据库操作代码

    github地址:https://github.com/hzphzp/HeartTrace_Server 我们的服务器端数据库并没有用sqllite, 而是直接用mysql,并且用JDBC直接进行操作 ...

  10. Linux /etc/fstab文件

    一,作用 /etc/fstab是用来存放文件系统的静态信息的文件,当系统启动时,系统会自动地从这个文件读取信息,并且会自动将此文件中指定的文件系统挂在到执行的目录 二,挂载的限制 1,根目录是必须挂载 ...