Find number of active connections in Linux using various tools

1. Netstat
Netstat (network statistics) is a command-line network utility tool that displays network connections for the Transmission Control Protocol (both incoming and outgoing), routing tables, and a number of network interface (network interface controller or software-defined network interface) and network protocol statistics.
Using “netstat -a” will give you something sort of like this (this is a segment of my server):

tcp 0 0 app.mydomain.com:http 93.184.216.119:16494 SYN_RECV
tcp 0 0 app.mydomain.com:http 93.184.216.119:18733 SYN_RECV
tcp 0 0 app.mydomain.com:http 93.184.216.119.dsl.mwe:64775 SYN_RECV
tcp 0 0 app.mydomain.com:http 93.184.216.119.threembb.:16490 SYN_RECV
tcp 0 0 app.mydomain.com:http 93.184.216.119:video-activmail SYN_RECV
tcp 0 0 app.mydomain.com:http 93.184.216.119:45025 SYN_RECV
tcp 0 0 app.mydomain.com:http 93.184.216.119:dvl-activemail SYN_RECV
tcp 0 0 app.mydomain.com:http 41-135-22-100.dsl.mwe:64774 SYN_RECV

As you can see it does name resolving for us and all that good stuff. Sometimes very hand but that’s not what this is about. We want to get some solid numbers so we can take a broader perspective. To do this we can use the following command:

netstat -an | wc -l

Now, If we can check the No of Ips connected to port 80 . which is very helpful to detect the Ddos attack . the command is as under

netstat -tn 2>/dev/null | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -nr | head

Output – Total connections by IP, from highest to lowest.

97 114.198.236.100
56 67.166.157.194
44 170.248.43.76
38 141.0.9.20
37 49.248.0.2
37 153.100.131.12
31 223.62.169.73
30 65.248.100.253
29 203.112.82.128
29 182.19.66.187

If you would like to Watching active IP connections

watch -d -n1 ‘netstat -anp | grep -i stream’

2. SS
Socket statistics, or ss for short, is an easy replacement command for netstat. One way to use it, is with parameters ss -aut

-a: show listening and non-listening sockets
-u: show UDP
-t: show TCP

[root@archlinux ~]# ss -aut
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:bootpc *:*
tcp LISTEN 0 128 *:ssh *:*
tcp ESTAB 0 0 192.168.1.251:ssh 192.168.1.220:hnmp
tcp LISTEN 0 128 :::19531 :::*
tcp LISTEN 0 128 :::ssh :::*

This way it will show similar information to what netstat shows. When using it for very specific requests, you should refer to the man page, as it has some nice options.

Share This :

Use the built-in PHP server in WordPress development

Unlike the traditional development setup which requires to download and config a web server, you can use the built-in PHP server, which doesn’t ask for any complicated configuration like Apache HTTP Server or nginx …

Create a directory where you want to run PHP code,

$ mkdir php-project

$ cd php-project

then start a locally-awesome server,

$ php -S localhost:8000

That’s it, you have a fully-functional PHP web server, good enough for development.

For WordPress, just download the package and extract, access the extracted directory, and start the server.

$ wget https://wordpress.org/latest.zip -O wordpress.zip

$ unzip wordpress.zip

$ cd wordpress

$ php -S localhost:8000

You can change the localhost to any of binding network interface on your machine and replace 8000 for any port you want to set for development.

That means, when you want to develop multiple WordPress projects at the same time via one local address, you can spawn each project with different ports. It might be like this,

$ cd project-01

$ php -S localhost:9001 &

$ cd ../project-02

$ php -S localhost:9002 &

$ cd ../project-03

$ php -S localhost:9003 &

Well, I personally think this is much more convenient than manual configuring web server for newly-added projects. It’s also good for newbies to avoid the configuration hell when dealing with whatever XAMPP, LAMP, MAMP, WAMP …

Not only for WordPress, but also for every code running in PHP. Not to mention that many modern PHP frameworks make uses of this today, like Laravel, Lumen, Symfony, CakePHP, CodeIgniter… just to name a few.

Enjoy coding PHP 🙂

Share This :

How to get a VPS stats from SSH bash command line for SolusVM API

The client API is a http based API that allows you and your clients to interact directly with a virtual server with security and easiness. The client API can be disabled at any time. Access is only granted with a key and hash on a per vps basis. We will show you how you can get the solusvm info from SSH bash script . It will be helpful for all of you to use it in different script

The basic command is of this form:

#curl –data “key=$KEY&hash=$HASH&action=info&ipaddr=true&mem=true&hdd=true&bw=true” $SERVERURL/api/client/command.php

This returns data of the form:

152.65.68.127516192768,325312512,7190880256,4268435456000,115806842,268319649158,0134217728,47583232,86634496,35successmaster.dnsmagic.org134.120.43.51

In script form:
First define variables:

KEY=”LNAQP-ZEWDZ-3EKFR”
HASH=”076dffc30f43cc3a134666a04afba78932212b3323e”
ACTION=”info&ipaddr=true&mem=true&hdd=true&bw=true”
URL=”https://manage.cloud.com”

Then execute it from bash:

curl –data “key=$KEY&hash=$HASH&action=$ACTION” “$URL/api/client/command.php”

Shutdown:

ACTION=”shutdown”

Share This :

Quality Server Administration Solutions for Businesses

Small businesses do not usually have the funds or the ability to have an in-house IT department at their disposal but their need for quality technology is just as great as any other company. In fact, if your business is completely online then you are totally reliant on the IT expert to get it setup correctly the first time. When you can’t bring the experienced technicians to you then the best option is to outsource and find them online. reading this post

Share This :