你能在.ssh / config中设置密码以允许自动登录吗?

我正在使用ubuntu 11.10。 我每天都使用ssh连接到很多服务器,所以我把它们的参数放在.ssh/config文件中; 像这样 :

 Host Home User netmoon Port 22 HostName test.com 

对于每个连接,有没有办法将密码放在此文件中? 因此,当服务器请求密码时,终端将其传递并发送到服务器。

这是因为有时候我会离开电脑,当我回去输入密码,然后按回车键,终端说CONNECTION CLOSED。

  • 我不想使用公钥/私钥对。

为方便起见,安全交易永远不会结束……

你可以使用openssh-client包中的ssh-copy-id吗?

来自man ssh-copy-id

 ssh-copy-id is a script that uses ssh to log into a remote machine and append the indicated identity file to that machine's ~/.ssh/authorized_keys file. 

如果您真的不想使用公钥/私钥对,可以编写一个expect脚本,根据目标地址自动输入密码。

编辑:我的意思是你可以有一个脚本,一方面使用expect为你输入密码,另一方面,从配置文件中读取给定用户和主机的密码。 例如,以下python脚本将适用于晴天场景:

 #!/usr/bin/python import argparse from ConfigParser import ConfigParser import pexpect def main(args): url = args.url user, host = url.split('@', 1) cfg_file = 'ssh.cfg' cfg = ConfigParser() cfg.read(cfg_file) passwd = cfg.get(user, host) child = pexpect.spawn('ssh {0}'.format(url)) child.expect('password:') child.sendline(passwd) child.interact() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Run ssh through pexpect') parser.add_argument('url') args = parser.parse_args() main(args) 

配置文件格式如下:

 [user_1] host1 = passwd_1 host2 = passwd_2 [user_2] host1 = passwd_1 host2 = passwd_2 

注意:如上所述,python脚本需要更加复杂,以处理来自ssh和所有可能URL的所有可能的错误和问题消息(在示例中,它假设它将类似于user@host ,但是用户大部分时间都不使用部分),但基本思路仍然是一样的。 注册配置文件,您可以使用不同的配置文件或使用.ssh/config并编写自己的代码来解析该文件并获取给定用户和主机的密码。

还有sshpass程序。 使用方法: sshpass -p MyPa55word ssh me@myservor.com

不,我害怕这是不可能的。

唯一真正的选择是使用私钥,但你说你不想(为什么不呢?)。

ProxyCommand怎么样:

 Host Home-raw HostName test.com Host Home User netmoon Port 22 ProxyCommand sshpass -pmypass ssh netmoon@%h-raw nc localhost %p 

您也可以使用ssh -W而不是nc

 ProxyCommand sshpass -pmypass ssh netmoon@%h-raw -W localhost:%p 

您可以在/ usr / local / bin中创建一个简单的ssh脚本替换:

 #!/bin/bash host=$1 password=`awk "/#Password/ && inhost { print \\\$2 } /Host/ { inhost=0 } /Host $host/ { inhost=1 }" ~/.ssh/config` if [[ -z "$password" ]]; then /usr/bin/ssh $* else sshpass -p $password /usr/bin/ssh $* fi 

然后在你的〜/ .ssh / config文件中你可以使用

 Host foohost User baruser #Password foobarpassword 

我使用VanDyke Software的一个名为SecureCRT的应用程序。

http://www.vandyke.com/products/securecrt/

它不是免费的,但价格非常合理。 我已经使用它多年(在Windows上运行,或使用Wine)进行远程访问,终端仿真和(分散的)网络管理。 他们最终在2011年初发布了本机Linux版本。

它支持复杂的登录设置(或脚本),存储的密码(或证书),标签式多个会话等。

在启动时,您可以从存储的远程(或本地)计算机的结构化列表(树视图)中选择哪个远程目标(和协议),或者只创建一个连接(然后存储)。

我发现它对具有高级身份validation,非标准端口或防火墙访问协商的远程站点特别有用。

如果您正在进行大量远程访问(主要角色的一部分),那么此应用程序将在使用的第一个月内certificate其费用。

@BrunoPereira 对此问题的回答显示了一种替代方法,可以在不明确输入密码和避免使用ssh密钥的情况下进行连接。

您可以在~/.bashrc创建脚本,别名或函数以快速执行该命令。

显然,这种方法应考虑安全性因素。

回答你问的问题,不能在ssh配置文件中配置默认​​密码。

