Eon*_*nil 592 automation ssh password
如何使用密码自动进行SSH登录?我正在配置我的测试虚拟机,所以没有考虑高安全性。选择 SSH 以获得可接受的安全性,且配置最少。
前任)
echo password | ssh id@server
Run Code Online (Sandbox Code Playgroud)
这不起作用。
我记得我是用一些技巧来做到这一点的,有人指导我,但我现在不记得我使用的技巧了......
wee*_*ens 752
$ sudo apt-get install sshpass
$ sshpass -p your_password ssh user@hostname
Run Code Online (Sandbox Code Playgroud)
Cak*_*mox 518
不要使用密码。生成无密码 SSH 密钥并将其推送到您的 VM。
如果你已经有一个 SSH 密钥,你可以跳过这一步……只需点击Enter密钥和两个密码:
$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
Run Code Online (Sandbox Code Playgroud)
将您的密钥复制到目标服务器:
$ ssh-copy-id id@server
id@server's password:
Run Code Online (Sandbox Code Playgroud)
现在尝试登录机器,使用ssh 'id@server'
,并签入:
.ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)
以确保我们没有添加您不期望的额外密钥。
最后检查登录...
$ ssh id@server
id@server:~$
Run Code Online (Sandbox Code Playgroud)
ssh-agent
如果您想尝试使用密码保护您的密钥,您可能还想考虑使用。
lza*_*zap 99
虽然您的问题的正确答案是sshpass(有关详细信息,请参阅其他答案),但还有一种更安全的方法 - SSH 密钥。您离解决方案只有三个简单的步骤:
以下所有命令都在客户端运行,即您的机器
输入以下命令开始生成rsa 密钥对:
# ssh-keygen
Run Code Online (Sandbox Code Playgroud)
当出现“输入要保存密钥的文件”消息时,只需按 Enter 将文件名留空即可。
当终端要求您输入密码时,也只需将此留空并按 Enter。
然后使用一个简单的命令将密钥对复制到服务器上:
# ssh-copy-id userid@hostname
Run Code Online (Sandbox Code Playgroud)
您现在无需密码即可登录:
# ssh userid@hostname
Run Code Online (Sandbox Code Playgroud)
oos*_*hro 54
使用期望:
#!/usr/bin/expect -f
# ./ssh.exp password 192.168.1.11 id
set pass [lrange $argv 0 0]
set server [lrange $argv 1 1]
set name [lrange $argv 2 2]
spawn ssh $name@$server
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"
interact
Run Code Online (Sandbox Code Playgroud)
例子:
# ./1.ex password localhost ooshro
spawn ssh ooshro@localhost
ooshro@localhost's password:
Linux ubuntu-1010-server-01 2.6.35-25-generic-pae #44-Ubuntu SMP Fri Jan 21 19:01:46 UTC 2011 i686 GNU/Linux
Ubuntu 10.10
Welcome to Ubuntu!
* Documentation: https://help.ubuntu.com/
Last login: Tue Mar 1 12:41:12 2011 from localhost
Run Code Online (Sandbox Code Playgroud)
nro*_*ans 38
SSH 单点登录通常是通过公钥身份验证和身份验证代理来实现的。您可以轻松地将测试 VM 密钥添加到现有的身份验证代理(请参见下面的示例)。其他方法如 gssapi/kerberos 也存在,但更复杂。
在password
唯一可用的身份验证方法的情况下,sshpass可用于自动输入密码。请特别注意手册页的安全考虑部分。在所有三个选项中,密码在某些时候是可见的或以纯文本形式存储的:
# Create a pipe
PIPE=$(mktemp -u)
mkfifo -m 600 $PIPE
# Attach it to file descriptior 3
exec 3<>$PIPE
# Delete the directory entry
rm $PIPE
# Write your password in the pipe
echo 'my_secret_password' >&3
# Connect with sshpass -d
sshpass -d3 ssh user@host
# Close the pipe when done
exec 3>&-
Run Code Online (Sandbox Code Playgroud)
它在 bash 中非常麻烦,可以说使用编程语言更容易。在写入密码之前,另一个进程可能会附加到您的管道/fd。机会之窗很短,仅限于您的进程或根。
# Set your password in an environment variable
export SSHPASS='my_secret_password'
# Connect with sshpass -e
sshpass -e ssh user@host
Run Code Online (Sandbox Code Playgroud)
您和 root 可以在 sshpass 运行 ( cat /proc/<pid>/environ | tr '\0' '\n' | grep ^SSHPASS=
) 时读取您进程的环境变量(即您的密码)。机会之窗更长,但仍仅限于您自己的进程或 root,而不是其他用户。
sshpass -p my_secret_password ssh user@host
Run Code Online (Sandbox Code Playgroud)
如手册页中所述,这很方便,但安全性较低。命令行参数对所有用户都是可见的(例如ps -ef | grep sshpass
)。sshpass 试图隐藏参数,但仍然有一个窗口,在此期间所有用户都可以看到您通过参数传递的密码。
将 bash HISTCONTROL 变量设置为ignorespace
orignoreboth
并在敏感命令前加上空格。他们不会被载入史册。
# Generate a key pair
# Do NOT leave the passphrase empty
ssh-keygen
# Copy it to the remote host (added to .ssh/authorized_keys)
ssh-copy-id user@host
Run Code Online (Sandbox Code Playgroud)
密码非常重要。任何以某种方式获得私钥文件的人都无法在没有密码的情况下使用它。
# Start the agent
eval `ssh-agent`
# Add the identity (private key) to the agent
ssh-add /path/to/private-key
# Enter key passphrase (one time only, while the agent is running)
Run Code Online (Sandbox Code Playgroud)
像往常一样连接
ssh user@host
Run Code Online (Sandbox Code Playgroud)
优点是你的私钥是加密的,你只需要输入一次密码(也可以通过更安全的输入方法)。
ead*_*ter 31
我很惊讶没有人plink
从putty-tools
Ubuntu的软件包中提到:
plink user@domain -pw mypass [cmd]
Run Code Online (Sandbox Code Playgroud)
它也可以在 Windows 上使用,并且语法大部分与 openssh 客户端兼容。
Jam*_*s L 19
这可能对您没有任何用处,但您可以使用 Perl 来完成:
\#!/usr/bin/perl
use warnings;
use strict;
use Net::SSH::Perl;
my $host = 'remote.serv.er';
my $user = 'root';
my $pass = 'hunter2';
my $ssh = Net::SSH::Perl->new('$host');
$ssh->login('$user', '$pass') or die "Oh noes! $!";
Run Code Online (Sandbox Code Playgroud)
确定您不想使用 SSH 密钥而不是密码吗?这样它既安全又自动。
我更喜欢passh
https://github.com/clarkwang/passh
sshpass 被设计破坏了。
当我的 ssh 服务器尚未添加时known_hosts
,sshpass
不会向我显示将服务器添加到我已知主机的消息,passh
没有这个问题。
登录到远程服务器:
$ passh -p password ssh user@host
Run Code Online (Sandbox Code Playgroud)
根据您的自动化需求,Ansible 可能适合您。它可以很好地管理诸如提示输入密码、提示输入 sudo 密码、更改使用的各种方式、安全地使用加密的机密 (Vault) 等内容。
如果这不合适,我会建议使用 Expect,正如另一个答案中所建议的那样。