Opening Cygwin with Windows bat file and running script file - windows

I require a tunnel between my windows machine to a UNIX server and wish to automate the process so that on startup the tunnel will be generated for me.
I installed Cygwin with ssh and autossh to connect to the remote server, built up the connection manually, and have confirmed that the connection works. The process involves 3 commands, which isn't a lot but something that would be great to have automated.
After creating a .sh script file, which includes my autossh connection commands, and saving it using Notepad ++ as a UNIX document (to avoid any potential conflicts regarding the file ending), I can navigate to this script in Cygwin and call bash script.sh. After which the connection is made and I can work on my server.
My problem comes when creating my bat file:
start /d "C:\cygwin\bin\" mintty.exe "C:\Users\user\Documents\Dev\" script.sh
The first part up to and including the .exe file works to open the Cygwin window, but I have been unsuccessful in feeding the script into it. I even tried including a --bash command before referencing the script file as follows, but I received an error that the command is unknown:
start /d "C:\cygwin\bin\" mintty.exe --bash "C:\Users\andrew\Documents\Development\" tunnel.sh
Does anyone know if and how it is possible to open a Cygwin window and call a script file within this window? This is my first time creating a bat file, so I hope this is perhaps a newbie problem that no one even bothers to post a solution online for...

you don't need start.
assuming your Cygwin is in C:\cygwin
you need just:
chdir c:\cygwin\bin
mintty /usr/bin/bash -l -c /cygdrive/c/Users/user/Documents/Dev/script.sh

Related

How to run zookeeper.sh file in windows

I am following this tutorial where i have to run this command in order to start the zookeeper server.
./bin/zookeeper-server-start.sh config/zookeeper.properties
But the problem is this command is not working properly. I found that .sh file is bash file that required cygwin. I have installed it and then run command like this
C:\cygwin64\bin\bash.exe ./bin/zookeeper-server-start.sh config/zookeeper.properties
But it is showing:
I can confirm that in bin directory the file is exsits. what i am doing wrong?
Here is my directory snapshot from where i running the command:
Note: I have successfully tested bin/windows zookeeper bat file but i want to run it through .sh file as the kafka security tutorial which i am following using this.
From your screenshot, I conclude that you are using Cygwin. So, please add the cygwin tag to your question.
As you can see from the error message, the command dirname is not found by bash, so assuming that your Cygwin installation is not broken, I assume that the PATH is not set correctly; in your setup, dirname.exe should be in C:/cygwin64/bin (please verify this).
Your usage of bash.exe is a bit unusual in that you run it directly from a Windows cmd prompt. The more common way would be to use it from the 'Cygwin Terminal', which you get created a Windows-link to, when installing Cygwin, or to use another suitable Terminal program; I'm using for instance mintty for this task (also available via the Cygwin installer).
Having said this, it is possible to run bash.exe in the way you are doing it, but you then have to ensure, that at least the PATH is set up correctly. One possibility to do this, is to add C:\cygwin64\bin to your Windows PATH, but this has the drawback, that some commands have the same name in the Windows world and in Cygwin, though they serve a completely different purpose, and this will bite you sooner or later. Another problem is that at some point, you will rely on other bash specific setups besides the PATH.
A better way to accomplish your goal is IMO to ensure, that the system wide bash-initialization files are sourced by bash. If I have to run the script from a Windows cmd prompt, I would run it by
C:\cygwin64\bin\bash.exe --login YOURSCRIPT
This will read the file (in your setup) C:\cygwin64\etc\profile before running YOURSCRIPT, so you can check, that the PATH is correctly set there, by looking at this file. In a default installation, this should be the case.
After having read this file, it will try to read the file .bash_profile in your Cygwin HOME directory, so if you need additional settings for your (non-interactive) bash-scripts, create this file and put your settings there.

TeraTerm execution through the Windows command prompt

I have some basic simple two character commands to be executed in TeraTerm.
Is there a way to execute the same TeraTerm commands through a Windows command prompt? This will overcome my dependency to open TeraTerm and then running, commands. I can directly write some .bat file to execute my short commands.
Assuming that your commands are for teraterm, and not for the OS of your device connected via teraterm, you can save your commands into a .ttl file (using notepad or whatever). You can still make it work if the commands are for the OS (using a command line or whatever in the shell of the OS program connected), but you will have to make extra commands to navigate to it.
Using the .ttl file, you can make a .bat file that does two things:
cd C:\Program Files\teraterm
TTPMacro C:\[point to .ttl file]
This won't prevent teraterm from opening, because if you script involves you interfacing with teraterm at all, it'll open unless you add /V after TTPMcro. However, it will be a hands free experience.
You can find out more about deploying the macro here: https://ttssh2.osdn.jp/manual/en/macro/
And a list of all the commands here: https://ttssh2.osdn.jp/manual/en/macro/command/index.html
And if you need special characters for navigation (ctrl+s, etc), the ASCII code table is here: https://ttssh2.osdn.jp/manual/en/macro/appendixes/ascii.html
Hope that helps!
If it's only several basic commands, you can make it without the ttl file by using TTermPro in lieu of TTPMacro.
cd C:\Program Files\teraterm
TTERMPRO /C=1 (connect thru serial com 1)
TTERMPRO etc etc
The syntax for command line using TTERMPRO can be found here: https://ttssh2.osdn.jp/manual/en/commandline/teraterm.html

