Using pscp With Remote Environment Variables

3

I'm using pscp.exe to transfer files from Windows to Linux. This command works fine:

pscp myfile.txt user@host:/airflow/dags

The location on the Linux host into which I want to transfer the files is stored in a environment variable:

$ echo $AIRFLOW_HOME
/airflow

I'd like to refer to that environment variable in the call to pscp. Unfortunately it doesn't work:

pscp myfile.txt user@host:$AIRFLOW_HOME/dags

returns pscp: unable to open /dags: permission denied (which makes sense of course, /dags does not exist).

Clearly the $AIRFLOW_DAGS part of the command is not getting resolved on the host. So, is there a way to refer to environment variables on the host in my pscp command?

jamiet

Posted 2017-04-26T14:54:23.147

Reputation: 135

Answers

4

The utility pscp cannot resolve remote shell variables. As a workaround, retrieve the value of a variable first over ssh (using the PuTTY command plink) and store it in a Windows variable.

Place the following commands into a batch file and run it.

@echo off 
for /f %%i in ('plink -ssh user@host echo $AIRFLOW_HOME') do set HOMEVAR=%%i
pscp myfile.txt user@host:%HOMEVAR%/dags

Note: To use the for /f... command from the command line (outside of a batch file), replace both %%i with %i.

Steven

Posted 2017-04-26T14:54:23.147

Reputation: 24 804

Nice, thanks. I'm actually using PowerShell but I'm sure I can adapt it. Much appreciated. – jamiet – 2017-04-26T19:12:37.057