但是,如果确实如此,正如你所说“ 这是因为有时候我会离开电脑,当我回去输入密码,然后按回车键,终端说CONNECTION CLOSED。 ”,那为什么不阻止关闭会话呢? SSH可以为您保持连接。

 Host Home User netmoon Port 22 HostName test.com ServerAliveInterval 300 ServerAliveCountMax 2 

以下是我对@ ArekBurdach答案的详细修改。 它提供以下扩展:

  • 主机可以在ssh命令行中的任何位置; 即它还支持ssh 语法
  • 没有硬编码ssh的路径
  • 更强大的ssh_config解析
  • 奖金: scp包装器也是

ssh-wrapper

 #!/bin/bash password=$(awk ' BEGIN { # Collect the SSH arguments as keys of a dictionary, so that we can easily # check for inclusion. for (i = 2; i < ARGC; i++) { sshArgs[ARGV[i]] = 1 } # Only process the first argument; all others are the command-line arguments # given to ssh. ARGC = 2 } $1 == "Password" && inhost { print $2 } /^\s*Host\s/ { if ($2 in sshArgs) inhost=1 else inhost=0 } ' ~/.ssh/config "$@") if [ "$password" ]; then sshpass -p "$password" "$(which ssh)" "$@" else "$(which ssh)" "$@" fi 

scp-wrapper

 #!/bin/bash password=$(awk ' BEGIN { # Collect the SCP arguments as keys of a dictionary, so that we can easily # check for inclusion. for (i = 2; i < ARGC; i++) { colonIdx = index(ARGV[i], ":") if (colonIdx > 0) { scpArgs[substr(ARGV[i], 1, colonIdx - 1)] = 1 } } # Only process the first argument; all others are the command-line arguments # given to scp. ARGC = 2 } $1 == "Password" && inhost { print $2 } /^\s*Host\s/ { if ($2 in scpArgs) inhost=1 else inhost=0 } ' ~/.ssh/config "$@") if [ "$password" ]; then sshpass -p "$password" "$(which scp)" "$@" else "$(which scp)" "$@" fi 

安装

~/.bashrc定义别名:

 alias ssh=ssh-wrapper alias scp=scp-wrapper 

组态

使用IgnoreUnknown指令, ssh不会抱怨新引入的Password指令,因此(与@ ArekBurdach的答案相反),我们可以将其显示为“真实”配置。 如果您不喜欢这样,将脚本更改回已注释掉的脚本是微不足道的。

 # Allow specifying passwords for Host entries, to be parsed by ssh-wrapper. IgnoreUnknown Password Host foohost User baruser Password foobarpassword 

如果您无法直接访问密钥对,则可以在本地计算机上加密密码。

除了@Eric Woodruff的ProxyCommand之外,这样做的方法是使用密钥加密密码 。

组合的方法是使用管道:

 openssl rsautl -decrypt -inkey /path/to/decrypt_key -in /path/to/encrypted_password | sshpass ssh real-destination -tt 

哪里

 Host real-destination Hostname test.com User netmoon 

感谢Arek的灵感……

而不是运行另一个shell进程,这只是在当前bash shell中运行的函数。 它运行单个awk来解析配置文件,并确定是否从shell变量或从明文写入的密码中获取密码到ssh配置文件中(由于我使用描述的问题而使用awk in eval而不是describe)。 我尝试使用ProxyCommand直接在ssh配置文件中使用sshpass的方法有很多,但似乎没有任何工作正常,除非我可以通过rsa登录到一个盒子但需要发送密码来打开我的加密目录。

 function ssh(){ host=$1; unset PASSWORD unset PASSVAR eval $(awk "/#Passvar / && inhost { printf \"PASSVAR=%s\",\$2; exit 1 } /#Password / && inhost { printf \"PASSWORD=%s\",\$2; } /^Host / && inhost { inhost=0; exit 1 } /^Host $host\$/ { inhost=1 }" ~/.ssh/config) if [[ -z "$PASSWORD" ]] && [[ -z "$PASSVAR" ]]; then /usr/bin/ssh -q $* 2>/dev/null else if [[ -n "$PASSVAR" ]]; then PASSWORD=$(TMP=${!PASSVAR-*};echo ${TMP##*-}) fi /usr/local/bin/sshpass -p"$PASSWORD" /usr/bin/ssh -q $* 2>/dev/null fi } 

然后~/.ssh/config部分如下所示:

 Host MyHostname Port 22 Hostname 2.1.2.2 User merrydan #Passvar MYPASS_ENVVAR #Password Some!Password 

如果配置部分中存在#Password覆盖#Password
$MYPASS_ENVVAR是保存密码的环境变量。

请享用!