我需要创建一个自动向OpenSSH ssh客户端输入密码的脚本。

假设我需要使用密码a1234b SSH到myname@somehost。

我已经试过了……

#~/bin/myssh.sh
ssh myname@somehost
a1234b

...但这并不奏效。

如何将此功能放入脚本中?


当前回答

这基本上是abbotto的答案的扩展,有一些额外的步骤(针对初学者),使启动你的服务器,从你的linux主机,非常容易:

编写一个简单的bash脚本,例如:

#!/bin/bash

sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no <YOUR_USERNAME>@<SEVER_IP>

保存文件,例如'startMyServer',然后在你的终端上运行这个命令使文件可执行:

sudo chmod +x startMyServer

将文件移动到“PATH”变量中的文件夹(在终端中运行“echo $PATH”查看这些文件夹)。例如,将它移动到'/usr/bin/'。

瞧,现在你可以通过在终端中输入'startMyServer'进入服务器。

P.S.(1)这不是很安全,请查看ssh密钥以获得更好的安全性。

附注(2)SMshrimant的答案很相似,对一些人来说可能更优雅。但我个人更喜欢使用bash脚本。

其他回答

通过shell脚本连接远程机器,使用以下命令:

sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no USERNAME@IPADDRESS

其中IPADDRESS, USERNAME和PASSWORD是需要在脚本中提供的输入值,或者如果我们想在运行时提供,使用“read”命令。

安全性更高的Sshpass

我在寻找ssh进入瘫痪服务器的方法时偶然发现了这个线程——处理ssh连接尝试花了一分钟多,在我输入密码之前超时了。在本例中,我希望能够在提示可用时立即提供我的密码。

(如果还不清楚的话:服务器处于这种状态,再设置公钥登录就太迟了。)

Sshpass来救援。但是,有比sshpass -p更好的方法。

我的实现直接跳到交互式密码提示(不会浪费时间查看是否可以进行公钥交换),并且从不以纯文本的形式显示密码。

#!/bin/sh
# preempt-ssh.sh
# usage: same arguments that you'd pass to ssh normally
echo "You're going to run (with our additions) ssh $@"

# Read password interactively and save it to the environment
read -s -p "Password to use: " SSHPASS 
export SSHPASS

# have sshpass load the password from the environment, and skip public key auth
# all other args come directly from the input
sshpass -e ssh -o PreferredAuthentications=keyboard-interactive -o PubkeyAuthentication=no "$@"

# clear the exported variable containing the password
unset SSHPASS
# create a file that echo's out your password .. you may need to get crazy with escape chars or for extra credit put ASCII in your password...
echo "echo YerPasswordhere" > /tmp/1
chmod 777 /tmp/1

# sets some vars for ssh to play nice with something to do with GUI but here we are using it to pass creds.
export SSH_ASKPASS="/tmp/1"
export DISPLAY=YOURDOINGITWRONG
setsid ssh root@owned.com -p 22

参考:https://www.linkedin.com/pulse/youre-doing-wrong-ssh-plain-text-credentials-robert-mccurdy?trk=mp-reader-card

我有一个更好的解决方案,包括登录您的帐户,而不是更改为根用户。 它是一个bash脚本

http://felipeferreira.net/index.php/2011/09/ssh-automatic-login/

Solution1:使用sshpass

#~/bin/myssh.sh
sshpass -p a1234b ssh myname@somehost

您可以通过

# Ubuntu/Debian
$ sudo apt-get install sshpass

# Red Hat/Fedora/CentOS
$ sudo yum install sshpass

# Arch Linux
$ sudo pacman -S sshpass

#OS X
brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

或从这里下载源代码

tar xvzf sshpass-1.08.tar.gz 
cd sshpass-1.08.tar.gz 
./configure
sudo make install

解决方案2:设置SSH无密码登录

假设您需要使用密码2b2b2b从aaa@1.1.1.1(客户端服务器A) SSH到bbb@2.2.2.2(远程服务器B)。

在A中使用如下命令生成公钥(.ssh/id_rsa.pub)和私钥(.ssh/id_rsa)

ssh-keygen -t rsa
[Press enter key]
[Press enter key]
[Press enter key]

使用以下命令将生成的公钥(.ssh/id_rsa.pub)以文件名authorized_keys分发到bbb的.ssh目录下的服务器B

ssh-copy-id bbb@2.2.2.2

第一次ssh登录需要输入密码,以后会自动登录,不需要再次输入!

ssh bbb@2.2.2.2 [Enter]
2b2b2b

然后你的剧本就可以

#~/bin/myssh.sh
ssh myname@somehost