无需提示密码和使用密钥即可通过 SSH 连接到机器的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13298487/
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
Bash Script to SSH into a machine without prompting password and without using keys
提问by SuperTetelman
I realize this question has been asked a few times but I could not find a relevant answer anywhere in my searching.
我意识到这个问题已经被问过几次了,但我在搜索中找不到相关的答案。
I am working in a development environment where security is not an issue and anyone could just guess the password if the thought for a few seconds.
我在一个安全性不是问题的开发环境中工作,如果想几秒钟,任何人都可以猜测密码。
What I am trying to do is simple. I have created an alias function in my local .bashrc file and I would like this function to automatically log into a machine with a default password.
我想要做的很简单。我在我的本地 .bashrc 文件中创建了一个别名函数,我希望这个函数能够使用默认密码自动登录到机器。
My current implementation looks something like this:
我当前的实现看起来像这样:
function s () {
ssh [email protected].
}
When I run it I get something like this:
当我运行它时,我得到这样的东西:
~]s 122
ssh [email protected]
[email protected]'s password:
Using Bash, and not using RSA keys I would like to get this to use the default password 'password'.
使用 Bash,而不是使用 RSA 密钥,我想让它使用默认密码“密码”。
I've tried the following where IP and User have already been set.
我已经尝试了以下 IP 和用户已经设置。
Do=$(expect -c "
spawn ssh $User@${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]}
expect \"yes/no\"
send \"yes\r\"
expect \"assword\" send \"password\"")
echo $Do
$Do
It gives the follwing error:
它给出了以下错误:
Connecting and logging into server using expect
usage: send [args] string
while executing
"send"
invoked from within
"expect "assword" send "password""
[email protected]'s password:
bash: spawn: command not found...
Using the following command I am able to connect a machine. If I remove the interact it just runs the uptime command and closes the connection. With the interact command I am unable to see what I am typing or actually interact with the machine. Any ideas?
使用以下命令,我可以连接一台机器。如果我删除交互,它只会运行正常运行时间命令并关闭连接。使用交互命令,我无法看到我正在输入的内容或实际与机器交互的内容。有任何想法吗?
Do=$(expect -c "spawn ssh $User@${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]}; set timeout 4; expect \"assword\"; send \"password\n\"; expect \"test\"; send \"uptime\n\"; interact;");echo $Do;
回答by sampson-chen
You can do this with the expect
tool: http://expect.sourceforge.net/
您可以使用该expect
工具执行此操作:http: //expect.sourceforge.net/
It's widely available, so depending on your system, the equivalent of sudo apt-get install expect
or yum install expect
will install it.
它是广泛可用的,因此根据您的系统,相当于sudo apt-get install expect
或yum install expect
将安装它。
Here's an example of an expect
script with ssh. This logs you in and gives you control of the interactive prompt:
这是一个expect
带有 ssh的脚本示例。这让您登录并让您控制交互式提示:
#!/usr/bin/expect
set login "root"
set addr "127.0.0.1"
set pw "password"
spawn ssh $login@$addr
expect "$login@$addr\'s password:"
send "$pw\r"
expect "#"
send "cd /developer\r"
interact
Here's an example of how to use expect
as part of a bash script. This logs in with ssh, cd to /var, runs a script, then exits the ssh session.
下面是一个示例,说明如何将其expect
用作 bash 脚本的一部分。这将使用 ssh 登录,cd 到 /var,运行脚本,然后退出 ssh 会话。
#!/bin/bash
...
login_via_ssh_and_do_stuff() {
# build the expect script in bash
expect_sh=$(expect -c "
spawn ssh [email protected]
expect \"password:\"
send \"password\r\"
expect \"#\"
send \"cd /var\r\"
expect \"#\"
send \"chmod +x my_script.sh\r\"
expect \"#\"
send \"./my_script.sh\r\"
expect \"#\"
send \"exit\r\"
")
# run the expect script
echo "$expect_sh"
}
You can leave these snippets in a script on your local system, and then just alias to the scripts.
您可以将这些片段保留在本地系统上的脚本中,然后只是脚本的别名。
Also: I know you said security isn't an issue, but I'd like to just note, again, that the "proper" way to ssh without using a password is to use a ssh key-pair =)
另外:我知道您说安全性不是问题,但我想再次说明,不使用密码进行 ssh 的“正确”方法是使用 ssh 密钥对 =)
回答by uvsmtid
Use sshpass
which is available in package repositories on major Linux-es.
使用sshpass
主要 Linux-es 上的软件包存储库中可用的。
For example, when password is in password.txt
file:
例如,当密码在password.txt
文件中时:
sshpass -fpassword.txt ssh username@hostname
sshpass
runsssh
in a dedicatedtty
, fooling it into thinking it is getting the password from an interactive user.
sshpass
运行ssh
在一个专用的tty
,愚弄它认为它是从交互式用户那里获取密码。