Swap usage Linux scripts and commands

Swap usage Linux scripts and commands

To see SWAP usage by processes script:

#!/bin/sh

# Get current swap usage for all running processes

# Erik Ljungstrom 27/05/2011

SUM=0

OVERALL=0

for DIR in `find /proc/ -maxdepth 1 -type d | egrep “^/proc/[0-9]”` ; do

PID=`echo $DIR | cut -d / -f 3`

PROGNAME=`ps -p $PID -o comm –no-headers`

for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk ‘{ print $2 }’`

do

let SUM=$SUM+$SWAP

done

echo “PID=$PID – Swap used: $SUM – ($PROGNAME )”

let OVERALL=$OVERALL+$SUM

SUM=0

done

echo “Overall swap used: $OVERALL”

./getswap.sh | egrep -v “Swap used: 0” |sort -n -k 5

To see free memory and swap space:

free -hm

Top command to calculate swap usage:

top

(SWAP = VIRT – RES)

Linux scripts and commands to see RAM/memory utilization

Script to run top command after every 5 minutes RAM utilization:

#!/bin/sh
top -b -n 1 -o %MEM  | head -17 >> /var/log/logging.txt
echo “=========” >> /var/log/logging.txt

Change the permissions to execute,
chmod 777 script1

move the script to cronfrequently to run it after every 5 mins,
mv script1 /etc/actions/cronfrequently/

(use cronhourlybyfour directory for 15 mins execution)

To see the RAM usage by processes:

ps -eo size,pid,user,command | awk ‘{ hr=$1/1024 ; printf(“%13.6f Mb “,hr) } { for ( x=4 ; x<=NF ; x++ ) { printf(“%s “,$x) } print “” }’ | sort

top -b -n 1 -o %MEM  | head -17

To see RAM stats:

vmstat

To see free memory and cached space:

free -hm

To see a specific process CPU or RAM utilization using top::

top -p `pgrep PROCESS_NAME | tr “\\n” “,” | sed ‘s/,$//’`
(e.g. datastore.pl)

Script to see what processes are using  max RAM every 5 minutes:

/var/log/9999monitorram

#!/bin/sh

OutDir=/var/log/monitoring

File=RamUsage$(date +%d-%m-%y)

mkdir -p $OutDir

echo “——————————–$(date +%d-%m-%y)—–$(date +%H:%M:%S)—————————————“>> $OutDir/$File

ps -eo size,pid,user,command | awk ‘{ hr=$1/1024 ; printf(“%13.6f Mb “,hr) } { for ( x=4 ; x<=NF ; x++ ) { printf(“%s “,$x) } print “” }’ | sort | tail -n20>> $OutDir/$File

ln -s 9999monitorram /etc/actions/cronfrequently/

Find Hidden Features On Chrome’s Internal Chrome:// Pages

Google Chrome’s internal chrome:// pages contain experimental features, diagnostic tools and detailed statistics. They’re hidden in Chrome’s user interface, so you have to know they exist to find them. These hidden pages are Chrome’s version of Firefox’s about: pages.

You can access each by typing chrome://, followed by the name of the page — you can also use the more traditional about: prefix, which redirects you to the chrome:// URL.

Chrome://About

The chrome://about page lists all Chrome’s internal pages. Click any of the links to access the page. Many of them are the same pages you can access from Chrome’s menus — for example, chrome://bookmarks is the bookmarks manager and chrome://settings is Chrome’s options page.

There are also a few special URLs for debugging purposes located near the bottom of the page — for example, you can enter chrome://kill in the address bar to kill the current tab.

Chrome://Flags

The chrome://flags page, formerly known as the chrome://labs page, is probably the most interesting internal page. This page contains a treasure trove of experimental features that aren’t yet enabled by default — Google warns you that your browser may “spontaneously combust” if you enable these features. They may have security, privacy or stability problems or cause data loss. Use these options at your own risk!

New features often turn up here before they become enabled by default.

For example, you can enable the “Enable NTP Bookmark Features” option to add the experimental bookmark interface to Chrome’s new tab page. The new bookmark manager isn’t enabled by default in Chrome 16, but it probably will be in a newer version. This feature has been removed from Chrome 17, which shows how quickly these flags can vanish or break.

Chrome://Sessions

