Linux bash脚本在通过ssh远程运行时不返回输出,但在本地运行时返回输出

时间:2018-02-20 21:38:30

标签: bash shell ssh

我在linux上创建了一个bash脚本,我可以通过ssh远程调用它。出于某种原因,只有当我直接在框上运行命令时,shell脚本才会生成预期的输出。但是,如果我使用完全相同的输入(来自远程框通过ssh )运行完全相同的命令,则不会收到任何输出。

有趣的是,我创建了一个“用法”字符串,当你传入没有参数时打印出来,如果我通过没有参数的ssh调用脚本,我会看到用法字符串。这告诉我ssh客户端可以从远程shell脚本接收输出。

我尝试从同一子网上的另一个linux框调用该命令。我试过从Windows调用命令(cygwin / open ssh)。在这两种情况下,行为都是一样的。

我完全难过了。如何获取通过ssh调用的shell脚本的输出。

如果您想知道shell脚本中的内容,它只生成过去20分钟内修改过的文件列表,然后调用python程序来处理这些文件:

if [ $# -lt 1 ]
then
    echo "Usage: $0 <cluster>"
    echo "-----------------------"
    echo "Example: $0 mycluster"
    exit
fi

clusterName=$1


find /mylogs/abc/123/mydir -mmin -20 -name "mylog*.log" > /home/myuser/file_names.txt
python filterDatesInFiles.py | sed -e "s/^/${HOSTNAME},\/mylogs\/abc\/123\/mydir\/^*~%$/"

如果你对python脚本感兴趣,那么它是:

#!/usr/bin/env python
import datetime, sys, time
# Example log line:
# [2/20/18 10:03:27:187 GMT-07:00] 0000001c SystemOut     O ....
f = open("/home/myuser/file_names.txt")
lines = f.readlines()
f.close()
for line in lines:
    line = line.strip()
    if len(line) == 0:
         continue
    f = open(line)
    fileLines = f.readlines()
    f.close()
    for fileLine in fileLines:
        fileLineStripped = fileLine.strip()
        if len(fileLine) == 0:
            continue
        if not fileLine.startswith("["):
            continue
        fields = fileLine.split(" ")
        dateTime = fields[0].split("[")[1] + " " + fields[1]
        dtObj = datetime.datetime.strptime(dateTime, "%m/%d/%y %H:%M:%S:%f")
        currentTime = datetime.datetime.now()
        d1_ts = time.mktime(dtObj.timetuple())
        d2_ts = time.mktime(currentTime.timetuple())
        minutesAgo = int(d2_ts-d1_ts) / 60
        if minutesAgo <= 15:
            print fileLineStripped

但是,我知道这个脚本有效,因为正如我所说,我可以直接在目标框上运行这个脚本,它会产生正确的输出。

以下是我通过ssh调用它的方法:

ssh myuser@mylinuxserver '/home/myuser/logScraper.sh mycluster'

注意 - 当我用“-v”运行ssh命令时,这是我得到的输出:

$ ssh -v myuser@myhost.mydomain.com '/home/myuser/logScraper.sh mycluster'
OpenSSH_6.1p1, OpenSSL 1.0.1c 10 May 2012
debug1: Connecting to myhost.mydomain.com [123.45.67.61] port 22.
debug1: Connection established.
debug1: identity file /home/myremoteuser/.ssh/id_rsa type 1
debug1: identity file /home/myremoteuser/.ssh/id_rsa-cert type -1
debug1: identity file /home/myremoteuser/.ssh/id_dsa type -1
debug1: identity file /home/myremoteuser/.ssh/id_dsa-cert type -1
debug1: identity file /home/myremoteuser/.ssh/id_ecdsa type -1
debug1: identity file /home/myremoteuser/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.1
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 93:a2:73:80:08:d5:cc:ad:78:c0:df:48:68:cb:b0:f4
debug1: Host 'myhost.mydomain.com' is known and matches the ECDSA host key.
debug1: Found key in /home/myremoteuser/.ssh/known_hosts:40
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/myremoteuser/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/myremoteuser/.ssh/id_dsa
debug1: Trying private key: /home/myremoteuser/.ssh/id_ecdsa
debug1: Next authentication method: password
myuser@myhost.mydomain.com's password:
debug1: Authentication succeeded (password).
Authenticated to myhost.mydomain.com ([123.45.67.61]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending command: /home/myuser/logScraper.sh mycluster
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2080, received 2392 bytes, in 0.5 seconds
Bytes per second: sent 4000.0, received 4600.0
debug1: Exit status 0

为什么我在通过ssh运行时无法获得输出的线索?

0 个答案:

没有答案