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

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

我已经试过了……

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

...但这并不奏效。

如何将此功能放入脚本中?


当前回答

我使用下面的解决方案,但你必须安装sshpass如果它还没有安装,使用sudo apt install sshpass安装它

现在你可以这样做,

sshpass-p *YourPassword* ssh root@IP

您还可以创建一个bash别名,这样就不必一次又一次地运行整个命令。 遵循以下步骤

cd ~

Sudo nano .bash_profile

在文件末尾添加以下代码

mymachine() {sshpass -p *YourPassword* ssh root@IP}

源. bash_profile

现在只要在终端运行mymachine命令,你就会进入你的机器,没有密码提示。

注意:

我的机器可以是你选择的任何命令。 如果在这个任务中安全性对您来说不重要,而您只想自动化工作,您可以使用此方法。

其他回答

通过shell脚本连接远程机器,使用以下命令:

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

其中IPADDRESS, USERNAME和PASSWORD是需要在脚本中提供的输入值,或者如果我们想在运行时提供,使用“read”命令。

我设法让它工作了:

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

我得到了这个工作如下

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

host *
     StrictHostKeyChecking no

为expect创建一个响应文件,即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脚本并在文件中调用expect

#!/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 datanode -假设您正在为hadoop/conf文件使用NFS挂载

希望这能帮助到一些人——我是一个Windows傻瓜,这花了我大约5个小时才弄明白!

我想我没有看到任何人提出这个建议,而操作人员只是说“脚本”,所以……

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

我用了paramiko图书馆。此外,我还需要使用sudo发布需要升级权限的命令。事实证明,sudo可以通过“-S”标志通过stdin接受其密码!见下文:

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, expect,然后想到了这个。

如果您在Windows系统上执行此操作,您可以使用Plink (PuTTY的一部分)。

plink your_username@yourhost -pw your_password