Another feature buried on Chrome’s chrome://flags page is the “Enable Syncing Open Tabs” option, which adds an “Open Tabs” option to Chrome’s sync settings.

After you enable this option, you’ll find a list of your other browser sessions on the chrome://sessions page. This feature will eventually be enabled by default and exposed in Chrome’s user interface, but you can use it now.

Chrome://Memory & Chrome://Tasks

The chrome://memory page gives you a breakdown of Chrome’s memory usage. It shows you just how much memory each extension, app and web page is using. If other browsers, such as Mozilla Firefox or Internet Explorer, are running, it’ll also show the other browsers’ memory usage.

This is the same page you can access by clicking the “Stats for nerds” link at the bottom of Chrome’s Task Manager window. Open the Task Manager by right-clicking Chrome’s title bar and selecting Task Manager or access the chrome://tasks URL to open it in a tab.

Chrome://Net-Internals

The chrome://net-internals page is packed full of network diagnostic information and tools. It can capture network data and dump it to a file, making it a useful tool for troubleshooting Chrome network problems.

Most of the tools here won’t be useful to average users, but the Tests page contains a particularly useful tool. If a website won’t load, you can plug its address into the Tests page and Chrome will attempt to determine the problem for you.

Chrome://Crashes

The chrome://crashes page lists crashes that have occurred. You’ll only see crashes here if you have the “Automatically send usage statistics and crash reports to Google” option enabled on the Under the Hood tab in Chrome’s settings.

Chrome://Tracing

The chrome://tracing page is a developer tool that allows you to analyze Chrome’s performance. Click Record and Chrome will start logging browser activity.

After stopping the record process, you can dig into the activity and see what’s taking up the most time. If you have a page that performs slowly in Chrome, you can see what part of your code Chrome is struggling with.


Many of the other pages are technical pages listing debug information. For example, the chrome://flash page lists information about the flash plug-in and the chrome://sync-internals page displays the state of Chrome’s sync process. Feel free to explore the rest on your own.

5 Commands to Get Public IP using Linux Terminal

Public IP are used for communication between computers over internet. A computer running with public ip is accessible all over world using internet. So we can say that it is the identity of computer on internet. Now the question is how do we know our public ip. For computers having GUI can easily get there ips using web tools like this but how we get ips of computers having terminal access only. Solution is here – use one of the following commands to find public ip of your system using Linux terminal. These are also useful to use in shell script.

Get Public IP using Linux Terminal

Command 1. Using wget

# wget http://ipecho.net/plain -O - -q ; echo

Command 2: Using curl

# curl ipecho.net/plain; echo

Command 3: Using wget

# wget http://observebox.com/ip -O - -q ; echo

Command 4: Using curl

# curl icanhazip.com

Command 5: Using curl

# curl ifconfig.me

Get Public IP in Shell Script

We can simply use following commands in our shell script get our computers public ip and store them in a variable to use anywhere in shell script.

#!/bin/bash

PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
echo $PUBLIC_IP

You could use a DNS request instead of HTTP request to find out your public IP:

C:\> nslookup myip.opendns.com resolver1.opendns.com

It uses resolver1.opendns.com dns server to resolve the magical myip.opendns.com hostname to your ip address.

Unix version:

$ dig +short myip.opendns.com @resolver1.opendns.com

Martian Packet Messages

Occasionally, you might see messages like the following in your Linux kernel messages:

martian source 192.168.1.1 from 127.0.0.1, on dev eth1<br />
        ll header: 52:54:00:98:99:d0:52:54:00:de:d8:10:08:00

There’s a lot of discussion out there about what this means, but not a lot about how to trace down the source.  Hopefully this will provide some insight into what the messages actually mean, and how to understand them.

What are martian packets?

A martian packet is one that comes from an “impossible source”.  For example, this might be from an RFC 1918 reserved netblock routed across the internet, or any packet with a “localnet” (127.0.0.1, etc.) IP on an interface other than the loopback interface.  In other words, martian packets mean either something is misconfigured, or someone is trying to do something very sneaky.  RFC 1812 provides more detail about what is and is not a valid routable IPv4 address.

So what do those messages mean?

