bash脚本SSH到一台机器,而不会提示密码,而无需使用钥匙 [英] Bash Script to SSH into a machine without prompting password and without using keys
问题描述
我意识到这个问题已经被问了几次,但我不能在我的搜索地方找到相关的答案。
我在开发环境中工作,其中安全性是不是一个问题,任何人都可能只是猜测密码,如果想了几秒钟。
我所试图做的是简单的。我在本地.bashrc文件创建一个别名功能,我想这个功能自动登录到一台具有默认密码。
我目前的实施看起来是这样的:
函数S(){
SSH root@192.168.1.$1
}
当我运行它,我得到的东西是这样的:
〜]第122条SSH root@192.168.1.122root@192.168.1.122's密码:
使用bash,而不是使用RSA密钥我希望得到这个使用默认密码密码。
我试过其中,IP和用户已设置如下。
不要= $(预计-c
产卵的ssh $用户@ $ {IP [0]}。$ {IP [1]}。$ {IP [2]}。$ {IP [3]}
预计\\是/否\\
发送\\是\\ r \\
预计\\assword \\发送\\密码\\)
回声$待办事项
$待办事项
它给人的follwing错误:
连接和使用期望登录到服务器
用法:发[参数]字符串
在执行
发送
从内部调用
期望assword发送密码
Administrator@192.168.1.176's密码:
庆典:产卵:未找到命令...
使用下面的命令,我可以连接一台机器。如果删除了互动它只运行uptime命令并关闭连接。随着交互命令我无法看到我打字或实际与机器交互。任何想法?
不要= $(预计-c重生SSH用户$ @ $ {IP [0]} $ {IP [1]} $ {IP [2]} $ {IP [3]};设置超时4;预计\\assword \\发送\\密码\\ n \\;预计\\测试\\发送\\正常运行时间\\ n \\;互动;);回声$做;
您可以使用期望
工具做到这一点:的 http://expect.sourceforge.net/
这是广泛使用的,所以要根据您的系统,相当于命令和apt-get安装期望
或百胜安装期望
将安装它。
下面是使用ssh的期望
脚本的例子。这将记录你并给你的提示交互控制:
#!的/ usr / bin中/预期组登录根
设置地址127.0.0.1
设置私服密码产卵的ssh登录$ @ $地址
预计$登录@ $地址\\的密码:
发送$ PW \\ R
预计#
发送CD /开发商\\ R
相互作用
下面是如何使用期望
作为一个bash脚本的一部分的例子。此登录用SSH,CD到/ var,运行一个脚本,然后退出SSH会话。
#!/斌/庆典
...login_via_ssh_and_do_stuff(){ #建立在bash的expect脚本 expect_sh = $(预计-c
产卵SSH root@127.0.0.1
预计\\密码:\\
发送\\密码\\ r \\
预计\\#\\
发送\\CD的/ var \\ r \\
预计\\#\\
发送\\搭配chmod + X my_script.sh \\ r \\
预计\\#\\
发送\\./ my_script.sh \\ r \\
预计\\#\\
发送\\退出\\ r \\
) #运行expect脚本
回声$ expect_sh
}
您可以留下一个脚本这些片段在本地系统上,然后只需别名脚本。
另外:我知道你说的安全性是不是一个问题,但我想只要注意,再次,认为正确的方法,而无需使用密码是使用SSH密钥对=)的ssh
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.
My current implementation looks something like this:
function s () {
ssh root@192.168.1.$1
}
When I run it I get something like this:
~]s 122
ssh root@192.168.1.122
root@192.168.1.122's password:
Using Bash, and not using RSA keys I would like to get this to use the default password 'password'.
I've tried the following where IP and User have already been set.
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""
Administrator@192.168.1.176'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;
You can do this with the expect
tool: 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.
Here's an example of an expect
script with ssh. This logs you in and gives you control of the interactive prompt:
#!/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.
#!/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\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 =)
这篇关于bash脚本SSH到一台机器,而不会提示密码,而无需使用钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!