如何将 sshpass 命令放入 bash 脚本中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19302572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:18:02  来源:igfitidea点击:

How to put sshpass command inside a bash script?

bashssh

提问by rcsimoes

I am trying to run a sshpass command inside a bash script but it isn't working.

我正在尝试在 bash 脚本中运行 sshpass 命令,但它不起作用。

If I run the same command from the terminal it works fine but running it in a bash script it doesn't.

如果我从终端运行相同的命令,它可以正常工作,但在 bash 脚本中运行它却不能。

#! /bin/bash

sshpass -p 'password' ssh user@host command

I am aware of the security issues but its not important now.

我知道安全问题,但现在不重要了。

Can someone help? Am I missing something.

有人可以帮忙吗?我是不是错过了什么。

Thanks

谢谢

回答by Emil Davtyan

Do which sshpassin your command line to get the absolute path to sshpassand replace it in the bash script.

不要which sshpass在命令行中获得的绝对路径sshpass,并在bash脚本替换它。

You should also probably do the same with the commandyou are trying to run.

您也应该对command您尝试运行的对象执行相同的操作。

The problem might be that it is not finding it.

问题可能是它没有找到它。

回答by Jotunn

Try the "-o StrictHostKeyChecking=no" option to ssh("-o" being the flag that tells ssh that your are going to use an option). This accepts any incoming RSA key from your ssh connection, even if the key is not in the "known host" list.

尝试ssh的“ -o StrictHostKeyChecking=no”选项(“-o”是告诉 ssh 您将要使用一个选项的标志)。这会接受来自您的 ssh 连接的任何传入 RSA 密钥,即使该密钥不在“已知主机”列表中。

sshpass -p 'password' ssh -o StrictHostKeyChecking=no user@host 'command'

回答by anubhava

1 - You can script sshpass's sshcommand like this:

1 - 您可以sshpass's ssh像这样编写命令脚本:

#!/bin/bash

export SSHPASS=password
sshpass -e ssh -oBatchMode=no user@host

2 - You can script sshpass's sftpcommandlike this:

2 -您可以脚本sshpasssftpcommandlike这样的:

#!/bin/bash

export SSHPASS=password

sshpass -e sftp -oBatchMode=no -b - user@host << !
   put someFile
   get anotherFile
   bye
!

回答by Secko

I didn't understand how the accepted answer answers the actual question of how to run any commands on the server after sshpass is given from within the bash script file. For that reason, I'm providing an answer.

我不明白接受的答案如何回答在 bash 脚本文件中给出 sshpass 后如何在服务器上运行任何命令的实际问题。出于这个原因,我提供了一个答案。



After your provided script commands, execute additional commands like below:

在您提供的脚本命令之后,执行如下附加命令:

sshpass -p 'password' ssh user@host "ls; whois google.com;" #or whichever commands you would like to use, for multiple commands provide a semicolon ; after the command

In your script:

在你的脚本中:

#! /bin/bash

sshpass -p 'password' ssh user@host "ls; whois google.com;"

回答by Bisca

This worked for me:

这对我有用:

#!/bin/bash

#Variables
FILELOCAL=/var/www/folder/$(date +'%Y%m%d_%H-%M-%S').csv    
SFTPHOSTNAME="myHost.com"
SFTPUSERNAME="myUser"
SFTPPASSWORD="myPass"
FOLDER="myFolderIfNeeded"
FILEREMOTE="fileNameRemote"

#SFTP CONNECTION
sshpass -p $SFTPPASSWORD sftp $SFTPUSERNAME@$SFTPHOSTNAME << !
    cd $FOLDER
    get $FILEREMOTE $FILELOCAL
    ls
   bye
!

Probably you have to install sshpass:

可能你必须安装 sshpass:

sudo apt-get install sshpass