使用ssh连接到服务器的Bash脚本 [英] Bash script that connects to server using ssh

查看:64
本文介绍了使用ssh连接到服务器的Bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a bash script that will allow me to login using ssh connection. The condition is that the password is stored into a .txt file.

我需要编写一个bash脚本,该脚本将自动请求与

I need to write a bash script that will automatically request a connection to

   ssh USER@example.net -p 733

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

and automatically will use the passwords that are stored into a .txt file

我从没用bash写过东西.有人能帮我吗.预先感谢您的宝贵时间.

I never wrote something in bash. Can someone help me. Thank you in advance for your time.

推荐答案

您不能使用普通的 bash 脚本来做到这一点.您需要使用 expect 命令.

You can't do it with plain bash script. You need to use expect command.

已经说过,在文本文件中存储密码确实是一个非常糟糕的主意,这是使用 expect 的方法:

Having said that it is really a very bad idea to have a password stored in a text file, here is how you can do it using 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 访问远程服务器时使用一对密钥.

Still I suggest to follow @Wolph's advice to look into using a pair of keys when accessing a remote server using ssh.

这篇关于使用ssh连接到服务器的Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