paramiko

Python模块 - Paramiko

半城伤御伤魂 提交于 2019-12-04 02:23:21
ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography)。 有了Paramiko以后,我们就可以在Python代码中直接使用SSH协议对远程服务器执行操作,而不是通过ssh命令对远程服务器进行操作。 paramiko包含两个核心组件:SSHClient和SFTPClient。 SSHClient的作用类似于Linux的ssh命令,是对SSH会话的封装,该类封装了传输(Transport),通道(Channel)及SFTPClient建立的方法(open_sftp),通常用于执行远程命令。 SFTPClient的作用类似与Linux的sftp命令,是对SFTP客户端的封装,用以实现远程文件操作,如文件上传、下载、修改文件权限等操作。 1 2 3 4 5 # Paramiko中的几个基础名词: 1 、Channel:是一种类Socket,一种安全的SSH传输通道; 2 、Transport:是一种加密的会话,使用时会同步创建了一个加密的Tunnels(通道),这个Tunnels叫做Channel; 3 、Session:是client与Server保持连接的对象,用connect() / start_client() / start_server()开始会话。 Paramiko的基本使用

Paramiko: Piping blocks forever on read

丶灬走出姿态 提交于 2019-12-04 02:11:14
I have a problem with getting piping to work with paramiko. This works: ssh = paramiko.SSHClient() [...] stdin, stdout, stderr = ssh.exec_command("find /tmp") stdout.read() This does not work (blocks forever on stdout.read()): [...] stdin, stdout, stderr = ssh.exec_command("bash -") stdin.write("find /tmp\n") stdin.close() stdout.read() Any ideas? EDIT: I looked at the source code for paramiko, and ChannelFile.close does not really do anything in terms of communication. So I looked at the channel API, and this seems to work: stdin.write("find /tmp\n") stdin.flush() stdin.channel.shutdown_write

What is the difference between exec_command and send with invoke_shell() on Paramiko?

喜你入骨 提交于 2019-12-04 01:58:10
问题 So what is the difference between SSHClient.exec_command() and send with SSHClient.invoke_shell on Paramiko? I can send and execute command with exec_command to MikroTik router device but can't execute it with send ( invoke_shell() ). On the other hand, I can send and execute command send ( invoke_shell() ) to Cisco device, but can't execute it with exec_command . The command I mean is the configuration command like routing (ip route xxx xxx) or make vlan or add an ip address and etc.. 回答1:

paramiko - connect with private key - not a valid OPENSSH private/public key file

邮差的信 提交于 2019-12-03 21:37:09
I am trying to find the solution to this and can't understand what I'm doing wrong. On my linux server I have run the following command: ssh-keygen -t rsa This generated an id_rsa and id_rsa.pub file. I then copied them both locally and attempted to run the following code: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('myserver', username='myuser', key_filename='id_rsa') sftp = ssh.open_sftp() sftp.chdir('/path/to/file') sftp.get(file, os.path.join(tmp_dir, '{0}.existing-{1}'.format('myfile', current_datetime))) except Exception, err: logging

CMDB和自动化运维

陌路散爱 提交于 2019-12-03 20:47:01
CMDB和自动化运维 IT运维的分类 IT运维,指的是对已经搭建好的网络,软件,硬件进行维护。运维领域也是细分的,有硬件运维和软件运维 硬件运维主要包括对基础设施的运维,比如机房的设备,主机的硬盘,内存这些物理设备的维护 软件运维主要包括系统运维和应用运维,系统运维主要包括对OS,数据库,中间件的监控和维护,这些系统介于设备和应用之间,应用运维主要是对线上业务系统的运维 这里讨论的主要是软件运维的自动化,包括系统运维和应用运维的自动化 传统运维痛点 日常工作繁琐 日常运维工作是比较繁琐的,研发同学会经常需要到服务器上查日志,重启应用,或者是说今天上线某个产品,需要部署下环境。这些琐事是传统运维的大部分工作 应用运行环境不统一 在部署某应用后,应用不能访问,就会听到开发人员说,在我的环境运行很好的,怎么部署到测试环境后,就不能用了,因为各类环境的类库不统一 还有一种极端情况,运维人员习惯不同,可能凭自己的习惯来安装部署软件,每种服务器上运行软件的目录不统一 运维及部署效率低下 想想运维人员需要登陆到服务器上执行命令,部署程序,不仅效率很低,并且非常容易出现人为的错误,一旦手工出错,追溯问题将会非常不容易 无用报警信息过多 经常会收到很多报警信息,多数是无用的报警信息,造成运维人员经常屏蔽报警信 另外如果应用的访问速度出了问题,总是需要从系统、网络、应用、数据库等一步步的查找原因

