use cygwin from cmd.exe - windows

is it possible to use cygwin from cmd.exe in Windows?
i want to do following
FOR /L %G in (1 1 100) DO sshpass -p "password" scp E:\in\File%G.csv openstack#remotecomp:/testfiles/
transfer 100 files from windows to unix computer
now question:
can i use sshpass and scp from cmd.exe as long as cygwin is installed? or do i have to use cygwin terminal?
and do the files which i want to transfer neccessarily have to be in cygwin's folder?
goal is to use this one command in a BPEL activity, and as such there should not be any kind of user interaction when the command is run

You can use the full paths to the binaries, or create a wrapper script and call that to do the cygwin commands, e.g.
#!/bin/bash
# doit.sh wrapper script in home dir of cygwin user
echo "got args $#"
and call it from windows shell with
for /L %G in (1 1 100) DO c:\cygwin\bin\bash --login -c "/home/youruser/doit.sh %G"
in this case it prints 1..100 out, but your wrapper script can do all the ssh stuff it needs to instead.
For a more complex script, you can pass params from windows to the shell script like this:
#!/bin/bash
# callssh.sh
COUNT_NUM=$1
PASSWORD=$2
FILE_PATH=$(cygpath $3)
sshpass -p $PASSWORD scp $FILE_PATH openstack#whereever:/some/path/
and you'd call it with
for /L %G in (1 1 100) DO c:\cygwin\bin\bash --login -c "/home/youruser/callssh.sh %G your_password E:/in/File%G.csv"
Note the use of cygpath to convert from windows path style to something that cygwin understands. The SSH executable is a cygwin process so you need to convert to something it will be able to use.
Also, note the change from backslashes to forward slashes, again for some quirks in path separators between windows and cygwin.
If you have trouble with this (I can't test your code for you directly), then do this on a cygwin shell:
cygpath E:/path/to/file
and then check it outputs something like /e/path/to/file (or /cygpath/e/path/to/file if you haven't remapped our root drives).

Related

Excute sh script using git-bash on Windows 10 using ssh

I want to execute an sh script on a remote Windows 10 computer without logging into interactive shell.
On Linux I would do something like
ssh user#machine "echo execute program automatically && ./my_program "
However when I ssh into my Windows 10 machine using open-ssh server it opens by default cmd. In interactive mode I would now open git-bash
"C:\Program Files\Git\bin\sh.exe" --login
Then execute my script from there.
When I try to combine the commands like this
ssh user#machine ' "C:\Program Files\Git\bin\sh.exe" --login && ./my_program " '
the shell freezes. Without the single quotes, the command also doesn't work (obviously).
So what to do?
Try to change default shell, with administrator CMD :
reg add HKLM\SOFTWARE\OpenSSH /v DefaultShell /d "C:\Program Files\Git\bin\sh.exe"

Linux to windows execute commands remotely

I have to zip files on a remote windows machine.
So I first ssh to the windows machine
ssh my_user#my_host "cd /d D:\MyFolder"
And the above command works.
However if I try to run any command after that it fails.
So if I do something like
ssh my_user#my_host "cd /d D:\MyFolder; dir"
The system cannot find the path specified.
You can try something like:
ssh my_user#my_host <<EOF
cd /d D:\MyFolder
dir
EOF
The second EOF should be alone on the line and start from the begin

ssh and chroot followed by cd in shell

How to excute cd command after chroot to a remote node in shell script?
For ex:
I need this.
ssh remote-node "chroot-path cd command here; extra commands"
Without chroot it works fine, If I put the command list in another shell script and execute the shell script after chroot it seems to run okay.
But chroot seems to break cd?
Use printf %q to have your local shell (which must be bash) give you correct quoting that works, and bash -c to explicitly invoke a remote shell compatible with that quoting (as %q can generate bash-only quoting with input strings that contain special characters) under your chroot.
cmd_str='cd /to/place; extra commands'
remote_command=( bash -c "$cmd_str" )
printf -v remote_command_str '%q ' "${remote_command[#]}"
ssh remote-node "chroot /path/here $remote_command_str"
The bash -c is necessary because cd is a shell construct, and chroot directly exec's its arguments (with no shell) by default.
The printf %q and correct (single-quote) quoting for cmd_str ensures that the command string is executed by the final shell (the bash -c invoked under the chroot), not your local shell, and not by the remote pre-chroot shell.
Assuming by chroot-path you mean chroot /some/root/path.
chroot only takes a single command and cd isn't a command it is a shell built-in so that won't work.
Additionally only cd command here is being run (or attempted to) under the chroot setup. Everything after the ; is running in the main shell.
A script is the easiest way to do what you want.

Windows 7 Task Scheduler BASH Script Fails

In order to use rsync I created a BASH script. It runs fine from the Cygwin shell in WIN 7 but fails when run from the WIN 7 Task Scheduler. My Task Scheduler Script is a simple:
c:\cygwin\bin\bash.exe -l -c "~user/rsync_Windows_Backup 2>&1 >> ~user/Documents_cron.log"
The initial directory is set to C:\Cygwin\bin.
My BASH script is a typical rsync command with [options] SRC DEST and some related housekeeping.
The rsync command within the "rsync_Windows_Backup" BASH script is:
/bin/time -f "\nElapse (hh:mm:ss.ss) %E" \
rsync.exe -v -rltz --chmod=a=rw,Da+x -u "$SRC" "$DEST" >> "$LOG" \
2 >> "$LOG"
$ ./rsync_Windows_Backup - succeeds.
But the Task Scheduler Job fails carping that it cannot find the DEST Folder that the BASH script references. When I do a "cd DEST" from the BASH command line the Folder is avialable and can be written to.
I should add some more details that the sender is a WIN 7 desktop that is mapped to a Vista desktop receiver with a drive mapping J:. The BASH script does start but fails with:
rsync: writefd_unbuffered failed to write 4 bytes to socket [sender]: Broken pipe (32)
rsync: mkdir "/cygdrive/J/DocumentsBackup" failed: No such file or directory (2) rsync error: error in file IO (code 11)
I have tried several ideas to influence how WIN 7 handles mappings and permissions assuming this is the root of the problem. So far nothing seems to help.
Another characteristic is that the exact same BASH script and Task Scheduler Job does succeed it WIN Vista Business Edition. So I am assuming there is something in WIN 7 that I am missing.
I am stumped and could use some guidance.
Thanks.
I now have this working in Win 7 from the task scheduler as I need. Thank you to #netubsi and #firerat of LinuxQuestionsorg and #konsolebox for the suggestions that lead to a solution.
Here is what I did:
cmd /c net use T: '\\server\share' # Created a separate temporary share for Cygwin
DEST="/cygdrive/T/User/FolderBackup/" # Use temporary Share in Destination
rsync -avuz --copy-links "$SRC" "$DEST" # Do backup
cmd /c net use T: /delete # Remove temporary share
It appears that in WIN 7 the share created in Windows is NOT available to a Cygwin script, IF it is launced from the Win 7 task scheduler. It IS available if the script is launced from the Cygwin command line. It also appears that this is NOT an issue in Win Vista.
This seems odd to me. Perhaps there is another explanation that I am missing. However I am just relieved to have this working!!
You can also just use the network address directly in cygwin:
DEST="//server/share/User/FolderBackup"
Cygwin mounts local and mapped drives under /cygdrive. Using taskscheduler in win7 if you list the contents of /cygdrive, all you will see are local drives???
First option is to run your script as
c:\cygwin\bin\bash.exe -l -c "~/rsync_Windows_Backup >> ~/Documents_cron.log 2>&1"
If you want to capture the stderr output as well, you have to place it in front to copy the fd of the file, and not of stdout.
Make sure that rsync_Windows_Backup has executable permissions. Running ls -l ~/rsync_Windows_Backup should show it.
If it doesn't work, try to use absolute paths. On your Cygwin screen where the current direcory shows ~ in the prompt type pwd which would show something like
User#System ~
$ pwd
/home/User
Basing from that as an example your command should now be like:
c:\cygwin\bin\bash.exe -l -c "/home/User/rsync_Windows_Backup >> /home/User/Documents_cron.log 2>&1"

From cmd, log into Bash and run command in one line

I am developing a TIBCO application and need to be able to run a scp command from an external command resource.
Anyway the issue boils down to being able to log in to bash.exe then run my scp command in the same line.
it works when I run (from cmd.exe)
c:\cygwin\bin\bash.exe --login
(I enter bash)
Then I can run my scp statement just fine
scp account#server:~mysourcedirectory/targetfilename* /cygdrive/c/targetfolder
I want to do these two things in the same line but it doesnt work, aka
c:\cygwin\bin\bash.exe --login -c scp account#server:~mysourcedirectory/targetfilename* /cygdrive/c/targetfolder
will fail. What am I doing wrong and how can i run this command in one go?
Thanks
You have to quote the command.
c:\cygwin\bin\bash.exe --login -c "scp account#server:~mysourcedirectory/targetfilename* /cygdrive/c/targetfolder"
Tested using cmd in wine in gnu/linux. Single quotes did not work, I don't know all the quoting rules for cmd. But the above did work.

Resources