bash script that checks ping output and run a second command restarting a strongswan service - amazon-linux

I'm new to the world of linux and scripting, The OS is Amazon Linux 2 (EC2 instance).
The instance have a Strongswan Site-to-Site VPN connectivity to endpoint IP address 1.1.1.1
The problem is that it disconnects randomly and I have to run the command "sudo strongswan restart" everytime to reestablish the connection.
What I would like to learn right now is a script that continuously checks the connectivity to 1.1.1.1 and runs the "sudo strongswan restart" once disconnected and how to set a cron job for it.
Right now the only thing I can manage for cron is to run this job every 1 hour which works.
0 * * * * sudo strongswan restart
I need help for the script that check for ping timeout and run the VPN restart command

Related

Bind for 0.0.0.0:50000 failed: port is already allocated on MacOS

I initially ran jenkins in a docker container through my MacOS terminal successfully after running docker-compose up which generated the long admin password cypher. However after I restarted my machine, the setup vanished. But each time I run docker-compose up after exposing jenkins port 8080 on port 8082 and Jira port 50000 on port 200000 having tried exposing them externally on other ports previously, I keep getting the error below:
**Creating jenkins ... error
ERROR: for jenkins Cannot start service jenkins: driver failed programming external connectivity on endpoint jenkins (****************************************************): Bind for 0.0.0.0:20000 failed: port is already allocated
ERROR: for jenkins Cannot start service jenkins: driver failed programming external connectivity on endpoint jenkins (****************************************************): Bind for 0.0.0.0:20000 failed: port is already allocated**
I have stopped, killed and removed all containers, removed all images and pruned all networks, but nothing seems to work.
What's a way around this and how do I free up allocated ports?
You can find the process that is running on port 20000 using:
lsof:
lsof -nP -iTCP -sTCP:LISTEN | grep <port-number>
or
netstat:
netstat -anv | grep <port-number>
It is probably just an old process that stays as zombie. Just kill that process (you can use kill -9 <pid>) and try the same operation again.

How to list currently running servers that are listening to localhost ports in vscode

I am using VS Code for development. After running the server as usually using npm start command (which was set up to run nodemon and the main 'app' file) I closed the terminal.
I thought that when terminal is shut down nodemon get shut down along with the terminal. Evidently this is not so as when I attempt to run npm start in the new terminal it throws an error that the port I set up my server to listen to is already in use.
Is it possible to see what servers are running currently and which ports they are listening to?
If there is no such command to list the currently running servers is there any way to shut down the running servers on the local machine without shutting down the laptop I am working on?
By the way everything mentioned above is being done on local machine and no remote server is used. Thank you in advance.
If you are on a Linux box you can run this to get the PID of any process running on that port:
Linux:
netstat -tnlp | grep {{PORT}}
This will likely find multiple lines since the number of the {{PORT}} value might show up in a PID, IP address, etc., so look through the list to find what you're looking for. The PID and process name will show up on the far right column of the result.
Example for Linux:
# netstat -tnlp | grep 443
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 14384/nginx: master
The column on the right (14384/nginx: master) is the PID of that process, and the process name. Once you have the PID you could do a kill {{PID}} to kill that process.
The Mac version of netstat is different, and doesn't display the PID (at least not that I can tell), and I'm not sure if there's a way to do the same thing on a Windows box.

port forwarding in raspberry pi on debian

I want to forward incomings from 192.168.1.50:5007 to 10.1.1.117:5007 on raspberry pi debian installed . I do not want to make a bridge between two networks. How can I achieve this?
You can use ssh:
ssh -L 192.168.1.50:5007:10.1.1.117:5007 -N localhost
this assumes that your server is the machine at 192.168.1.50. if not, you're going to need to give me the IPs of all the machines involved and tell me which is which.
You'll need sshd running but you should already have that with Raspbian.
After you run it, you'll need to authenticate. No forwarding will occur until then.
Once you've logged in, it will look like it's hung, but it's not; it just doesn't have any output to show you.
At this point the forwarding is active.
You can kill it with ctrl-c when you're done.
If you'd rather keep it running in the background instead of having an empty window sitting around, you can use ctrl-z (which will pause its execution) followed by bg which will resume the process in the background.
To stop the forwarding from a backgrounded job, you're going to have to find the pid in ps and kill it.
Run netstat -ano --tcp |grep 5007 to see your server listening for connections on 192.168.1.50:5007, remote computers making connections to 192.168.1.50:5007, and new connections from your server to 10.1.1.117:5007

