sshpass 不会 运行 在获取脚本时在 Ubuntu 远程主机上提供命令,但会在 windows 上 运行 并且在同一脚本中 运行
sshpass will not run provided command on Ubuntu remote host when script is sourced, but does run on windows and if run within the same script
我在这里搜索了一下,找不到遇到此问题的人。
我有一个 tool/script 我正在构建,它使用 sshpass 登录到远程主机并提取日志、重新启动等...如果我 运行 主脚本中的函数它 运行没有问题。如果我使用函数获取脚本,那么如果它访问 windows IOT Core 远程主机,它 运行 很好,但如果远程主机是 linux,则不会执行命令。
我的设备:
Macbook Pro 2018
macOs Mojave 10.14.2
申请
航站楼
这是有效的:
mainFile.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
我更喜欢但不起作用的:
MainFile.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 在获取时的行为方式。我不明白为什么远程主机 OS 会有所不同,但这是唯一不同的地方。我尝试了 运行ning 只是一个 ls
或 echo
而没有。我也尝试了 sshpass -p 'password' ssh user@0.0.0.0 <command>
,甚至没有得到 Pseudo-terminal will not be allocated because stdin is not a terminal.
。正如您在第一个输出中所见,已加载 linux 系统信息。这在第二个输出中没有发生。
我知道我可以将所有内容都放在一个脚本中,但我希望扩展此工具,并且宁愿在工具准备就绪时只添加一个菜单项。保持环境清洁也能让我很容易把它传给其他人扩展。
我希望我为一些终端天才提供了足够的信息(此时可能是 TMI)来解决这个问题。
提前致谢。
想通了。我觉得我应该知道这一点,但不知怎的,它让我忘记了。我必须在 sshpass 上添加 -t -oStrictHostKeyChecking=no
。所以命令应该是
sshpass -p 'password' ssh -t -oStrictHostKeyChecking=no "user@0.0.0.0" <<EOF
echo 'password' | sudo -S docker logs --tail 10 container
EOF
希望有人能找到它并对他们有所帮助。
我在这里搜索了一下,找不到遇到此问题的人。
我有一个 tool/script 我正在构建,它使用 sshpass 登录到远程主机并提取日志、重新启动等...如果我 运行 主脚本中的函数它 运行没有问题。如果我使用函数获取脚本,那么如果它访问 windows IOT Core 远程主机,它 运行 很好,但如果远程主机是 linux,则不会执行命令。
我的设备:
Macbook Pro 2018
macOs Mojave 10.14.2
申请
航站楼
这是有效的:
mainFile.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
我更喜欢但不起作用的:
MainFile.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 在获取时的行为方式。我不明白为什么远程主机 OS 会有所不同,但这是唯一不同的地方。我尝试了 运行ning 只是一个 ls
或 echo
而没有。我也尝试了 sshpass -p 'password' ssh user@0.0.0.0 <command>
,甚至没有得到 Pseudo-terminal will not be allocated because stdin is not a terminal.
。正如您在第一个输出中所见,已加载 linux 系统信息。这在第二个输出中没有发生。
我知道我可以将所有内容都放在一个脚本中,但我希望扩展此工具,并且宁愿在工具准备就绪时只添加一个菜单项。保持环境清洁也能让我很容易把它传给其他人扩展。
我希望我为一些终端天才提供了足够的信息(此时可能是 TMI)来解决这个问题。
提前致谢。
想通了。我觉得我应该知道这一点,但不知怎的,它让我忘记了。我必须在 sshpass 上添加 -t -oStrictHostKeyChecking=no
。所以命令应该是
sshpass -p 'password' ssh -t -oStrictHostKeyChecking=no "user@0.0.0.0" <<EOF
echo 'password' | sudo -S docker logs --tail 10 container
EOF
希望有人能找到它并对他们有所帮助。