Let’s start with the first message: it contains the destination and source IP addresses, as set in the IPv4 packet, and the network interface on which the packet arrived.  Despite how it reads, the first address is NOT the source address in the packet — it is, in fact, the destination address!  The 2nd IP is the IP that was contained in the IPv4 packet, but this isn’t of very much help in finding the sending host — remember, the whole point of this message is an indication that the source of the packet is “suspect.”

Fortunately, the 2nd line contains a lot of information for us, but it’s printed in a format that’s very hard to read.  This is the link-layer header of the packet that contained the erroneous source address.  Almost all machines these days use physical connectivity that looks like ethernet, and the log entry above is based on an Ethernet Frame.  (PPP connections will probably look different, but I’ve never seen this message on anything but ethernet.)  The ethernet header is at least 14 bytes long, and most of us will only see 14 byte headers.  The longer headers are used with 802.1q VLANs, but most people will never see those, so we’re going to discuss only the 14 byte headers.

The 14 bytes above, printed in colon-separated hex, contain the destination MAC address, the source MAC address, and either the length or the ethertype code.  The MAC addresses are in standard notation, and the first 6 octets are the destination, with octets 7-12 representing the source.  These will help you track down where the packet came from (though it might be your upstream router, in which case you’ll need to do more investigative work).

The last 2 bytes take more explaining.  If the value is less than 0x0600, then it represents the length of the data field for 802.3 compliant ethernet frames.  Greater values, as seen above, are actually EtherType codes.  This will mostly be 0x0800, which means an IPv4 packet is contained in the data segment.  (After all, this error does come from an IPv4 issue.)  Other values might be 0x8100 (802.1q VLAN-tagged packet) or 0x0806 (ARP packet).  There are many more, but you’re unlikely to see them in this context.

Long Story Short

Look at bytes 7-12 of the link-layer header.  That’s the MAC of the last machine to touch the packet.  Look there for the cause (if it’s a router, look upstream from there, and then figure out why you’re not filtering the martian packets on your router).  Yes, the source MAC could be spoofed, but then someone’s playing games on your local network segment.  (Which probably means they have physical access, or you have a terribly designed network.  Remember, physical access=pwned.)

Basic MySQL Tutorial

The basic MySQL tutorial covers everything from managing databases, tables to querying data from tables by using various techniques such as projection, inner join, left join, etc.

Manage Database in MySQL

You will learn various statements to manage MySQL databases including creating a new database, removing an existing database, selecting a database to work with and listing all databases.

Understanding MySQL Table Types, or Storage Engines

You will learn various MySQL table types, or storage engine. It is essential to understand the features of each table type in MySQL so that you can use them effectively to maximize the performance of your databases

MySQL Data Types

This tutorial shows you various MySQL data types so that you can apply them effectively in designing your database tables.

Creating Tables Using MySQL CREATE TABLE Statement

In this tutorial, we will show you how to create a new tables in a particular database using MySQL CREATE TABLE statement.

MySQL Sequence

This tutorial shows you how to use MySQL sequence to generate unique numbers for ID columns of tables automatically using AUTO_INCREMENT attribute

MySQL Primary Key

In this tutorial, you will learn how to use MySQL primary key constraint to create the primary key for the table.

MySQL Foreign Key

This tutorial introduces you to MySQL foreign key and shows you step by step how to create, add and drop foreign keys in MySQL.

Using MySQL ALTER TABLE To Change Table Structure

In this tutorial, you will learn about the ALTER TABLE statement that changes existing table structure such as adding or removing column, changing column attribute, etc. We will also show you how to rename the table using ALTER TABLE statement.

Using MySQL DROP TABLE To Remove Existing Tables

In this tutorial, we will show you how to remove existing tables using MySQL DROP TABLE statement.

MySQL Temporary Table

This tutorial discusses about MySQL temporary table and shows you step by step how to create, use and drop temporary tables.

MySQL Managing Database Index

In this tutorial, you will learn how to work with MySQL index and how to take advantages of MySQL index to speed up the data retrieval.

Using MySQL SELECT Statement to Query Data

In this tutorial, we will show you how to use the simple SQL SELECT statement to query the data from database tables in MySQL.

Filter Rows Using MySQL WHERE

This tutorial show you how to use MySQL WHERE clause to filter rows based on specified conditions.

MySQL ORDER BY

This tutorial shows you how to sort a result by using MySQL ORDER BY clause. The custom sort order with the FIELD function will be covered.