What is veewee waiting for when it's waiting for ssh login?

When veewee is displaying the following message, Waiting for ssh login on 127.0.0.1 with user veewee to sshd on port => 7222 to work, timeout=10000 sec what exactly is it waiting on?
As far as I can tell there is a ssh server on port 7222 on the host that veewee has put up and it's waiting on that. This means that something in the guest is going to connect back to it. However, I can't figure out what that thing might be - and thus I can't debug further.
Further details
I'm trying to build a virtualbox image for vagrant with the CentOS-6.3-x86_64-minimal template. My steps:
bundle exec veewee vbox define 'ejs-centos6.3-1' 'CentOS-6.3-x86_64-minimal'
wget http://mirror.symnds.com/distributions/CentOS-vault/6.3/isos/x86_64/CentOS-6.3-x86_64-minimal.iso
bundle exec veewee vbox build 'ejs-centos6.3-1'
The CentOS install appeared to run without error but it's stuck waiting for the ssh login.
You're right, there's a Ssh server on listening on port 7222, but it's on the guest (VM), not the host.
The host (Veewee) is waiting to connect to it. This SSH service is supposed to become available when the VM install process finishes, that's one of the steps used by Veewee to assume that the setup went fine and that the VM is ready.
If Veewee blocks and never gets this SSH connection, I think there could be multiple reasons:
VM setup went wrong and something prevents it from finishing successfully. Check Veewee output and the Virtualbox VM graphical console that should have opened when launching vewee box build.
There's something preventing your host computer to connect to the VM at the network level.
The VM image doesn't have Sshd installed, and/or the veewee box configuration files (in veewee/definitions/ejs-centos6.3-1/) miss instructions to install the ssh package
You should try to login to the VM using Virtuabox console window and check if there's an ssh package installed (rpm -qa | grep openssh-server) and a process named sshd running.
I've run Veewee against Centos 7 built with GUI on and it stuck on anaconda asking for source of packages. I've checked the ks.cfg and it was pointing to dead resource (404). After pointing to valid url it went through.

How to set nodejs debug mode don't listen 127.0.0.1

I want to remote debug the nodejs program in Eclipse. I start the node script with the debug option.
$node debug script.js
But I can't connect to the node in Eclispe. When I netstat the node's TCP port. I found that node only listen 127.0.0.1 in debug mode. So I can't connect it from different computer.
But I can't find any startup options that can change to listen to any address.
Anyone know to make it listen to any address to remote debug in other computer?
if anyone else stumble upon this: you can set the node debug to any address as you set the port
node --debug=169.168.1.2:5858 app.js
if that would be the ip of your remote machine or even better to every machine
node --debug=0.0.0.0:5858 app.js
but please be aware that the 2nd option should only be used if you are debugging in your own private network as you open it up for everyone
This is what I do in linux Debian:
install balancer
sudo apt-get install balance -y
then create a route in balancer to reroute your 5858 port to 5859
balance 5859 127.0.0.1:5858
start your app
node --debug app.js
now you can access it from everywhere on port 5859
I'm looking into V8 code that goes through deps/v8/src/debug-agent.* down to deps/v8/src/platform-posix.cpp (for linux) to POSIXSocket::Bind method and it can't seem to have any option about this (unless I'm missing something).
I bet you either hack it and recompile node or you'll need to build a small proxy beside your node process.
Here's a great tut on debugging nodejs from eclipse. Note at the bottom there is a script the author uses to forward localhost:5858 to the remote server's 127.0.0.1. You could also just use an SSH tunnel.
So, to summarize:
start your script with node --debug app.js
configure eclipse as if you were debugging locally
use the node_g script or configure an SSH tunnel
go on vacation now that your code is bug-free
to debug nodejs remotely over SSH session do:
1. install balance on Linux: https://balance.inlab.net/overview/
2. run the command: balance -df 8585 127.0.0.1:5858 > /tmp/balance.out 2>&1 &
3. ssh to your remote Linux box (tunnel will be created 8585 > 5858 > nodejs)
4. run your node script on server: node --debug-brk --nolazy ./myNodeApp.js
5. kick off debug session in WebStorm alt-d to port 8585
now you are remote debugging securely over SSH session