Linux 如何编写脚本以使用我的凭据连接到 ssh 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19671954/
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
How can I write a script to connect to an ssh server with my credentials?
提问by
I want to automate password entry when using SSH to log on to a remote linux server.
我想在使用 SSH 登录远程 linux 服务器时自动输入密码。
I made an executable bash script with
我制作了一个可执行的 bash 脚本
#! /bin/bash
ssh [email protected]
But is there a way to automate my password entry?
但是有没有办法自动输入密码?
回答by Xu Ding
ssh-keygen # generate a pair of RSA keys
scp ~/.ssh/id_rsa.pub [email protected]:id_rsa.tmp # copy the public key to remote host
ssh [email protected] # ssh to remote server, this time it requires password
cat id_rsa.tmp >> .ssh/authorized_keys # add the public key to authorized_keys
Then you could log off and log on without passwords.
然后,您可以注销并在没有密码的情况下登录。
回答by Bernard
Install sshpass
under Debian / Ubuntu Linux
sshpass
在 Debian / Ubuntu Linux 下安装
Type the following command:
$ sudo apt-get install sshpass
输入以下命令:
$ sudo apt-get install sshpass
How do I use sshpass?
我如何使用 sshpass?
Login to ssh server called server.example.com
with password called t@uyM59bQ
:
登录到server.example.com
使用密码调用的ssh 服务器t@uyM59bQ
:
$ sshpass -p 't@uyM59bQ' ssh [email protected]
$ sshpass -p 't@uyM59bQ' ssh [email protected]
Under shell script you may need to disable host key checking:
在 shell 脚本下,您可能需要禁用主机密钥检查:
$ sshpass -p 't@uyM59bQ' ssh -o StrictHostKeyChecking=no [email protected]
$ sshpass -p 't@uyM59bQ' ssh -o StrictHostKeyChecking=no [email protected]
How do I backup /var/www/html using rsync?
如何使用 rsync 备份 /var/www/html?
Run rsync over SSH using password authentication, passing the password on the command line:
使用密码身份验证通过 SSH 运行 rsync,在命令行上传递密码:
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/
回答by Alexander L. Belikoff
回答by cashman04
I have looked in to this extensively, and this is why I decided to learn python. The short answer is "No". There is no way with standard bash to pass a password to ssh prompt. There are some things you can install like expect or sshpass which can give you this functionality, but using those depends on if you have free roam on the server you are running the script. If you have ever wanted to learn python, this is a good one to learn on. There is a python module called "Fabric" which has this functionality built in, and is easy to use.
我已经对此进行了广泛的研究,这就是我决定学习 Python 的原因。最简洁的答案是不”。标准 bash 无法将密码传递给 ssh 提示符。您可以安装一些东西,例如 expect 或 sshpass,它们可以为您提供此功能,但是使用这些取决于您在运行脚本的服务器上是否可以自由漫游。如果你曾经想学习 Python,这是一个很好的学习方法。有一个名为“Fabric”的 Python 模块,它内置了此功能,并且易于使用。
Sorry this isn't a real answer, just thought I would pass on my advice, since I have been exactly where you are....
对不起,这不是一个真正的答案,只是想我会传递我的建议,因为我一直在你所在的地方......
回答by James
Expect is the tool of choice for any situation where you want to automate man-machine interaction.
对于您想要自动化人机交互的任何情况,Expect 都是首选工具。
Check it out... it is very easy to learn (at least for simple tasks like the one you need to tackle), and it is not uncommon to find it already installed in many a server.
检查一下……它很容易学习(至少对于像您需要处理的任务这样的简单任务),并且发现它已经安装在许多服务器中的情况并不少见。
Just thought I would pass on my view on this... good luck!
只是想我会传递我对此的看法......祝你好运!