sshj
sshj copied to clipboard
How to pass an environment variable when executing a command
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:
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:
String[] envp = { "PGPASSWORD=" + password };
p = Runtime.getRuntime().exec(command, envp);
Thanks,