How do I tell a windows batch script to execute the next line without waiting for the previous line to finish executing?

I'm setting up a .cmd script to be run on startup for a pseudo-server (It's actually a laptop, but that's irrelevant) to automatically launch pageant, load an SSH key, connect to an SSH server using Putty (Pageant would automatically authenticate with the key), then launch mIRC which in turn has a series of scripts setup to operate as an IRC bot and automatically connect to networks using putty as an SSH tunnel.
With that in mind, I have the below code in a startup.cmd file:
"C:\Program Files (x86)\PuTTY\pageant.exe" c:\Path\To\Private\Key.ppk
"C:\Program Files (x86)\PuTTY\putty.exe" -ssh user#host
"C:\Program Files (x86)\mIRC\mirc.exe"
EXIT
When I test run this file, the command prompt runs the first line, launches pageant, and then sits there and does nothing until I close pageant completely. I believe I have an idea on what the issue here is, but I can't find any information on how to resolve this in a batch file.
I do know on linux systems, if I were running a bash script to do something similar, I would want to have a & symbol at the end of each line to tell it to run the next command without waiting for the previous command to finish executing. I did try that in the batch script in the off chance that would work (It didn't).
For those who may ask, this is on Windows 8.1 64 bit. The user running this script is not an administrator.
I can't comment to expand on Squashman's suggestion, so let me answer here.
In your case, if you only want to have Pageant running in the background, without interacting with it, I think it's best to run:
START "" /B <your command>
The /B parameter will spawn the process without launching a new window for it, which seems like something you'd like to avoid (anyway, it's probably closest to the behaviour you can obtain in Linux with &).
Please note that if you close the window from which you spawned this process, it will terminate as well.

Run Cygwin script on shutdown or startup

I'm extremely new to Cygwin but I am somewhat comfortable in Linux (I can read man files fine).
I want to create a BASH script using Cygwin that deletes the files in a folder on the shutdown signal given by Windows. If this can't be done, I also could try deleting the files in the same folder on startup. I installed CRON but does CRON only works for scheduled tasks, rather than on 'signals'? Answers would be nice but a general idea of how to proceed would be even better!
I can write the script. I just don't know exactly how Cywgin interacts with the Windows OS in order to perform these procedures.
Another question, how do I run CRON on Windows startup?
If it matters, my O.S. is Windows 10 x64 running Cygwin.
Cygwin.bat, a batch file which was installed under cygwin installation folder will give you hint of how to run cygwin script.
The script contains just:
C:
chdir C:\cygwin64\bin
bash --login -i
to run the bash shell interactive.
Make a copy of Cygwin.bat with another name (Startup ?) and change last line in
bash --login path_to_your_script_here
Put the bat file or a link to in in the Startup folder.
Great thread over here: https://serverfault.com/questions/245945/autostart-cygwin-on-windows-boot-and-run-a-cygwin-command
tl;dr
you can put command directly:
#echo off
C:
chdir C:\cygwin64\bin
bash -c "/usr/bin/whatever"

Run a remote bash script on a Mac using PuTTy

I want to run a bash script on a mac remotely from a batch script on a windows machine. On Windows I have this:
#echo off
echo bash /applications/snowflake/table-updater/test2.sh; exit>tmp_file
putty -ssh User#remote_machine -pw password -m tmp_file
And here is test2.sh on the remote machine
#!/bin/bash
# test2.sh
#
#
7za x table-apps.zip -y -o/Applications/snowflake/applications
When the batch file runs it logs in successfully, but for some reason fails to run the bash file. However the bash file runs fine from mac terminal, where it unzips the files perfectly. What could be happening here?
Please note test2.sh is actually in Applications/snowflake/table-updater as specified in the batch file. And the tmp file does write fine as well. My aim is to have a script to access a further 10 remote machines with the same directory structure.
Thanks in advance
The standard program which resembles the scriptable Unix command ssh in the PuTTy suite is called plink, and would probably be the recommended tool here. The putty program adds a substantial terminal emulation layer which is unnecessary for noninteractive scripting (drawing of terminal windows, managing their layout, cursor addressing, fonts, etc) and it lacks the simple feature to specify a command directly as an argument.
plink user#remote_machine -pw password /Applications/snowflake/table-updater/test2.sh
From your comments, it appears that the problem is actually in your script, not in how you are connecting. If you get 7za: command not found your script is being executed successfully, but fails because of a PATH problem.
At the prompt, any command you execute will receive a copy of your interactive environment. A self-sufficient script should take care to set up the environment for itself if it needs resources from non-standard locations. In your case, I would add the following before the 7za invocation:
PATH=$PATH:/Applications/snowflake/table-updater
which augments the standard PATH with the location where you apparently have 7za installed. (Any standard installation will take precedence, because we are adding the nonstandard directory at the end of the PATH -- add in front if you want the opposite behavior.)
In the general case, if there are other settings in your interactive .bashrc (or similar shell startup file) which needs to be set up in order for the script to work, the script needs to set this up one way or another. For troubleshooting, a quick and dirty fix is to add . /Users/you/.bashrc at the top of the script (where /Users/you should obviously be replaced with the real path to your home directory); but for proper operation, the script itself should contain the code it needs, and mustn't depend on an individual user's personal settings file (which could change without notice anyway).

Resources