bash 如何使用bash输入ssh密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16928004/
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
How to enter ssh password using bash?
提问by Prostak
Everyday I am connecting to a server through ssh. I go through this routine:
我每天都通过 ssh 连接到服务器。我经历了这个例程:
IC001:Desktop user$ ssh [email protected]
[email protected]'s password:
Last login: Tue Jun 4 10:09:01 2013 from 0.0.0.0
$
I would like to automate this process and create a bash script to do it for me. I don't care about security and okay to store my password openly in the script. I am also okay for it to get typed openly on the screen while the script gets executed. So I've created this:
我想自动化这个过程并创建一个 bash 脚本来为我做这件事。我不关心安全性,可以在脚本中公开存储我的密码。我也可以在脚本执行时在屏幕上公开输入它。所以我创建了这个:
#!/bin/bash
ssh [email protected]
echo mypassword
But it doesn't work. I've also tried send
instead of echo
, but it also didn't work. Please advise if it is possible to do.
但它不起作用。我也试过send
代替echo
,但它也没有奏效。请告知是否可行。
回答by michas
Double check if you are not able to use keys.
仔细检查您是否无法使用密钥。
Otherwise use expect:
否则使用期望:
#!/usr/bin/expect -f
spawn ssh [email protected]
expect "assword:"
send "mypassword\r"
interact
回答by michas
Create a new keypair: (go with the defaults)
创建一个新的密钥对:(使用默认值)
ssh-keygen
Copy the public key to the server: (password for the last time)
将公钥复制到服务器:(最后一次的密码)
ssh-copy-id [email protected]
From now on the server should recognize your key and not ask you for the password anymore:
从现在开始,服务器应该识别您的密钥并且不再要求您输入密码:
ssh [email protected]