用脚本自动输入SSH密码 [英] Automatically enter SSH password with script

查看:72
本文介绍了用脚本自动输入SSH密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个脚本来自动向 OpenSSH ssh 客户端输入密码.

I need to create a script that automatically inputs a password to OpenSSH ssh client.

假设我需要使用密码 a1234b SSH 到 myname@somehost.

Let's say I need to SSH into myname@somehost with the password a1234b.

我已经试过了...

#~/bin/myssh.sh
ssh myname@somehost
a1234b

...但这不起作用.

我怎样才能将此功能写入脚本?

How can I get this functionality into a script?

推荐答案

首先你需要安装sshpass.

  • Ubuntu/Debian:apt-get install sshpass
  • Fedora/CentOS:yum install sshpass
  • Arch:pacman -S sshpass

示例:

sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM

自定义端口示例:

sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM:2400

注意事项:

    -f 标志被传递时,
  • sshpass 也可以从文件中读取密码.
    • 使用 -f 可防止在执行 ps 命令时密码可见.
    • 存储密码的文件应该具有安全权限.
    • sshpass can also read a password from a file when the -f flag is passed.
      • Using -f prevents the password from being visible if the ps command is executed.
      • The file that the password is stored in should have secure permissions.

      这篇关于用脚本自动输入SSH密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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