paramiko

Download files from SFTP server that are older than 5 days using Python

别来无恙 提交于 2019-11-30 21:21:17
问题 I got a Python script on this site that downloads files from the directory from SFTP server. Now I need help to modify this code so that it only downloads the files that older than 5 days from the day the code is used. Code to download files (based on Python pysftp get_r from Linux works fine on Linux but not on Windows): import os import pysftp from stat import S_IMODE, S_ISDIR, S_ISREG cnopts = pysftp.CnOpts() cnopts.hostkeys = None sftp=pysftp.Connection('192.168.X.X', username='username'

Using Channels with Paramiko

泄露秘密 提交于 2019-11-30 21:02:28
问题 I am working on converting a tool from Ruby to Python, and the Ruby version uses Net::SSH to connect to a remote host, and send commands and retrieve responses/data. I have been using paramiko in the Python counterpart, but I'm confused as to the purpose of Channels in paramiko. From what I've read so far, it seems to me that a channel (which uses a paramiko Transport) is used to keep a persistent connection to SSH rather than executing a command and then terminating the connection. Is a

Using wildcards in file names using Python's SCPClient library

半腔热情 提交于 2019-11-30 20:53:18
问题 I want to copy files from remote machine using Python script to my local machine. I only know the extension of the file names so I want to use wildcards to express the file name. In addition, I want to use the SCPClient Python library and not the os.system directly as suggested in the question titled using wildcards in filename in scp in python But when I run the following code: from paramiko import SSHClient import paramiko from scp import SCPClient with SSHClient() as ssh: ssh.set_missing

Ansible :一个配置管理和IT自动化工具(1/3)

吃可爱长大的小学妹 提交于 2019-11-30 19:29:00
ansible,一个由 Python 编写的强大的配置管理解决方案。尽管市面上已经有很多可供选择的配置管理解决方案,但他们各有优劣,而 ansible 的特点就在于它的简洁。让 ansible 在主流的配置管理系统中与众不同的一点便是,它并不需要你在想要配置的每个节点上安装自己的组件。同时提供的一个优点在于,如果需要的话,你可以在不止一个地方控制你的整个基础架构。最后一点是它的正确性,或许这里有些争议,但是我认为在大多数时候这仍然可以作为它的一个优点。说得足够多了,让我们来着手在 RHEL/CentOS 和基于 Debian/Ubuntu 的系统中安装和配置 Ansible。 准备工作 发行版:RHEL/CentOS/Debian/Ubuntu Linux Jinja2:Python 的一个对设计师友好的现代模板语言 PyYAML:Python 的一个 YAML 编码/反编码函数库 paramiko:纯 Python 编写的 SSHv2 协议函数库 (译者注:原文对函数库名有拼写错误) httplib2:一个功能全面的 HTTP 客户端函数库 本文中列出的绝大部分操作已经假设你将在 bash 或者其他任何现代的 shell 中以 root 用户执行。 Ansible 如何工作 Ansible 工具并不使用守护进程,它也不需要任何额外的自定义安全架构,因此它的部署可以说是十分容易

CMDB与自动化运维

流过昼夜 提交于 2019-11-30 19:26:14
一、传统的运维痛点 1.1 日常工作繁琐   日常运维工作是比较繁琐的,研发同学会经常需要到服务器上查日志,重启应用,或者是说今天上线某个产品,需要部署下环境。这些琐事是传统运维的大部分工作 1.2 应用运行环境不统一   在部署某应用后,应用不能访问,就会听到开发人员说,在我的环境运行很好的,怎么部署到测试环境后,就不能用了,因为各类环境的类库不统一 还有一种极端情况,运维人员习惯不同,可能凭自己的习惯来安装部署软件,每种服务器上运行软件的目录不统一 1.3 运维及部署效率低下   想想运维人员需要登陆到服务器上执行命令,部署程序,不仅效率很低,并且非常容易出现人为的错误,一旦手工出错,追溯问题将会非常不容易 1.4 无用报警信息过多   经常会收到很多报警信息,多数是无用的报警信息,造成运维人员经常屏蔽报警信   另外如果应用的访问速度出了问题,总是需要从系统、网络、应用、数据库等一步步的查找原因 1.5 资产管理和应用管理混乱   资产管理,服务管理经常记录在excel、文本文件或者wiki中,不便于管理,老员工因为比较熟,不注重这些文档的维护,只有靠每次有新员工入职时,资产才能够更正一次 二、自动化运维平台特性 运维自动化最重要的就是标准化一切 OS的选择统一化,同一个项目使用同样的OS系统部署其所需要的各类软件 软件安装标准化,例如JAVA虚拟机,php,nginx

python paramiko

与世无争的帅哥 提交于 2019-11-30 18:09:58
下载文件 p_key = paramiko.RSAKey.from_private_key_file('id_rsa_path') scp = paramiko.Transport(server_ip, 22) scp.connect(username=ssh_username, pkey=p_key) sftp = paramiko.SFTPClient.from_transport(scp) sftp.get(src_file, tgt_file) 上传文件 p_key = paramiko.RSAKey.from_private_key_file('id_rsa_path') scp = paramiko.Transport(server_ip, 22) scp.connect(username=ssh_username, pkey=p_key) sftp = paramiko.SFTPClient.from_transport(scp) sftp.put(src_file, tgt_file) 来源: https://my.oschina.net/redhands/blog/3112553

How to use Paramiko getfo to download file from SFTP server to memory to process it

≡放荡痞女 提交于 2019-11-30 17:18:28
问题 I am trying to download a CSV file (in-memory) from SFTP using Paramiko and import it into a pandas dataframe. transport = paramiko.Transport((server, 22)) transport.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(transport) with open(file_name, 'wb') as fl: sftp.getfo(file_name, fl, callback=printTotals) df = pd.read_csv(fl, sep=' ') The code below fails, telling me: OSError: File is not open for reading I assume that I need some kind of buffer or file

用Python怎么SSH到网络设备

ⅰ亾dé卋堺 提交于 2019-11-30 16:50:32
[ TOC ] 0. 前言 自上一篇文章《用python怎么telnet到网络设备》,简单使用了telnetlib库给大家演示了下,但是,现实环境中仍不建议去使用telnet。 SSH(Secure Shell)协议也是属于TCP/IP协议族里的一种,端口号22,可以代替telnet来远程管理的一种方法。 SSH提供了双向认证、数据加密等方法保证数据的安全性,推荐使用SSHv2版本 1. 测试环境及关键代码解释 1.1 简单测试环境 使用python3环境 使用第三方Netmiko模块(基于Paramiko库进行改版) 简单的实验环境 1.2 关键代码 import xx:导入模块 class xx:定义类 def xx: 定义函数 try-except :处理可能引发的异常 ssh.enable():进入enable模式 ssh.find_prompt():返回当前提示符 ssh.send_command():发送查询命令并返回结果 ssh.send_config_set():发送配置命令到目标设备 ssh.disconnect():关闭连接 Tips : import ConnectHandler和import Netmiko是一样的效果的哈。 2. 完整代码 ''' 欢迎关注微信公众号:'diandijishu' 此平台是网路工程师个人日常技术、项目案例经验分享,

How to keep ssh session not expired using paramiko?

馋奶兔 提交于 2019-11-30 13:07:45
I intend to run several commands on remote host using paramiko, but the ssh session closed after running a command. The code listed below: from paramiko import SSHClient import paramiko ssh = SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, 22, user, passwd, timeout=3) stdin, stdout, stderr = ssh.exec_command('uname -a') So is there any way to stop the ssh session from closing? Or any alternatives for paramiko? Update : I was able to keeping calling exec_command on my Macbook when connected to a Linux server, but the ssh

How do use paramiko.RSAKey.from_private_key()?

血红的双手。 提交于 2019-11-30 12:41:29
I'm new to paramiko, any idea how I can use the paramiko.RSAKey.from_private_key() function? I know there is a from_private_key_file(), but I'm interested in using a function to parse a private key (like below) and use that private key for SSHClient. Private Key (sample) -----BEGIN RSA PRIVATE KEY-----\nMIICXgIBAAKCAIEAmfgmlY95SHXhCeBNdkhSrsG4JVbqyew845yoZRX3wcS2/doz\niVQxgx0aiOwLi+/Rnkb3PLUIwoxb/LoD/W0YMS6/NSUMt+LdH+zsjeNF2iq4rDzU\nwDSqi27q/8u/egrK7H+9HNKEVXb/87utAAm3VTM9KqKaK3VuVFrNrnsDSuECAwEA\nAQKCAIBZn3y2KiGq8BLiMNJmO4sFdnW+Jm3cw8pdo17SGItzGxJ5iX3ePkfjzhkY\nAm5mMl6OBzj6+VX0CMeywIR6C