简体   繁体   English

通过 SSH 将密码作为环境变量传递

[英]Pass a password as an environment variable through SSH

Here is what I know:这是我所知道的:

echo "password" | sudo SOME_COMMAND

The above mentioned command will log in as root directly in Bash shell and run SOME_COMMAND .上述命令将直接在 Bash shell 中以root身份登录并运行SOME_COMMAND

What I am trying to do:我正在尝试做的事情:

I want to perform same task, but from a remote machine.我想从远程机器上执行相同的任务。 For that I want to pass the password as a variable via an SSH command.为此,我想通过 SSH 命令将密码作为变量传递。 Something like this:像这样的东西:

ssh -o PASSWORD=password user@hostA 'echo $PASSWORD | sudo SOME_COMMAND'

(Reference: When ssh'ing, how can I set an environment variable on the server that changes from session to session? ) (参考: ssh'ing时,如何在服务器上设置一个从会话到会话变化的环境变量?

But it doesn't pass the $PASSWORD variable.但它不传递$PASSWORD变量。

How can I do it?我该怎么做?

Simple summary:简单总结:

Here is what I want to do:这是我想做的事情:

I want to pass a variable to remote host when I log in through SSH so I can access it in the remote host script.当我通过 SSH 登录时,我想将一个变量传递给远程主机,以便我可以在远程主机脚本中访问它。

You can use the sshpass utility to do this task.您可以使用sshpass实用程序来执行此任务。 But this is not a secure way;但这不是一种安全的方式; your password is not in an encrypted format.您的密码不是加密格式。

Reference: sshpass: Login To SSH Server / Provide SSH Password Using A Shell Script参考: sshpass:登录到 SSH 服务器/使用 Shell 脚本提供 SSH 密码

You should not use sudo and ssh in one line.您不应该在一行中使用sudossh Here is a discussion and solution that I was answering yesterday on Super User.这是我昨天在超级用户上回答的讨论和解决方案


To pass a variable though a pipe you can do the following...要通过管道传递变量,您可以执行以下操作...

echo "Hello, World!" | ssh USER@HOST cat

Other methods would be passing files via scp or rsync .其他方法是通过scprsync传递文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM