hierynomus / hierynomus/sshj

How to pass an environment variable when executing a command

Open
#637 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.7k
Forks
620
Avg merge
3d 23h
Merged PRs (30d)
11

Description

Hi,

I use SSHJ to perform a remote backup of my PostgreSQL database.

I need to pass the password as an environment variable named `PGPASSWORD`. I don't use the .htpasswd file to prevent the password from appearing in clear text on the disk.

I was able to do it this way:

```java
String command = "/usr/bin/pg_dump --file " + dumpFileName + " --host 127.0.0.1 --port 5432 --username postgres --no-password --format=c --blobs --clean " + baseName;

command = "export PGPASSWORD=\"" + password + "\" && " + command;

try (Session session = ssh.startSession();
Command cmd = session.exec(command);)
{
cmd.join(TIMEOUT, TimeUnit.MILLISECONDS);
}
```

**Is there a better way to pass this environment variable ?**

For example something like in Java for local command execution:

```java
String[] envp = { "PGPASSWORD=" + password };
p = Runtime.getRuntime().exec(command, envp);
```

Thanks,

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.