我知道这个问题已经被问过几次了,但是我在搜索中找不到任何相关的答案。
我在一个开发环境中工作,在这个环境中安全性不是问题,只要思考几秒钟,任何人都可以猜测密码。
我想做的很简单。我已经在本地.bashrc文件中创建了一个别名函数,我希望该函数自动使用默认密码登录到计算机。
我当前的实现看起来像这样:
function s () {
ssh root@192.168.1.$1
}
当我运行它时,我得到的是这样的:
~]s 122
ssh root@192.168.1.122
root@192.168.1.122's password:
使用Bash,而不使用RSA密钥,我想使用默认的密码" password"。
我已经尝试了以下已经设置IP和用户的地方。
Do=$(expect -c"
spawn ssh $User@${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]}
expect "yes/no"
send "yes
"
expect "assword" send "password"")
echo $Do
$Do
它给出了以下错误:
Connecting and logging into server using expect
usage: send [args] string
while executing
"send"
invoked from within
"expect"assword" send"password""
Administrator@192.168.1.176's password:
bash: spawn: command not found...
使用以下命令,我可以连接机器。如果删除交互,它将运行uptime命令并关闭连接。使用interact命令,我看不到我正在键入的内容或与机器实际交互的内容。有任何想法吗?
Do=$(expect -c"spawn ssh $User@${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]}; set timeout 4; expect "assword"; send "password
"; expect "test"; send "uptime
"; interact;");echo $Do;
spawn命令是否设置了环境?如果没有,那么将不会设置路径,并且它将不知道在哪里寻找ssh。尝试将完整路径放入可执行文件。
只是想知道,您对RSA密钥的厌恶是什么?设置新系统时,我总是做的第一件事就是授权RSA密钥。
我不想使用RSA密钥,因为我正在使用许多不同的服务器,这些服务器的生命周期非常有限,因此该脚本将被许多人使用。其想法是通过不必键入密码来节省时间。每次都使用相同的密码。如果每次我第一次访问新计算机以ssh-copy-id标识其上的密钥时,都必须输入密码,那么这将达到目的。
@Jay您是在说期望spawn命令不会知道如何处理ssh,而是应该使用/ bin / ssh?
@SuperTetelman,为什么不将RSA密钥与脚本一起发送并让脚本使用ssh -i?
@SuperTetelman是的,它很简单,可以尝试。 Cron脚本有这个问题。
密钥是一个更好的解决方案。首先如何设置这些使用寿命有限的服务器?无论您使用什么来获取root密码集,都可以轻松地安装authorized_keys文件。
您可以使用expect工具执行此操作:http://expect.sourceforge.net/
它广泛可用,因此根据您的系统,将安装sudo apt-get install expect或yum install expect等效项。
这是带有ssh的expect脚本的示例。 这将使您登录并控制交互式提示:
#!/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
"
expect"#"
send"cd /developer
"
interact
这是一个如何在bash脚本中使用expect的示例。 使用ssh登录,使用cd登录到/ var,运行脚本,然后退出ssh会话。
#!/bin/bash
...
login_via_ssh_and_do_stuff() {
# build the expect script in bash
expect_sh=$(expect -c"
spawn ssh root@127.0.0.1
expect "password:"
send "password
"
expect "#"
send "cd /var
"
expect "#"
send "chmod +x my_script.sh
"
expect "#"
send "./my_script.sh
"
expect "#"
send "exit
"
")
# run the expect script
echo"$expect_sh"
}
您可以将这些片段保留在本地系统的脚本中,然后仅是脚本的别名。
另外:我知道您说安全性不是问题,但我想再次指出,不使用密码的"正确" ssh方式是使用ssh key-pair =)
@凯文:负面:pantz.org/software/expect/expect_examples_and_tips.html
我已经为期望脚本使用了 n和 r。 是否推荐其中一个?
使用主要Linux-es的软件包存储库中提供的sshpass。
例如,当密码在password.txt文件中时:
sshpass -fpassword.txt ssh username@hostname
sshpass runs ssh in a dedicated tty, fooling it into thinking it is
getting the password from an interactive user.