0

I need to expand variables before running the SCP command as a result I can't use single quote. If I run the script using double quotes in Powershell ISE it works fine.

But doesn't work if I run the script through command prompt.

I'm using zabbix to run the script which calls the script as [cmd /C "powershell -NoProfile -ExecutionPolicy Bypass -File .\myscript.ps1"]

Here is the code that needs to run SCP using Cygwin bash.

if ((test-path  "$zipFile"))

{ 

C:\cygwin\bin\bash.exe -l "set -x; scp /cygdrive/e/logs/$foldername/dir1/$foldername.zip [email protected]:~/"

}

Output:

/usr/bin/bash: set -x;  /cygdrive/e/logs/myfolder/dir1/server.zip [email protected]:~/: No such file or directory

If I run the same command above in Cygwin manually it works.

I even tried to use bash -l -c but then the SSH session is stuck maybe because the [email protected] becomes $1 according to the documentation.

Documentation link

   -c        If the -c option is present, then commands are read from

             the first non-option argument command_string.  If there are

             arguments after the command_string, the first argument is

             assigned to $0 and any remaining arguments are assigned to

             the positional parameters.  The assignment to $0 sets the

             name of the shell, which is used in warning and error

             messages.
4
  • Try constructing the string? e.g. 'set -x; scp /cygdrive/e/logs/' + $foldername + '/dir1/' + $foldername + '.zip [email protected]:~/' or similar. Could also put that in brackets.... Commented May 15, 2020 at 6:51
  • Same error. No such file or directory. I looks /cygdrive/ isn't mounted when calling the ps script. I tried 'set -x; ls /cygdrive/c/' and it showed the same error. Commented May 15, 2020 at 7:17
  • hm set -x; /usr/bin/echo "test" says no such file or directory too Commented May 15, 2020 at 7:21
  • oh i have to use the bash -l -c switch and then ls and echo works. But -c switch doesn't work with scp. The session still hangs when constructing the string as well Commented May 15, 2020 at 7:24

1 Answer 1

1

Figured it out. It was halting when using bash -c was due to StrictHostKeyChecking, the known hosts thing (where you get a prompt to type yes/no). I set the -v switch to SCP and it showed me the Debug logs where it was halting.

Had to set scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null options.

The complete line now looks like the following:

c:\$cygwin_folder\bin\bash.exe -c ("/usr/bin/scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -v -i /cygdrive/c/cygwin/home/myuser/.ssh/id_rsa  /cygdrive/e/logs/$foldername/dir1/$foldername.zip [email protected]:~/")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.