Linux 用脚本自动输入SSH密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12202587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically enter SSH password with script
提问by user1467855
I need to create a script that automatically inputs a password to OpenSSH ssh
client.
我需要创建一个脚本来自动向 OpenSSHssh
客户端输入密码。
Let's say I need to SSH into myname@somehost
with the password a1234b
.
假设我需要myname@somehost
使用密码SSH 进入a1234b
。
I've already tried...
我已经试过了...
#~/bin/myssh.sh
ssh myname@somehost
a1234b
...but this does not work.
...但这不起作用。
How can I get this functionality into a script?
我怎样才能把这个功能放到脚本中?
回答by Diego Woitasen
Use public key authentication: https://help.ubuntu.com/community/SSH/OpenSSH/Keys
使用公钥认证:https: //help.ubuntu.com/community/SSH/OpenSSH/Keys
In the source host run this only once:
在源主机中只运行一次:
ssh-keygen -t rsa # ENTER to every field
ssh-copy-id myname@somehost
That's all, after that you'll be able to do ssh without password.
就是这样,之后您就可以在没有密码的情况下执行 ssh。
回答by Lipongo
You could use an expects script. I have not written one in quite some time but it should look like below. You will need to head the script with #!/usr/bin/expect
您可以使用期望脚本。我已经有一段时间没有写过一篇了,但它应该如下所示。你需要用#!/usr/bin/expect
#!/usr/bin/expect -f
spawn ssh HOSTNAME
expect "login:"
send "username\r"
expect "Password:"
send "password\r"
interact
回答by abbotto
First you need to install sshpass.
首先你需要安装sshpass。
- Ubuntu/Debian:
apt-get install sshpass
- Fedora/CentOS:
yum install sshpass
- Arch:
pacman -S sshpass
- Ubuntu/Debian:
apt-get install sshpass
- Fedora/CentOS:
yum install sshpass
- 拱:
pacman -S sshpass
Example:
例子:
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM
Custom port example:
自定义端口示例:
sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM:2400
Notes:
笔记:
sshpass
can also read a password from a file when the-f
flag is passed.- Using
-f
prevents the password from being visible if theps
command is executed. - The file that the password is stored in should have secure permissions.
- Using
sshpass
当-f
标志被传递时,还可以从文件中读取密码。-f
如果ps
执行命令,使用可防止密码可见。- 存储密码的文件应该具有安全权限。
回答by WT29
I got this working as follows
我按如下方式工作
.ssh/config was modified to eliminate the yes/no prompt - I'm behind a firewall so I'm not worried about spoofed ssh keys
.ssh/config 被修改以消除是/否提示 - 我在防火墙后面,所以我不担心欺骗的 ssh 密钥
host *
StrictHostKeyChecking no
Create a response file for expect i.e. answer.expect
为expect ie answer.expect 创建一个响应文件
set timeout 20
set node [lindex $argv 0]
spawn ssh root@node service hadoop-hdfs-datanode restart
expect "*?assword {
send "password\r" <- your password here.
interact
Create your bash script and just call expect in the file
创建你的 bash 脚本并在文件中调用 expect
#!/bin/bash
i=1
while [$i -lt 129] # a few nodes here
expect answer.expect hadoopslave$i
i=[$i + 1]
sleep 5
done
Gets 128 hadoop datanodes refreshed with new config - assuming you are using a NFS mount for the hadoop/conf files
使用新配置刷新 128 个 hadoop 数据节点 - 假设您对 hadoop/conf 文件使用 NFS 挂载
Hope this helps someone - I'm a Windows numpty and this took me about 5 hours to figure out!
希望这对某人有所帮助 - 我是一个 Windows numpty,这花了我大约 5 个小时才弄清楚!
回答by RemiZOffAlex
Variant I
变体 I
sshpass -p PASSWORD ssh USER@SERVER
Variant II
变体二
#!/usr/bin/expect -f
spawn ssh USERNAME@SERVER "touch /home/user/ssh_example"
expect "assword:"
send "PASSWORD\r"
interact
回答by damn_c
After looking for an answer for the question for months, I finally found a better solution: writing a simple script.
在为这个问题寻找了几个月的答案后,我终于找到了一个更好的解决方案:编写一个简单的脚本。
#!/usr/bin/expect
set timeout 20
set cmd [lrange $argv 1 end]
set password [lindex $argv 0]
eval spawn $cmd
expect "assword:"
send "$password\r";
interact
Put it to /usr/bin/exp
, then you can use:
把它放到/usr/bin/exp
,然后你可以使用:
exp <password> ssh <anything>
exp <password> scp <anysrc> <anydst>
exp <password> ssh <anything>
exp <password> scp <anysrc> <anydst>
Done!
完毕!
回答by Xoroz
I have a better solution that inclueds login with your account than changing to root user. It is a bash script
我有一个更好的解决方案,包括使用您的帐户登录,而不是更改为 root 用户。这是一个bash脚本
http://felipeferreira.net/index.php/2011/09/ssh-automatic-login/
http://felipeferreira.net/index.php/2011/09/ssh-automatic-login/
回答by RmccurdyDOTcom
# 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 [email protected] -p 22
参考:https: //www.linkedin.com/pulse/youre-doing-wrong-ssh-plain-text-credentials-robert-mccurdy?trk=mp-reader-card
回答by MustSeeMelons
The answer of @abbotto did not work for me, had to do some things differently:
@abbotto 的答案对我不起作用,必须做一些不同的事情:
- yum install sshpass changed to - rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/sshpass-1.05-1.el6.x86_64.rpm
- the command to use sshpass changed to - sshpass -p "pass" ssh user@mysite -p 2122
- yum install sshpass 改为 - rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/sshpass-1.05-1.el6.x86_64.rpm
- 使用 sshpass 的命令更改为 - sshpass -p "pass" ssh user@mysite -p 2122
回答by shyam
To connect remote machine through shell scripts , use below command:
要通过 shell 脚本连接远程机器,请使用以下命令:
sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no USERNAME@IPADDRESS
where IPADDRESS
, USERNAME
and PASSWORD
are input values which need to provide in script, or if we want to provide in runtime use "read" command.
其中IPADDRESS
,USERNAME
和PASSWORD
是需要在脚本中提供的输入值,或者如果我们想在运行时提供,请使用“读取”命令。