paramiko

Python---初识堡垒机

折月煮酒 提交于 2019-12-21 05:15:21
在学习堡垒机之前,我们需要首先了解下Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作。 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='192.168.132.130', port=22, username='wangxin', password='111111') # 执行命令 stdin, stdout, stderr = ssh.exec_command('df') # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close() SSHClient 封装 Transport方式 import paramiko transport = paramiko.Transport(('192.168.132.130', 22)) transport.connect(username='wangxin', password='111111') ssh =

Problem originating SSH tunnels from python

核能气质少年 提交于 2019-12-21 02:57:30
问题 The object is to set up n number of ssh tunnels between satellite servers and a centralized registry database. I have already set up public key authentication between my servers so they just log right in without password prompts. Now what ? I've tried Paramiko. It seems decent but gets pretty complicated just to set up a basic tunnel, although code exmplaes would be aprreciated. I've tried Autossh and it dies 2 minutes after setting up a working tunnel, bizarre! Hopefully someone can help me

Paramiko — using encrypted private key file on OS X

余生长醉 提交于 2019-12-20 18:29:06
问题 I'm trying to use Paramiko to connect to an SSH server from Python. This is what I tried so far: >>> import paramiko >>> import os >>> privatekeyfile = os.path.expanduser('~/.ssh/id_rsa') >>> mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/pkey.py", line 198, in from_private_key_file key = cls(filename=filename,

Timeout in paramiko (python)

泪湿孤枕 提交于 2019-12-20 12:21:37
问题 I'm looking for a way to set a timeout for this: transport = paramiko.Transport((host, port)) transport.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.get(remotepath, localpath) sftp.close() transport.close() 回答1: The connection timeout can be set with the timeout parameter (that indicated the number of seconds for the time out as described here) of the connect function. ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy

Paramiko “Invalid command” when trying non-standard command for Vyatta

自闭症网瘾萝莉.ら 提交于 2019-12-20 03:23:11
问题 I've just tried to connect my Python/Django app with Vyatta server using Paramiko for SSHing. Unfortunately, when I try to run show interfaces it throws "Invalid command". However, if try to SSH manually from that server, it works fine. I tried also '/vbash -c "show interfaces"' - the same result. ssh = paramiko.SSHClient() ssh.connect('10.0.0.1','vyatta','vyatta') stdin, stdout, stderr = ssh.exec_command('show interfaces') # or stdin, stdout, stderr = ssh.exec_command('vbash -c "show

Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko

一笑奈何 提交于 2019-12-19 10:57:47
问题 I'm having a problem with a ShoreTel voice switch, and I'm trying to use Paramiko to jump into it and run a couple commands. What I believe the problem might be, is that the ShoreTel CLI gives different prompts than the standard Linux $ . It would look like this: server1$:stcli Mitel>gotoshell CLI> (This is where I need to enter 'hapi_debug=1') Is Python still expecting that $ , or am I missing something else? I thought it might be a time thing, so I put those time.sleep(1) between commands.

Python, Paramiko: How to do a “ssh -n user@host cmd” using paramiko?

妖精的绣舞 提交于 2019-12-19 10:29:16
问题 I'm trying to execute a command remotely via SSH from Python, and in this particular case need stdin to be redirected to /dev/null. That is, the same as using the OpenSSH client with its -n flag: ssh -n user@host cmd How do you achieve this (-n) with Paramiko? paramiko.SSHClient.exec_command() doesn't seem to allow this, but maybe I'm missing something? 回答1: Unless I understand your question incorrectly: you don't need to achieve this. Nothing is automatically read from stdin/written to the

paramiko.SSHException: Error reading SSH protocol banner

佐手、 提交于 2019-12-19 05:58:31
问题 I am using Paramiko and trying to connect to my SFTP server. Here is the code I wrote: class SFTPUploader: def __init__(self, host, username, password, port): transport = paramiko.Transport((host, port)) print transport transport.connect(username = username, password = password) self.sftp = paramiko.SFTPClient.from_transport(transport) I can connect to my server from the terminal. This thread didn't help since our scenario is different. 回答1: That error is generated when paramiko doesn't

Solving thread cleanup on paramiko

放肆的年华 提交于 2019-12-19 05:25:44
问题 I have an automated process using paramiko and have this error: Exception in thread Thread-1 (most likely raised during interpreter shutdown) .... .... <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'error' I understand that is a problem in the cleanup/threading, but I don't know how to fix it. I have the latest version (1.7.6) and according to this thread, it was solved, so I download the code directly but still get the error. The failure occurs on Python 2.5/2.6

Executing command using “su -l” in SSH using Python

狂风中的少年 提交于 2019-12-19 04:18:14
问题 I use a friends server that allows only one user to be logged from SSH, so normally I just log in as that user and then do su -l myuser to change accounts. I wanted to automate some boring stuff using Python, but I ran into problems with that. Apparently Paramiko module that I tried first invokes a single shell for every command, so that was out of the question. Later I tried using invoke_shell() to overcome that, but it still failed (I assume it's because changing user changes shell as well)