Bash:控制 SSH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1924464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:29:09  来源:igfitidea点击:

Bash: controlling SSH

bashsshcontrols

提问by Deniz Zoeteman

I have this bash file, which asks for IP, password, etc. for OpenSSH to a device.

我有这个 bash 文件,它要求提供 IP、密码等,以便 OpenSSH 连接到设备。

Now, if i use ssh root@ip, i have to enter the password. This is really irritating. Secondly; i cannot let my script send commands to it.

现在,如果我使用ssh root@ip,我必须输入密码。这真的很烦人。其次; 我不能让我的脚本向它发送命令。

This is what i want->

这就是我想要的->

  1. Not the password thing; i already found something; but it tells me the commands are not found?:

    #!/usr/bin/expect -f

    spawn ssh root@$IPADDR

    expect "password:"

    sleep 1

    send "$rpass\r"

    1. I want the user to be able to see some menus where it can choose from; after that; every command is done for him. So like a new window, or something like that?

    2. I do not want to use: -Any external stuff -No extra editing of the SSH connection

  1. 不是密码的东西;我已经找到了一些东西;但它告诉我找不到命令?:

    #!/usr/bin/expect -f

    spawn ssh root@$IPADDR

    expect "password:"

    sleep 1

    send "$rpass\r"

    1. 我希望用户能够看到一些可供选择的菜单;在那之后; 每一条命令都是为他完成的。所以像一个新窗口,或者类似的东西?

    2. 我不想使用: - 任何外部的东西 - 没有额外的 SSH 连接编辑

BASH INFO: GNU Bash, v. 4.0.33(1)-release (i486-pc-linux-gnu), running on Linux Mint. But it got to be available for several linux distro's, and also on Mac?

BASH 信息:GNU Bash,v. 4.0.33(1)-release (i486-pc-linux-gnu),在 Linux Mint 上运行。但是它必须可用于几个 linux 发行版,也可以在 Mac 上使用?

采纳答案by bmargulies

Many tools to go great lengths to prevent what you are doing. I recommend using ssh public keys to solve this problem instead of passwords.

许多工具可以不遗余力地防止您正在做的事情。我建议使用 ssh 公钥来解决这个问题,而不是密码。

The big alternative is to write your own modified ssh client based on the open source so as to take control of the password management.

最大的选择是基于开源编写自己的修改过的 ssh 客户端,以便控制密码管理。

Oh, well, I forgot. You can probably outsmart this with a pty, since then /dev/tty will be what you control. expect might help you with this.

哦,好吧,我忘记了。您可能可以使用 pty 来超越它,因为 /dev/tty 将是您控制的对象。期望可能会帮助您解决这个问题。

回答by Chev

The proper way to do this without storing passwords in plaintext on your machine is with ssh. First run:

在您的机器上不以明文形式存储密码的情况下,正确的方法是使用 ssh。第一次运行:

ssh-keygen

ssh-keygen

This will generate a new SSH key in ~/.ssh/id_rsa.pub. After that simply run:

这将生成一个新的 SSH 密钥~/.ssh/id_rsa.pub。之后只需运行:

ssh-copy-id [email protected]

ssh-copy-id [email protected]

If you're on OS X or another machine that does not have "ssh-copy-id" there are one-line alternativessuch as this one:

如果您使用的是 OS X 或另一台没有“ssh-copy-id”的机器,则可以使用单行替代方法,例如:

cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

Ultimately you just need to append the contents of ~/.ssh/id_rsa.pubon your local machine to ~/.ssh/authorized_keyson the remote server. How you do that is up to you, the above are just quick shortcuts to do that.

最终,您只需~/.ssh/id_rsa.pub要将本地机器~/.ssh/authorized_keys上的内容附加到远程服务器上。你如何做到这一点取决于你,以上只是做到这一点的快捷方式。

回答by Paused until further notice.

Expectis the usual tool for automating interactive sessions.

Expect是自动化交互式会话的常用工具。

回答by nachoparker

The proper way to go is to copy the keys as has been said here. To add something to the conversation, there are cases where sshpasscan be handy.

正确的方法是按照此处所述复制密钥。要在对话中添加一些内容,在某些情况下sshpass会很方便。

The question asks specifically about scripting in a system with SSH. If it is the development of an embedded system, it can be useful to combine sshpasswith command line options, as it reads on this post

该问题专门询问有关在具有 SSH 的系统中编写脚本的问题。如果是嵌入式系统的开发,结合sshpass命令行选项会很有用,正如这篇文章中所读到的

sshpass -p raspberry ssh [email protected]

this can be combined with

这可以与

ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no [email protected]

to avoid confirmation questions that prevent scripting from happening.

以避免出现阻止脚本编写的确认问题。

Again, only use this in development systems where different machines share an IP and security is not important.

同样,仅在不同机器共享 IP 且安全性不重要的开发系统中使用它。

https://ownyourbits.com/2017/02/22/easy-passwordless-ssh-with-sshh/

https://ownyourbits.com/2017/02/22/easy-passwordless-ssh-with-sshh/

回答by Tom

Even if I would use pem keys for this and this is an old topic, I also wanted to quote sshpass

即使我会为此使用 pem 密钥并且这是一个老话题,我也想引用sshpass

回答by Alex Brasetvik

Have you considered Paramiko? It's a Python-library for interacting with SSH.

你考虑过Paramiko吗?它是一个用于与 SSH 交互的 Python 库。

回答by Nick Dixon

Use ssh-keygen to create a public key for your machine, then copy your local ~/.ssh/id_rsa.pubor ~/.ssh/identity.pubto the remote system, in ~/.ssh/authorized_keys.

使用 ssh-keygen 为您的机器创建一个公钥,然后将您的本地~/.ssh/id_rsa.pub~/.ssh/identity.pub远程系统复制到~/.ssh/authorized_keys.

You may need to tighten the permissions on the authorized_keys file: chmod 600

您可能需要收紧对authorized_keys 文件的权限:chmod 600

回答by Andrew Preizner

You can use this script: https://github.com/aprey10/ssh-authorizer

你可以使用这个脚本:https: //github.com/aprey10/ssh-authorizer

It also allows to skip ssh keys passphrase request.

它还允许跳过 ssh 密钥密码请求。