自动化 SSH 登录导致远程 shell 无响应,返回到客户端终端(无错误)

自动化 SSH 登录导致远程 shell 无响应,返回到客户端终端(无错误)

我在 Ubuntu 14.04 LTS 下使用 expect 工具时遇到了问题。我想自动通过 ssh 登录到某些 Sophos UTM 防火墙,然后使用“sudo su -”和正确的密码直接提升我的权限。我不必担心纯文本密码,因为我的脚本直接从 KeePass URL 字段中运行(双击执行脚本并通过脚本后面的 agrument/KeePass {Placeholder} 用正确的密码填充它)。我设法完成了所有这些工作,除了有一个远程根 shell,它不执行任何命令并“断开连接”回到我的 ubuntu 系统。因此,我尝试远程运行的命令在 4-5 秒内没有执行,然后突然在 ubuntu 系统上执行,没有告诉我发生了什么。

我必须做什么才能拥有功能齐全的远程 shell?SSH 密钥和直接 root 登录对我来说不是一个解决方案,因为我们那里有太多的 Sophos UTM。

解释发生的事情:

sshtool.sh:
#!/usr/bin/expect -f
spawn sshpass -pPASSWORD ssh -t [email protected] "sudo su -"
expect -- "oot's password:"
send "PASSWORD\r"
expect -- "/root #"
expect eof



What happens in the terminal:
vct@vct-virtual-machine:~$ ./sshtool.sh
spawn sshpass -pPASSWORD ssh -t [email protected] sudo su -
root's password:
utm:/root # whoami
# *enter*
# not reacting for 4-5 seconds
vct@vct-virtual-machine:~$ whoami
vct
vct@vct-virtual-machine:~$

像这样更改脚本对于解决断开连接的问题没有帮助:

sshtool.sh:
#!/usr/bin/expect -f
spawn ssh [email protected]
expect -- "password:"
send "PASSWORD\r"
expect -- "/home/login > "
send -- "sudo su -\r"
expect -- "oot's password:"
send "PASSWORD\r"
expect -- "/root #"
send -- "whoami\r"
expect eof


vct@vct-virtual-machine:~$ ./sshtool.sh
spawn ssh [email protected]
[email protected]'s password:
Last login: Mon Apr 18 09:14:41 2016 from 192.168.1.44


Sophos UTM
(C) Copyright 2000-2015 Sophos Limited and others. All rights reserved.
Sophos is a registered trademark of Sophos Limited and Sophos Group.
All other product and company names mentioned are trademarks or registered
trademarks of their respective owners.

For more copyright information look at /doc/astaro-license.txt
or http://www.astaro.com/doc/astaro-license.txt

NOTE: If not explicitly approved by Sophos support, any modifications
      done by root will void your support.

<M> loginuser@utm:/home/login > sudo su -
root's password:
# Following "whoami" directly executed by the script itself works fine
<M> utm:/root # whoami
root
# After 4-5 seconds, it's dropping the connection again
<M> utm:/root # vct@vct-virtual-machine:~$

提前致谢!

答案1

如果您想要一个“响应式”远程 shell,则在完成登录交换后需要一个interact命令。如下所示:

#!/usr/bin/expect -f
spawn sshpass -pPASSWORD ssh -t [email protected] "sudo su -"
expect -- "oot's password:"
send "PASSWORD\r"
expect -- "/root #"
interact

相关内容