bash 是否可以使用密码自动进行 ssh 登录(不是无密码的 ssh)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12236894/
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
Is it possible to automate ssh login WITH passwd (not passphraseless ssh)
提问by Richard
Regardless of security issues, I want to automate ssh login by putting password into a script file (in form of plaintext). For example, I tried following, but without success...
不管安全问题,我想通过将密码放入脚本文件(以明文形式)来自动登录 ssh。例如,我尝试跟随,但没有成功......
echo "mypassword" | ssh -X root@remote_node_address
回声“我的密码”| ssh -X root@remote_node_address
it still prompt with password inputs...
它仍然提示输入密码...
Edit: I am aware of setting up passphraseless ssh (and actually have done this). What my question really is is how to automate process of setting up passphraseless ssh...
编辑:我知道设置无密码 ssh(实际上已经这样做了)。我真正的问题是如何自动化设置无密码 ssh 的过程...
回答by Todd A. Jacobs
Automate with Expect
使用 Expect 自动化
You can use Expectto drive password authentication with SSH. For example:
您可以使用Expect来通过 SSH 来驱动密码身份验证。例如:
#!/usr/bin/expect -f
set timeout -1
spawn ssh -o PubkeyAuthentication=no host.example.com
expect -exact "Password: "
send -- "secret\r"
expect {$\s*} { interact }
This script is a very basic example, and not especially robust in the face of failure or when running under a non-standard remote TERMlike GNU screen, but it works for the common case. You can also use /usr/bin/autoexpectfrom the expect-devpackage to generate your own custom scripts based on a manual session.
此脚本是一个非常基本的示例,在遇到故障或在非标准远程TERM(如 GNU 屏幕)下运行时并不是特别健壮,但它适用于常见情况。您还可以使用expect-dev包中的/usr/bin/autoexpect来基于手动会话生成您自己的自定义脚本。
回答by jdevelop
you will need to use public key authentication, see
您将需要使用公钥身份验证,请参阅
http://www.ece.uci.edu/~chou/ssh-key.html
http://www.ece.uci.edu/~chou/ssh-key.html
in order to add new keys for existing hosts, you will need to automate updating of public keys in ~/.ssh/authorized_keys on remote machine
为了为现有主机添加新密钥,您需要在远程机器上的 ~/.ssh/authorized_keys 中自动更新公钥
it is easy to do with
这很容易做到
ssh-keygen -t rsa -b 1024 -f ~/.ssh/new-key -P ""
cat ~/.ssh/new-key.pub | ssh root@target-host 'cat >> ~/.ssh/authorized_keys'
then you can use new key to access host with
然后您可以使用新密钥访问主机
ssh -i ~/.ssh/new-key root@remote-host
回答by Sulisu
I run into emptyrecently. I am surprised that it seems not to be well known since it is rarely talked about when problems like "how to automate ssh" arise.
我最近遇到空的。我很惊讶它似乎并不为人所知,因为在出现“如何自动化 ssh”之类的问题时很少有人谈论它。
I use it on openwrt, it has a package about 7KB in size without dependency, while tcl package is around 440KB. And you can use it in shell directly.
我在 openwrt 上使用它,它有一个大约 7KB 的包,没有依赖项,而 tcl 包大约是 440KB。你可以直接在shell中使用它。
"empty is an utility that provides an interface to execute and/or interact with processes under pseudo-terminal sessions (PTYs). This tool is definitely useful in programming of shell scripts designed to communicate with interactive programs like telnet, ssh, ftp, etc. In some cases empty can be the simplest replacement for TCL/expect or other similar programming tools "
“empty 是一个实用程序,它提供了一个接口来执行和/或与伪终端会话 (PTYs) 下的进程交互。这个工具在 shell 脚本的编程中绝对有用,这些脚本旨在与 telnet、ssh、ftp 等交互式程序进行通信. 在某些情况下,empty 可以是 TCL/expect 或其他类似编程工具的最简单替代品”
For example:
例如:
#!/bin/sh
empty -f -i in -o out telnet foo.bar.com
empty -w -i out -o in "ogin:" "luser\n"
empty -w -i out -o in "assword:" "TopSecret\n"
empty -s -o in "who am i\n"
empty -s -o in "exit\n"

