Establish SSH Tunnel as background windows service - windows

I would like to create a background service on Windows 11 for a persistent SSH Tunnel to a remote Linux machine using OpenSSH.
I have setup the SSH config file as such that you can run the command
ssh MyRemoteHost
and the connection will be successfully made.
However, when I try to create a service with
PS New-Service -Name "MyRemoteConnection" -BinaryPathName "C:\Windows\system32\OpenSSH\ssh.exe MyRemoteHost"
the service will be created but I canĀ“t start it. I also tried to create a .BAT file with the SSH command and run this as a service with the same result.
Any help with this would be greatly appreciated.

Related

Keeping the processes running even after exiting SSH to a windows machine

I am connecting to a remote machine through SSH client. Both SSH server and client are on windows OS.
I usually run tomcat 7 from such ssh session. But, as soon as I end the session, tomcat server shuts down as well. I run tomcat7.exe directly from its installation folder.
I have used &! at the end of the command to ensure that the process doesn't get attached to the terminal. But, couldn't achieve the desired result.
Kindly suggest how to detach such processes from the ssh client.
tomcat7.exe start and tomcat7.exe stop
these two commands ensure that tomcat starts as a service. Worked like a charm for me.

How to persist Enable-PSRemoting in powershell

I am able to enable remoting using below command
"Enable-PSRemoting -Force".
But after restarting machine, enable remoting is going off and need to enable it again using above command.
Is there any way to persist PSRemoting enable?
Actual my requirement contains
Connecting remote machine
Executing few commands
Restarting remote machine
connect remote machine again
Execute few more commands.
The above all steps needs to be done in single script.
I am not able to connect my remote machine after restart as its settings(Enable-remoting) is going off.

How I can start a service on my Unix server from windows

I need to stop/start an application service on Unix, while I being on windows. I don't want to do putty and login to unix server and stop service.
What batch script I can write to do this from windows?
If you don't want to do this manually, make sure you can run the ssh command from your batch file (by instaling it and extending your PATH if necessary to where it is installed) and do:
ssh root#your-system-name stop service-name
ssh root#your-system-name start service-name
This assumes that your linux machine has start and stop commands, otherwise you might need to invoke /etc/init.d/service-name stop or other Linux distro specific command.

SSH Connection from MAC to Amazon EC2 not working

I am trying to connect to Amazon EC2 via:
ssh -i ~/.ssh/YOUR_KEYPAIR_FILE.pem ec2-user#YOUR_IP_ADDRESS
The terminal takes 1 or 2 mins and then prints:
ssh: connect to host XXX port 22: Operation timed out
Any ideas?
Login to AWS
Go to the Instances section
Click on the security group associated with your EC2 instance
Down the bottom click on the inbound tab and then click edit
Create this rule
TYPE SSH
PROTOCOL TCP
PORT RANGE 22
SOURCE Anywhere
You should now be able to connect to the instance on port 22 via ssh with your key.
You need to open port 22 in your security group. All ports are closed by default.
Can you try changing permissions to YOUR_KEYPAIR_FILE.pem like this
chmod 600 YOUR_KEYPAIR_FILE.pem
Then shoot the command
ssh -i YOUR_KEYPAIR_FILE.pem ec2-user#YOUR_IP_ADDRESS
I had a similar problem. I checked all my networking time and time again from the ec2 instance all the way through the VPC and out to the internet. Security groups were allowing all sources through ports 22 and 80. My NACL was allowing the right permissions. I knew AWS was all ok yet everytime I went to try ssh into an instance I would still get an operation timeout, indicating that problem must be with my local machine instead.
First to check that the ssh port was open I ran the following:
ssh localhost
This worked fine!
Afte doing some research on the net, in the end it all boiled down to java and my terminal not recognising that java was installed on my machine.
Supporting Document:
AWS Documentation
No Java means that your .pem will not be recognised
Start by running the follwing:
java -version
If you get no hits then install relevant java SDK for your OS and once installed run
which java
You should get something like this:
/usr/bin/java
Now we can try connect to an instance again and hopefully you should have success this time!
ssh -v -i ~/Downloads/labamikey.pem ec2-user#ec2-34-200-217-2.compute-
__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|
[ec2-user#ip-10-0-0-54 ~]$

Windows Batch file for checking ssh tunnel alive

Using putty (windows version), I have established a SSH tunnel for accessing MySQL. I can automatically start the session by putty -load "sessionname" -pw 1234
Using the windows task scheduler, I'd like to periodically execute a batch file which checks if the connection is still alive - and if not, the tunnel should be restarted automatically.
Any idea how to achieve that?
brgds, janosh.

Resources