paramiko Incompatible ssh peer (no acceptable kex algorithm)

你说的曾经没有我的故事 提交于 2019-12-03 19:07:24
问题 I'm getting the following error when trying to ssh to a Cisco ACS device using the paramiko library. I've used paramiko in python without issue, and I can ssh to this box from the command line, or using putty without issue. I've turned on debugging and copied the info here. Please let me know if you can help me out. import paramiko import sys import socket try: paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG) sshConnection = paramiko.SSHClient() sshConnection.set_missing_host

如何使用python远程操作linux

≯℡__Kan透↙ 提交于 2019-12-03 18:35:30
在云服务测试中,往往需要我们进入云服务内容进行相关内容的测试。这测试可以使用平台自身的noVNC、外部辅助xshell等工具连接到云服务内部进行测试。 但是在如此反复的测试操作中,就需要用到自动化测试方法去解决这方面的需求。 在python中我们可以通过第三方库paramiko,对linux的云服务器进行操作。 如下命令先行安装 pip install paramiko paramiko包含两个核心组件:SSHClient和SFTPClient。 SSHClient的作用类似于Linux的ssh命令,是对SSH会话的封装,该类封装了传输(Transport),通道(Channel)及SFTPClient建立的方法(open_sftp),通常用于执行远程命令。 SFTPClient的作用类似与Linux的sftp命令,是对SFTP客户端的封装,用以实现远程文件操作,如文件上传、下载、修改文件权限等操作。 Paramiko中的几个基础名词: Channel:一种类Socket,一种安全的SSH传输通道; Transport:一种加密的会话,使用时会同步创建了一个加密的Tunnels(通道),这个Tunnels叫做Channel; Session:client与Server保持连接的对象,用connect()/start_client()/start_server()开始会话。

paramiko模块(基于SSH用于连接远程服务器)

雨燕双飞 提交于 2019-12-03 18:04:35
paramiko模块,基于SSH用于连接远程服务器并执行相关操作 class SSHConnection(object): def __init__(self, host_dict): self.host = host_dict['host'] self.port = host_dict['port'] self.username = host_dict['username'] self.pwd = host_dict['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 run_cmd(self, command): """ 执行shell命令,返回字典 return {'color': 'red','res':error}或 return {'color': 'green', 'res':res} :param command: :return: """ ssh =

paramiko server mode port forwarding

若如初见. 提交于 2019-12-03 15:37:52
I need to implement a ssh server using paramiko that only handles '-R' port forwarding requests like this: ssh -N -T -R 40005:destination_host:22 user@example.com So far from what i understand i'll have to implement ServerInterface.check_port_forward_request and at some point after, create a socket and listen to the specified port. Any data that comes through the Channel/Connection go to Connection/Channel respectively class Server (paramiko.ServerInterface): . . . def check_port_forward_request(self, address, port): 'Check if the requested port forward is allowed' ... return port def handler

Python ssh using Tor proxy

僤鯓⒐⒋嵵緔 提交于 2019-12-03 13:46:13
I would like to be able to send data through Tor when I use ssh from Python scripts. Tor works as expected when I use an OpenSSH client to manually ssh to the host. This is my ssh config file. I use connect-proxy with ProxyCommand to route the connections through Tor (again, this works fine via a standard OpenSSH client): host host user user hostname host.domain.com CheckHostIP no Compression yes Protocol 2 ProxyCommand connect-proxy -S localhost:9050 %h %p I have this Python test script: import paraproxy import paramiko conf = paramiko.SSHConfig() conf.parse(open('/home/user/.ssh/config'))