How to Use MySQL DISTINCT to Eliminate Duplicate Rows

In this tutorial, you will learn how to use MySQL DISTINCT with SELECT statement to eliminate duplicate rows in a result set.

Using MySQL LIMIT

In this tutorial, you will learn how to use MySQL LIMIT clause to select rows from the beginning, middle and end of a result set.

Querying Data with MySQL IN Operator

You will learn how to use MySQL IN operator that determines if a specified value matches any value in a list or a subquery.

MySQL BETWEEN Operator Explained

In this tutorial, you will learn how to use MySQL BETWEEN operator to specify a range to test.

Using MySQL LIKE Operator to Select Data Based On Patterns

Shows how to use MySQL LIKE operator to select data based on pattern matching.

MySQL Alias

This tutorial shows you how to use MySQL alias to improve the readability of the queries.

Combining Result Sets by Using MySQL UNION

This tutorial shows you how to use MySQL UNION to combine two or more than two result sets from multiple SELECT statements into a single result set.

MySQL INNER JOIN

In this tutorial, you will learn how to use MySQL INNER JOIN clause to select data from multiple tables based on join conditions.

MySQL LEFT JOIN

Helps you understand MySQL LEFT JOIN concept and how to apply it to query data from two or more database tables.

MySQL Self Join

In this tutorial, you will learn how to use MySQL self join to join a table to itself using join statement.

MySQL GROUP BY

In this tutorial, you will learn how to use MySQL GROUP BY to group rows into subgroups based on columns or values returned from an expression.

MySQL HAVING

In this tutorial, you will learn how to use MySQL HAVING clause to specify a filter condition for groups of rows or aggregates.

MySQL Subquery

In this tutorial, we will show you how to use MySQL subquery to write more complex queries. In addition, we will show you how to use correlated subquery

Inserting Data into Tables Using MySQL INSERT Statement

In this tutorial, you will learn how to use various forms of MySQL INSERT statement to insert data into database tables.

Updating Data Using MySQL UPDATE Statement

You will learn how to use UPDATE statement and its options to update data in database tables.

MySQL UPDATE JOIN

This tutorial shows you how to perform cross table update by using MySQL UPDATE JOIN statement with INNER JOIN and LEFT JOIN.

Using MySQL DELETE to Remove Data from Tables

This tutorial shows you how to use the MySQL DELETE statement to remove data from one or more tables.

MySQL ON DELETE CASCADE Deletes Data From Multiple Tables

Learn how to use MySQL ON DELETE CASCADE referential action for a foreign key to delete data from a child table automatically when you delete data from a parent table.

MySQL DELETE JOIN

This tutorial shows you how to delete data from multiple tables by using MySQL DELETE JOIN statement with INNER JOIN and LEFT JOIN.

MySQL TRUNCATE TABLE

This tutorial shows you how to use the MySQL TRUNCATE TABLE statement to delete all data in a table.

MySQL TIMESTAMP

This tutorial introduces you to MySQL TIMESTAMP and its features called automatic initialization and automatic update that allow you to define auto-initialized and auto-updated columns for a table.

MySQL Replace

In this tutorial, you will learn how to use MySQL REPLACE statement to insert or update data.

MySQL Transaction

In this tutorial, you will learn what MySQL transactions are and how to use MySQL COMMIT and MySQL ROLLBACK to manage transaction in MySQL.

MySQL Table Locking

In this tutorial, you will learn how to use MySQL locking for cooperating table access between sessions.MySQL allows a client session to acquire a table lock explicitly for preventing other sessions from accessing the table during a specific period….

MySQL Prepared Statement

We will show you how to use MySQL prepared statement to execute a query with placeholders to improve the speed of the query and make your query more secure.

MySQL Character Set

This tutorial discusses about MySQL character set and shows you step by step how to perform various operations on character sets.

MySQL Collation

This tutorial discusses about MySQL Collation and shows you how to set character set and collations for the MySQL server, database, tables and columns.

Restore Your USB Key to it’s original state (Format Linux USB on Windows)

Restore Your USB Key to it’s original state

After having tooled around with a USB Linux version using your image overwritten or multi partitioned flash pen drive, you might find it necessary to revert it back to a single fat partition (restore the flash pen drive to it’s original state) that can again be read by all computers. Windows users can follow the Windows instructions below to Restore a Flash Drive using the HP USB Format Tool. For those working from Linux this task can easily be accomplished via the Linux Flash Drive Restoration tutorial that follows.

