自动进入与脚本SSH密码(Automatically enter SSH password with

2019-06-17 22:42发布

我需要创建一个脚本,自动输入密码才能OpenSSH的ssh客户端。

比方说,我需要通过SSH进入myname@somehost与密码a1234b

我已经尝试过...

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

...但是这是行不通的。

我怎样才能得到这个功能集成到一个脚本?

Answer 1:

首先,你需要安装sshpass 。

  • Ubuntu的/ Debian的: apt-get install sshpass
  • Fedora的/ CentOS的: yum install sshpass
  • 弓: 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

笔记:

  • sshpass也可以从一个文件时,读取密码-f标志传递。
    • 使用-f防止如果可见密码ps执行命令。
    • 该密码存储在该文件应该有安全权限。


Answer 2:

找对问题的答案为个月后,我终于找到了一个更好的解决办法:写一个简单的脚本。

#!/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

把它/usr/bin/exp ,那么你可以使用:

  • exp <password> ssh <anything>
  • exp <password> scp <anysrc> <anydst>

完成!



Answer 3:

使用公共密钥认证: https://help.ubuntu.com/community/SSH/OpenSSH/Keys

在源主机上运行这个只有一次:

ssh-keygen -t rsa # ENTER to every field
ssh-copy-id myname@somehost

这一切,在那之后,你就可以做的ssh没有密码。



Answer 4:

你可以使用一个预计脚本。 我没有写在一个相当长的一段时间,但它应该看起来像下面。 您将需要与头部的脚本#!/usr/bin/expect

#!/usr/bin/expect -f
spawn ssh HOSTNAME
expect "login:" 
send "username\r"
expect "Password:"
send "password\r"
interact


Answer 5:

我变异

sshpass -p PASSWORD ssh USER@SERVER

变体II

#!/usr/bin/expect -f
spawn ssh USERNAME@SERVER "touch /home/user/ssh_example"
expect "assword:"
send "PASSWORD\r"
interact


Answer 6:

sshpass具有更好的安全性

我偶然在此线程,同时寻找一种方式ssh到一中熄火,关闭的服务器 - 花了超过一分钟来处理SSH连接尝试,并且超时之前,我可以输入密码。 在这种情况下,我希望能够立即提供我的密码时,提示是可用的。

(如果它不是痛苦明确:在这种状态下的服务器,这是为时已晚,以建立一个公共密钥登录)。

sshpass救援。 不过,也有更好的方式去了解这方面比sshpass -p

我执行直接跳转到交互式密码提示(不浪费时间看,如果公共密钥交换可能发生),从来没有揭示了密码为纯文本。

#!/bin/sh
# preempt-ssh.sh
# usage: same arguments that you'd pass to ssh normally
echo "You're going to run (with our additions) ssh $@"

# Read password interactively and save it to the environment
read -s -p "Password to use: " SSHPASS 
export SSHPASS

# have sshpass load the password from the environment, and skip public key auth
# all other args come directly from the input
sshpass -e ssh -o PreferredAuthentications=keyboard-interactive -o PubkeyAuthentication=no "$@"

# clear the exported variable containing the password
unset SSHPASS


Answer 7:

sshpass + autossh

已经提到的一个不错的奖金sshpass是,你可以用它autossh ,消除更加互动效率低下。

sshpass -p mypassword autossh -M0 -t myusername@myserver.mydomain.com

这将允许autoReconnect的,如果,例如,您的无线网络被关闭你的笔记本电脑中断。



Answer 8:

# 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 root@owned.com -p 22

参考: https://www.linkedin.com/pulse/youre-doing-wrong-ssh-plain-text-credentials-robert-mccurdy?trk=mp-reader-card



Answer 9:

我不认为我看见有人建议这和OP只是说:“剧本”,所以...

我需要解决同样的问题,我最舒服的语言是Python。

我用的paramiko库。 此外,我还需要发出,我将需要使用升级权限的命令sudo 。 事实证明,sudo命令可以通过标准输入接受其密码通过“-S”标志! 见下文:

import paramiko

ssh_client = paramiko.SSHClient()

# To avoid an "unknown hosts" error. Solve this differently if you must...
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# This mechanism uses a private key.
pkey = paramiko.RSAKey.from_private_key_file(PKEY_PATH)

# This mechanism uses a password.
# Get it from cli args or a file or hard code it, whatever works best for you
password = "password"

ssh_client.connect(hostname="my.host.name.com",
                       username="username",

                       # Uncomment one of the following...
                       # password=password
                       # pkey=pkey
                       )

# do something restricted
# If you don't need escalated permissions, omit everything before "mkdir"
command "echo {} | sudo -S mkdir /var/log/test_dir 2>/dev/null".format(password)

# In order to inspect the exit code
# you need go under paramiko's hood a bit
# rather than just using "ssh_client.exec_command()"
chan = ssh_client.get_transport().open_session()
chan.exec_command(command)

exit_status = chan.recv_exit_status()

if exit_status != 0:
    stderr = chan.recv_stderr(5000)

# Note that sudo's "-S" flag will send the password prompt to stderr
# so you will see that string here too, as well as the actual error.
# It was because of this behavior that we needed access to the exit code
# to assert success.

    logger.error("Uh oh")
    logger.error(stderr)
else:
    logger.info("Successful!")

希望这可以帮助别人。 我使用案件创建目录,发送和执行解压缩文件,并在约300台服务器作为时间启动程序。 因此,自动化是极为重要的。 我试过sshpass,并期待再与该走了过来。

我希望它可以帮助别人,因为它没有我一样多!



Answer 10:

我得到这个工作如下

的.ssh /配置被修改,以消除是/否提示 - 我在防火墙后面,所以我不担心被欺骗的SSH密钥

host *
     StrictHostKeyChecking no

创建响应文件的预期,即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

创建您的bash脚本并调用希望在文件中

#!/bin/bash
i=1
while [$i -lt 129]    # a few nodes here

  expect answer.expect hadoopslave$i

  i=[$i + 1]
  sleep 5

done

获取与新的配置刷新128个Hadoop的数据节点 - 假设你使用的是NFS挂载为Hadoop / conf文件

希望这可以帮助别人 - 我是Windows numpty,这花了我大约5小时搞清楚!



Answer 11:

我有一个更好的解决方案,inclueds登录您的帐户不是改变以root用户。 这是一个bash脚本

http://felipeferreira.net/index.php/2011/09/ssh-automatic-login/



Answer 12:

@abbotto的答案并没有为我工作,不得不做一些不同的事情:

  1. 百胜安装sshpass改为- 转-ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/sshpass-1.05-1.el6.x86_64.rpm
  2. 要使用的命令sshpass改为- sshpass -p “通过” SSH用户@ mysite的-p 2122


Answer 13:

我设法得到它与工作:

SSH_ASKPASS="echo \"my-pass-here\""
ssh -tt remotehost -l myusername


Answer 14:

使用脚本中的这个脚本tossh,第一个参数是主机名,第二个将是密码。

     #!/usr/bin/expect
     set pass [lindex $argv 1]
     set host [lindex $argv 0]
     spawn ssh -t root@$host echo Hello
     expect "*assword: " 
     send "$pass\n";
     interact"


Answer 15:

要获取密钥交换从闪存盘的工作,你有你的私钥复制到您的驱动器,并在您的SSH命令中指定它(避免使用本地帐户的私钥),例如:

ssh -i id_rsa host

另外,您也可以使用预期(这是从外壳一个单独的脚本)。 这里有一个以前的有关问题,SSH和期待 。

请注意,任何人都可以打开expect脚本并查看纯文本的登录凭据。



Answer 16:

要通过外壳脚本连接远程机器上,使用下面的命令:

sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no USERNAME@IPADDRESS

其中IPADDRESSUSERNAMEPASSWORD都是需要脚本提供的输入值,或者如果我们想在运行时使用,以提供“读”命令。



文章来源: Automatically enter SSH password with script