首页 > 解决方案 > 获取脚本时,sshpass 不会在 Ubuntu 远程主机上运行提供的命令,但会在 Windows 上运行并且如果在同一脚本中运行

问题描述

我在这里搜索了一下,找不到遇到这个问题的人。

我有一个我正在构建的工具/脚本,它使用 sshpass 登录到远程主机并提取日志、重​​新启动等......如果我在主脚本中运行该函数,它运行没有问题。如果我使用该函数获取脚本,那么如果它访问 Windows IOT Core 远程主机,它运行良好,但如果远程主机是 linux,则不会执行命令。

我的设备:
Macbook Pro 2018
macOs Mojave 10.14.2

应用
终端

这是有效的:

主文件.sh:

#!/bin/bash

function pullWindowsLog {
    sshpass -p 'password' ssh user@0.0.0.0 <<EOF
powershell.exe
Get-Content  c:\Path\To\Log\Log.log -tail 10
EOF
}

function pullLinuxLog {
    sshpass -p 'password' ssh user@0.0.0.0 <<EOF
echo 'password' | sudo -S docker logs --tail 10 container
EOF
}



function mainMenu {
    local PS3="What would you like to do?"
    local options=("Pull Windows Log" "Pull Linux Logs" "Quit")
    select opt in "${options[@]}"
    do
        case $opt in
            "Pull Windows Log")
                pullWindowsLog
                ;;
            "Pull Linux Logs")
                pullLinuxLog
                ;;
            "Quit")
                clear
                break
                ;;
            *) echo "\ninvalid option \n";;
        esac
    done
}

mainMenu

输出:

1) Pull Windows Log
2) Pull Linux Logs
3) Quit


What would you like to do?1 # <- user input

Pseudo-terminal will not be allocated because stdin is not a terminal.
Microsoft Windows [Version 0.0.0.0]
Copyright (c) Microsoft Corporation. All rights reserved.

user@WindowsDevice C:\Data\Users\User>powershell.exe
Windows PowerShell 
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Data\Users\User> Get-Content  c:\Path\To\Log\Log.log -tail 10
00:00:00.0000 Log line 1
00:00:00.0000 Log line 2
00:00:00.0000 Log line 3
00:00:00.0000 Log line 4
00:00:00.0000 Log line 5
00:00:00.0000 Log line 6
00:00:00.0000 Log line 7
00:00:00.0000 Log line 8
00:00:00.0000 Log line 9
00:00:00.0000 Log line 10

PS C:\Data\Users\User>

What would you like to do?2 # <- user input

Pseudo-terminal will not be allocated because stdin is not a terminal.
Welcome to Ubuntu 00.00.00 

  System information as of Day Mon  0 00:00:00 xxx Year

  System load:  0.00               Users logged in:             0
  Usage of /:   00.0% of 00.00GB   IP address for eth0:         00.00.00.00
  Memory usage: 00%                IP address for eth1:         00.00.00.00
  Swap usage:   0%                 IP address for eth2:         00.00.00.00
  Processes:    000

0 packages can be updated.
0 updates are security updates.

[sudo] password for user:

00:00:00.0000 Log line 1
00:00:00.0000 Log line 2
00:00:00.0000 Log line 3
00:00:00.0000 Log line 4
00:00:00.0000 Log line 5
00:00:00.0000 Log line 6
00:00:00.0000 Log line 7
00:00:00.0000 Log line 8
00:00:00.0000 Log line 9
00:00:00.0000 Log line 10

What would you like to do?3 # <- user input

我更喜欢,但不工作:

主文件.sh:

#!/bin/bash

source pullWindowsLog.sh
source pullLinuxLog.sh

function mainMenu {
    local PS3="What would you like to do?"
    local options=("Pull Windows Log" "Pull Linux Logs" "Quit")
    select opt in "${options[@]}"
    do
        case $opt in
            "Pull Windows Log")
                pullWindowsLog
                ;;
            "Pull Linux Logs")
                pullLinuxLog
                ;;
            "Quit")
                clear
                break
                ;;
            *) echo "\ninvalid option \n";;
        esac
    done
}

mainMenu

pullWindowsLog.sh:

#!/bin/bash

function pullWindowsLog {
    sshpass -p 'password' ssh user@0.0.0.0 <<EOF
powershell.exe
Get-Content  c:\Path\To\Log\Log.log -tail 10
EOF
}

pullLinuxLog.sh:

#!/bin/bash

function pullLinuxLog {
    sshpass -p 'password' ssh user@0.0.0.0 <<EOF
echo 'password' | sudo -S docker logs --tail 10 container
EOF
}

输出:

1) Pull Windows Log
2) Pull Linux Logs
3) Quit


What would you like to do?1 # <- user input

Pseudo-terminal will not be allocated because stdin is not a terminal.
Microsoft Windows [Version 0.0.0.0]
Copyright (c) Microsoft Corporation. All rights reserved.

user@WindowsDevice C:\Data\Users\User>powershell.exe
Windows PowerShell 
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Data\Users\User> Get-Content  c:\Path\To\Log\Log.log -tail 10
00:00:00.0000 Log line 1
00:00:00.0000 Log line 2
00:00:00.0000 Log line 3
00:00:00.0000 Log line 4
00:00:00.0000 Log line 5
00:00:00.0000 Log line 6
00:00:00.0000 Log line 7
00:00:00.0000 Log line 8
00:00:00.0000 Log line 9
00:00:00.0000 Log line 10

PS C:\Data\Users\User>

What would you like to do?2 # <- user input

Pseudo-terminal will not be allocated because stdin is not a terminal.

What would you like to do?3 # <- user input

对于任何关心安全的人,我没有在脚本中硬编码任何密码。我只是为这个问题这样写的。我没有传递变量的问题,只是 sshpass 在获取时的行为方式。我不明白为什么远程主机操作系统会有所不同,但这是唯一不同的地方。我试着只运行一个lsecho什么都没有。我也尝试过sshpass -p 'password' ssh user@0.0.0.0 <command>,甚至没有得到Pseudo-terminal will not be allocated because stdin is not a terminal.。正如您在第一个输出中看到的那样,加载了 linux 系统信息。这在第二个输出中没有发生。

我知道我可以在一个脚本中包含所有内容,但我希望扩展这个工具,并且宁愿在工具准备好时添加一个菜单项。保持环境清洁也可以让我轻松地将其传递给其他人以进行扩展。

我希望我为某些终端天才提供了足够的信息(此时可能是 TMI)来解决这个问题。

先感谢您。

标签: linuxbashfunctionpowershellsshpass

解决方案


弄清楚了。我觉得我应该知道这一点,但不知怎的,它让我忘记了。我不得不添加-t -oStrictHostKeyChecking=nosshpass。所以命令应该是

sshpass -p 'password' ssh -t -oStrictHostKeyChecking=no "user@0.0.0.0" <<EOF
echo 'password' | sudo -S docker logs --tail 10 container
EOF

希望有人能找到它并帮助他们。


推荐阅读