Restoring your USB key to it’s original state using Windows/Mac OS:

Windows and or Mac OS users could use the SD Formatter Tool to reformat and restore a USB Drive.

SD Formatter

Distributing Smoothwall’s (https) mitm self-signed CA certificate to BYOD (mobile) client devices (unmanaged device other than Windows clietns)

If you wish to use ‘decrypt and inspect’ for BYOD devices, the users will need to have the Smoothwall’s HTTPS MiTM CA cert sent out to them so that they can install it on their systems.

The best ways to get this CA to the clients is to either:

– Email the CA to them directly
or
– Provide a download link to the CA so the users can download it themselves directly.

Then, according to their devices instructions, they need to install the CA so it can be trusted.

(It isn’t possible to use a normal real world certificate for the MiTM as this requires a full certificate authority)

Smoothwall IPSec (Openswan) VPN to Microsoft Azure (Site-to-Site VPN)

Smoothwall can only be set to use Diffie Hellman group 5 in Phase 1 when initiating the VPN, however when offered by the other device the smoothwall can downgrade to DH2.

All the other encryption settings can be done on the smoothwall. So when setting up the connection on the smoothwall’s end, it would look something like this:

Authenticate by: preshared key
Use comrpession – off
Initiate the connection – off
Perfect forward secrecy – off
Authentication type: ESP
Phase 1 cryptograhic algo: AES256
Phase 1 hash algo: SHA
Phase 2 cryptograhic algo: AES256
Phase 2 hash algo: SHA

Key life: 480 mins
IKE lifetime: 60 mins

These settings would need to be set the same on the Azure gateway, and it would need to be set up as the initiator

Ifconfig: 7 Examples To Configure Network Interface

Ifconfig command is used to configure network interfaces. ifconfig stands for interface configurator. Ifconfig is widely used to initialize the network interface and to enable or disable the interfaces.

In this article, let us review 7 common usages of ifconfig command.

1. View Network Settings of an Ethernet Adapter

Ifconfig, when invoked with no arguments will display all the details of currently active interfaces. If you give the interface name as an argument, the details of that specific interface will be displayed.

# ifconfig eth0

eth0   Link encap:Ethernet  HWaddr 00:2D:32:3E:39:3B
inet addr:192.168.2.2  Bcast:192.168.2.255  Mask:255.255.255.0
inet6 addr: fe80::21d:92ff:fede:499b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:977839669 errors:0 dropped:1990 overruns:0 frame:0
TX packets:1116825094 errors:8 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2694625909 (2.5 GiB)  TX bytes:4106931617 (3.8 GiB)
Interrupt:185 Base address:0xdc00

2. Display Details of All interfaces Including Disabled Interfaces

# ifconfig -a

3. Disable an Interface

# ifconfig eth0 down

4. Enable an Interface

# ifconfig eth0 up

5. Assign ip-address to an Interface

Assign 192.168.2.2 as the IP address for the interface eth0.

# ifconfig eth0 192.168.2.2


Change Subnet mask of the interface eth0.

# ifconfig eth0 netmask 255.255.255.0


Change Broadcast address of the interface eth0.

# ifconfig eth0 broadcast 192.168.2.255


Assign ip-address, netmask and broadcast at the same time to interface eht0.

# ifconfig eth0 192.168.2.2 netmask 255.255.255.0 broadcast 192.168.2.255

6. Change MTU

This will change the Maximum transmission unit (MTU) to XX. MTU is the maximum number of octets the interface is able to handle in one transaction. For Ethernet the Maximum transmission unit by default is 1500.

# ifconfig eth0 mtu XX

7. Promiscuous mode

By default when a network card receives a packet, it checks whether the packet belongs to itself. If not, the interface card normally drops the packet. But in promiscuous mode, the card doesn’t drop the packet. Instead, it will accept all the packets which flows through the network card.


Superuser privilege is required to set an interface in promiscuous mode. Most network monitor tools use the promiscuous mode to capture the packets and to analyze the network traffic.


Following will put the interface in promiscuous mode.

# ifconfig eth0 promisc


Following will put the interface in normal mode.

# ifconfig eth0 -promisc