linux - 如何使用 ssh 通过提供静态文本来执行与键盘交互的操作

标签 linux bash shell ssh openssh

我正在编写一个 shell 脚本以使用 ssh 连接到基于 Linux 的远程计算机

成功登录到远程机器后,我能够执行 Linux 命令,现在我面临的真正问题是当尝试为另一台要求本地身份验证的远程机器运行相同的脚本时(usernamepassword) ssh 登录以继续下一步。

谁能指导我如何用我的用户名填充第一行并立即回车并为密码执行类似的操作。

我试过下面的代码 connect.sh

sshpass -p <remote-passwd> ssh root@<remote-ip> 'bash -s' < test.sh

test.sh 包含

ls
pwd

如果我运行 connect.sh 脚本,它会完美执行而无需询问远程计算 secret 码。它还执行 lspwd

我真的可以用我的 usernamepassword 替换 lspwd 来实现我正在尝试的吗做什么??

另外,我是不是在寻找一些不可能的东西?? (我在 VB.NET 中看到了一个类似的代码,它解决了我的目的,但它不是一个健壮的代码,我真的对 VB 脚本没有任何想法)

Update: I'm able to login to remote machine non interactively, but the remote machine environment immediately asks for a local authentication which again requires keyboard interaction, I'm looking for achieving this authentication non interactively

最佳答案

如果可能的话,您应该在服务器上配置一个公钥,这样您就不必提供密码了。这样更安全,会更直接地解决你的问题。

您可能还想研究编排框架,而不是自己全部实现。如果你在做小事,Fabric是一个不错的选择。如果这看起来会变得更大,您应该查看类似 Ansible 的内容,它还可以额外处理系统配置和一百万个其他事情,但只需很少的设置即可开始。

关于linux - 如何使用 ssh 通过提供静态文本来执行与键盘交互的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919124/

相关文章:

linux - 程序参数中参数扩展的奇怪行为

linux - 从终端中的 bash 脚本内部调用函数

c++ - 如何使用 shell magic 使用 GNU etags 创建递归 etags?

git ps1 bash 未在 Lion 上加载

windows - 批处理文件: List Directory & File names to individual variables and display as selection menu

使用 `seq` 的 Bash 通配符模式

linux - 用于切换用户的 Bash 脚本

linux - 如何从文本文件添加用户并生成随 secret 码 Fedora

linux - 大多数 "vanila"linux 发行版

linux - 如何在shell中输出返回码?