- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我尝试使用 paramiko 远程执行多个命令,但是 recv_ready()
未返回正确的值。
例如,在执行pwd\n
命令之后,它将连续报告 channel 尚未准备好(显然是错误的)。对于某些命令它可以正常工作,例如ls
。我正在做的事情有问题吗,或者 paramiko 有问题吗?
import paramiko
import re
import time
def sudo_ssh(hostname, usernameIn, passIn, cmd):
# Create an SSH client
client = paramiko.SSHClient()
# Make sure that we add the remote server's SSH key automatically
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the client
client.connect(hostname, username=usernameIn, password=passIn)
# Create a raw shell
channel = client.invoke_shell()
# Send the sudo command
for command in cmd:
print("CMD= " + command + "\n")
time.sleep(1)
# wait until channel is ready
while not channel.recv_ready() :
print("NOT READY " + str(channel.recv_ready()) + "\n \n")
time.sleep(1)
# Send the command
channel.send(command)
channel.send("\n")
# Wait a bit, if necessary
time.sleep(1)
# Flush the receive buffer
receive_buffer = channel.recv(4096)
# If promted send the sudo pass
if re.search(b".*\[sudo\].*", receive_buffer):
time.sleep(1)
print(" TYPING SUDO PASSWORD .... \n")
channel.send( "sudoPass" + "\n" )
receive_buffer = channel.recv(4096)
# Print the receive buffer, if necessary
print(receive_buffer)
print("Executed all of the commands. Now will exit \n")
client.close()
com = []
com.append("sudo ls")
com.append("cd /home/user/Downloads")
com.append("sleep 5")
com.append("ls")
com.append("pwd")
com.append("cd /opt/")
sudo_ssh("myhost.com", "user", "pass", com)
最佳答案
recv_ready
方法是检查 channel 的数据是否准备好读取,即数据是否被缓冲。它不会检查 channel 本身是否已准备好,请参阅 - recv_ready() .
所以你应该移动 recv_ready()
while 循环就在 receive_buffer = channel.recv(4096)
之前使其发挥作用。
关于python - Paramiko recv_ready() 返回假值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451767/
我不是 C 程序员。有什么想法吗? 这是我尝试过的安装命令: pip install paramiko sudo pip install paramiko 输出: src/_fastmath.c:15
Importing paramiko throws following exception on Python3. Traceback (most recent call last): File
我正在编写一个 Python 脚本,需要下载远程 xml 文件来解析它。 我正在使用 paramiko。 这是脚本: def copyFile(ip, user, pwd, remotePath, l
好的,所以我正在尝试在我的 python 脚本中实现 paramiko。 目的是连接同一局域网内的另一台PC,通过python执行命令。 我的 python 版本: Python 2.7.6 (def
我正在调试一些代码,这将导致我不断地登录/退出一些外部 sftp 服务器。有谁知道如果代码中出现非 paramiko 异常,paramiko 是否会自动关闭外部服务器上的 ssh/sftp sessi
我不断收到以下错误: Traceback (most recent call last): File "/home/d/workspace/paramiko/connect_test/main.p
我正在尝试通过 SSH 从 Python 远程执行命令,在这种特殊情况下需要将 stdin 重定向到/dev/null。 也就是说,与使用带有 -n 标志的 OpenSSH 客户端相同: ssh -n
当我尝试打开 mysql 工作台时,它显示“导入错误:没有名为 paramiko 的模块;操作失败:无法启动 SSH 隧道管理器”,尽管我已经安装了 paramiko。我正在使用 python 2.7
我正在尝试使用 paramiko 通过 netcat 反弹 SSH session : MyLocalMachine ----||----> MiddleMachine --(netcat)-->
我目前正在开发一个程序,该程序尝试联系运行 Cisco IOS 的众多路由器以获取其当前配置。我正在尝试使用 Paramiko 模块的 SSHClient 来实现这一点目的: def get_conf
我有基于 paramiko 模块的 python 2.7 代码,我想将其移植到 python 3.3,但我似乎找不到可以工作的 paramiko 版本或合适的替代版本。 我需要该模块在 Windows
我正在尝试运行使用 SSHLibrary 的机器人框架测试用例。我已经安装了所有必需的库(在本例中为 paramiko)。但我仍然收到此错误。 我得到的错误是: Error in file '/opt
有什么方法可以在后台运行 mysql 命令或保持连接事件一段时间,以便可以对其执行查询。可以用nohup来实现吗?如果是,怎么办? 最佳答案 是的,你可以用ajax来做到这一点 使用Nohup你可以做
我正在尝试在 python 中使用 paramiko 执行一个简单的代理命令。 基本上我正在尝试复制此 ssh 命令的行为: ssh -i ~/.ssh/destination_key user@de
This question already has answers here: Pass input/variables to command/script over SSH using Python
Paramiko 支持基于证书的身份验证吗? connect 方法中的 key_filename 提到它同时支持私钥和证书,这里的证书是指基于证书的身份验证吗? key_filename (str)
我刚刚尝试与 paramiko 进行 ssh 连接。一切看起来都很好,但在最后一步,当调用 close() 方法断开客户端连接时。 这是我的脚本: #!/usr/bin/python import p
上下文 如果有一个连接到服务器的脚本,然后 curl 本地主机以获取我需要的信息 问题 我的问题是,我需要从大约 200 台服务器获取信息,我使用的方法大约需要 15 分钟才能完成,这还不错,但我想做
我在尝试关闭 Paramiko SFTP 连接时遇到问题。即使我调用关闭连接仍然挂起,我通过运行 netstat (Windows) 检查: netstat -an | find ":22" 和pyt
localpath = 'U:\\' utime = sftp.stat("/TestBTEC/").st_mtime last_modified = datetime.fromtimestamp(u
我是一名优秀的程序员,十分优秀!