Paramiko无法获取grep命令的输出

时间:2016-06-20 11:35:57

标签: python linux paramiko

我正在使用$(document).ready(function() { $("#content").find("[id^='tab']").hide(); // Hide all content $("#tabs li:first").attr("id", "current"); // Activate the first tab $("#content #tab1").fadeIn(); // Show first tab's content $('#tabs a').click(function(e) { e.preventDefault(); if ($(this).closest("li").attr("id") == "current") { //detection for current tab return; } else { $("#content").find("[id^='tab']").hide(); // Hide all content $("#tabs li").attr("id", ""); //Reset id's $(this).parent().attr("id", "current"); // Activate this $('#' + $(this).attr('name')).fadeIn(); // Show content for the current tab $(this).css("background-color",getRandomColor()); } }); }); function getRandomColor() { var letters = '0123456789ABCDEF'.split(''); var color = '#'; for (var i = 0; i < 6; i++ ) { color += letters[Math.floor(Math.random() * 16)]; } return color; } 库。

使用paramikoshell = ssh.invoke_shell()

我发送的命令是shell.send()

但我没有得到输出。

我正在使用cmd = """grep -i "Lost LLUS websocket" /var/log/debesys/cme.log\n"""来获取输出但我总是空白。我已经手动测试了命令并且它工作正常。有没有人知道如何获得输出?

1 个答案:

答案 0 :(得分:0)

我不确定这是否能按照您想要的方式正确运行,但只是不涉及问题标题,如果要发送grep命令本身,这就是我的处理方法:

def run_cmd(sshClient, command):
    channel = sshClient.get_transport().open_session()
    channel.get_pty()
    channel.exec_command(command)
    out = channel.makefile().read()
    err = channel.makefile_stderr().read()
    returncode = channel.recv_exit_status()
    channel.close()                       # channel is closed, but not the client
    return out, err, returncode

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username = user, password = pw)
out, err, rc = run_cmd(client, 'grep -i "Lost LLUS websocket" /var/log/debesys/cme.log')
# Do whatever with the output
# Run any other commands
client.close()

当然,还有许多其他选择。您可以将文件scp到本地计算机,用python打开它,然后搜索它。您甚至可以通过ssh调用对文件进行分类,并在输出中使用python搜索。

相关问题