使用ssh连接到服务器的Bash脚本

时间:2015-09-28 09:18:44

标签: bash ssh

我正在尝试编写一个允许我使用ssh连接登录的bash脚本。 条件是密码存储在.txt文件中。

我需要编写一个自动请求连接到

的bash脚本
setEnabled(false)

并自动使用存储在.txt文件中的密码

我从未在bash中写过一些东西。有人能帮我吗。 提前感谢您的时间。

2 个答案:

答案 0 :(得分:2)

您无法使用普通bash脚本执行此操作。您需要使用expect命令。

将密码存储在文本文件中真的是一个非常糟糕的主意,以下是使用expect执行此操作的方法:

#!/usr/bin/expect

set timeout 20
set f [open "password.txt"]
set password [read $f]
close $f

spawn ssh user@host
expect "user@host's password:"
send $password
interact

我仍然建议按照@ Wolph的建议,在使用ssh访问远程服务器时查看使用一对密钥。

答案 1 :(得分:1)

我建议你使用密码少的身份验证,按照以下步骤更安全:

example@example:~/bash$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/example/.ssh/id_rsa): ## the directory which located private key and public key.
Enter passphrase (empty for no passphrase): ## you can use passphrase password if you want .
Enter same passphrase again: ## enter same password again.
Your identification has been saved in /home/example/.ssh/id_rsa.
Your public key has been saved in /home/example/.ssh/id_rsa.pub.
The key fingerprint is:
70:03:68:70:0a:52:16:55:55:66:62:2b:51:67:81:92 example@example
The key's randomart image is:
+--[ RSA 2048]----+
|o.=+oo++=oB.     |
|.o oo E+.B       |
|  ..  o.+        |
|       + .       |
|        S        |
|                 |
|                 |
|                 |
|                 |
+-----------------+

然后输入

scp -o Port=733 ~/.ssh/id_rsa.pub USER@example.net:/home/example/.ssh/authorized_keys
  • 然后它会第一次询问您的远程服务器是否有最新消息 时间
  • 现在,如果您输入ssh -p 733 USER@example.net,则无需输入 再次密码,密码减去认证比任何脚本更安全。
相关问题