msgbartop
MAC OS X, Linux, Windows and other IT Tips and Tricks
msgbarbottom

31 Jul 15 Encrypt a password with different encryption methods

Situation:
I happen to have configured a Linux system with MySQL database and wanted to enter a password in the password field in the DB. Here is a way I found.
This will output in the terminal the encrypted string you can then enter directly in the password field via phpmyadmin or mysql client.
Note: The encryption method may differ between Linux distributions and versions. To find it out, you should be able to get it with this command:
grep "^ENCRYPT_METHOD" /etc/login.defs | awk '{print $2}'

Encryption with SHA512 method: (best security)
(default method used by Ubuntu 14.04-2)
echo "<username>:<clear text password>" | chpasswd -S -c SHA512 | cut -d: -f2

Encryption with SHA256 method: (little less security)
echo "<username>:<clear text password>" | chpasswd -S -c SHA256 | cut -d: -f2

Encryption with MD5 method: (no more secure !!!)
echo "<username>:<clear text password>" | chpasswd -S -c MD5 | cut -d: -f2

Encryption with DES method: (really no more secure !!!)
echo "<username>:<clear text password>" | chpasswd -S -c DES | cut -d: -f2

Note: If you need to include such command in a script to change the password of a user in the Linux system, just do as follows:
echo "<username>:<clear text password>" | chpasswd -c SHA512

09 Jun 15 Downloading tar.gz files from the Linux command line

Sometimes we need to download a file from Internet using the bash command line. here are some suggestions:
Using WGET:
wget {URL}
eg.
wget https://my.server.com/downloads/file.tar.gz

Using CURL:
(Fancy progress info given as the download progresses and will unpack it at the same time)
curl -L --progress {URL} | tar xz
eg.
curl -L --progress https://my.server.com/downloads/file.tar.gz | tar xz

22 May 15 Extending dynamically Linux RAMs in VMWare VM without rebooting

Situation:
Need to raise the amount of RAM in a VMWare VM without rebooting.

Solution:
– In VMWare interface: Raise the amount of RAM for the VM
– In the Linux VM: Run the following script:

#!/bin/bash
# This script enables in system the unrecognized RAMs
deleteline () {
echo -ne $dellineup
}
### check preconditions ###
if ! type -P $modprobe > /dev/null; then
echo -e "'modprobe' package is not installed! \n z.B. apt-get install modprobe"
fi
### check if there is any offline RAM ###
RAMOFFLINE=`grep offline /sys/devices/system/memory/*/state|wc -l`
if [ $RAMOFFLINE -gt 0 ]; then
echo "RAMs found that are not yet recognized by the system. Enable RAMs live recognition? (y/N)" ; read yesno ; deleteline
case "$yesno" in
[yY])
echo -e "Recognition of unused RAMs will now be enabled"
modprobe acpiphp
modprobe acpi_memhotplug
for i in $(grep -l offline /sys/devices/system/memory/*/state);do echo online > $i;done
echo -e "\n\n"
free -m
;;
*)
echo -e "Process cancelled"
;;
esac
else
echo "No Unrecognized RAMs present."
fi

Check the new amount of RAM:
free | grep Mem

XEN NOTE: With XEN environment it appears that the System/Kernel immediately recognizes the new amount of RAM dynamically without the need to run this above script.

11 May 15 Copying all files including hidden files in Linux command

PROBLEM:
If you use the command cp or mv on hidden files you will notice that the hidden files won’t get ‘seen’ and therefore not copied or moved. The problem doesn’t belong to cp or mv but to bash. Bash doesn’t include the hidden files in the globbing expansion.
for example:
mkdir ~/temp1 ~/temp2
touch ~/temp1/.file1 ~/temp1/file2
cp -a ~/temp1/* ~/temp2/
ls -la ~/temp2/
total 16
drwxrwxr-x 2 michel michel 4096 Mai 11 17:58 .
drwxr-xr-x 117 michel michel 12288 Mai 11 17:58 ..
-rw-rw-r-- 1 michel michel 0 Mai 11 17:58 file2

The file ~/temp/.file1 was not included in the copying.

SOLUTION: (one of them…)
Setting the bash option which will include all the files including the hidden ones in the globbing expansion.
shopt -s dotglob # for considering dot files (turn on dot files)
and
shopt -u dotglob # for don't considering dot files (turn off dot files)

Example:
shopt -s dotglob
cp -a ~/temp1/* ~/temp2/
shopt -u dotglob
ls -la ~/temp2/
#
total 16
drwxrwxr-x 2 michel michel 4096 Mai 11 18:01 .
drwxr-xr-x 117 michel michel 12288 Mai 11 17:58 ..
-rw-rw-r-- 1 michel michel 0 Mai 11 17:58 .file1
-rw-rw-r-- 1 michel michel 0 Mai 11 17:58 file2

31 Mar 15 Monitoring latency time of http requests

Here is a simple but useful command which shows the latency time of http requests. You can adjust the delay between repeats as well as the URL being queried.
Reference: http://www.shellhacks.com/en/Check-a-Website-Response-Time-from-the-Linux-Command-Line

host="www.google.de"; delay=5; while true ; do echo -n "Response time for http://$host:" ;curl -s -w %{time_total}\\n -o /dev/null http://$host ;sleep $delay; done

Results:
Response time for http://www.google.de:0,025
Response time for http://www.google.de:0,024
Response time for http://www.google.de:0,024
Response time for http://www.google.de:0,024
Response time for http://www.google.de:0,024
Response time for http://www.google.de:0,026
Response time for http://www.google.de:0,024
Response time for http://www.google.de:0,024
Response time for http://www.google.de:0,024
.......

ADVANCED:
Here is a more advance version which performs more timing tests:

host="www.google.de"; delay=5; while true ; do echo "------"; curl -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null http://$host; sleep $delay; done

Results:
Lookup time: 0,002
Connect time: 0,011
PreXfer time: 0,011
StartXfer time: 0,022
.
Total time: 0,023
------
.
Lookup time: 0,001
Connect time: 0,012
PreXfer time: 0,013
StartXfer time: 0,023
.
Total time: 0,023
------
.......

Meanings:
Lookup time: The time, in seconds, it took from the start until the name resolving was completed.
Connect time: The time, in seconds, it took from the start until the TCP connect to the remote host was completed.
PreXfer time: The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all ‘pre-transfer’ commands and negotiations that are specific to the particular protocol(s) involved.
StartXfer time: The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes ‘time_pretransfer’ and also the time the server needed to calculate the result.

20 Mar 15 Resolve the IP from a hostname

As a Linux administrator I often need to resolve hostnames into IPs specially in scripts. Using the command ‘host’ the sytem will resolve the IP only through the resolver libraries omitting to look into the /etc/hosts file. The same is true for the command ‘nslookup’ and ‘dig’. A good trick is to use the command ‘ping’ which starts the resolving at the /etc/hosts and then the resolver libraries. But ping is meant to send a ping (at least one) and what if don’t want to send a ping. Here is a good suggestion for it based on:
http://unix.stackexchange.com/questions/20784/how-can-i-resolve-a-hostname-to-an-ip-address-in-a-bash-script

All following commands will resolve an IP address if the host still exist according to the configuration in /etc/nsswitch.conf which generally means the following lookup sequence:
– Look in /etc/hosts
– If not found in /etc/hosts, then query the system resolver libraries which ask the configured DNSs in /etc/resolv.conf.

If host points to CNAME it will also get the IP in that case.

Resolves to IPv4 address:
getent ahostsv4 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

Resolves to IPv6 address
getent ahostsv6 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

Will return the owners preferred address what may IPv4 or IPv6 address:
getent hosts google.de | head -n 1 | cut -d ' ' -f 1

Getting the zone info for a domain

dig +nocmd {domain} any +multiline +noall +answer [@NAMESERVER]
eg.
dig +nocmd itmatrix.de any +multiline +noall +answer @nsc1.schlundtech.de.

22 Jan 15 Changing keyboard layout in Ubuntu/Debian Linux comand line

Ref: http://askubuntu.com/questions/209597/how-do-i-change-keyboards-from-the-command-line

You can find all the different keymaps in the following location:
/usr/share/keymaps/i386/ or /usr/share/kbd/keymaps/i386/
To change the keyboard layout (e.g. to German) in the Linux command line, type the following command:
loadkeys de
For X:
setxkbmap de
To make these changes system wide, assuming you’re using Ubuntu, you can use the following:
sudo dpkg-reconfigure console-setup

25 Sep 14 Unlocking dpkg database

Under Debian Squeeze I ran the command
dpkg -i bash_4.1-3+deb6u1_amd64.deb
and got the following error message:
dpkg: status database area is locked by another process

Solution:
1 – make sure you are not already runnning any package administration program in another bash session like with dpkg or apt-get or aptitude
2 – If no other package management program is concurrently running then delete the lock file with the following command:
rm /var/lib/dpkg/lock
You will then again be able to use package administration programs.

25 Sep 14 Bash bug ‘Shellshock’ Debian Squeeze packages

For those who still have Debian Squeeze and wonder where to find the fixed Bash Debian package for the dangerous bash bug (http://www.bbc.co.uk/news/technology-29361794)you can find it here:
64 Bit:
ftp://ftp.fr.debian.org/debian/pool/main/b/bash/bash_4.1-3+deb6u2_amd64.deb
32 Bit:
ftp://ftp.fr.debian.org/debian/pool/main/b/bash/bash_4.1-3+deb6u2_i386.deb

Want to test your Bash to see if it is fixed? Run the command:
test="() { echo Hello; }; echo Buggy" bash -c ""
Nothing should appear as result if it’s fixed,
and the word ‘Buggy’ will appear if bash is not yet fixed.

For Debian Wheezy the upgrade is simple:
apt-get update && apt-get install bash

In case for some obscure reason you are still running Debian Lenny (Not recommended) and want to fix bash then use the lastest Squeeze compiled version which has all its libraries compiled with it,. It is called bash-static and it is found at:
ftp://ftp.fr.debian.org/debian/pool/main/b/bash/bash-static_4.1-3+deb6u2_amd64.deb

11 Jun 13 Converting Windows text files to Linux

Here is a small overview of how (in)compatible computer operating systems are.
Normal text files end the lines as follows:
Windows: CR+LF
Linux: LF
OS X: CR

In order to convert line ends here are few tricks I extracted from this page(many thanks to the author):
http://kb.iu.edu/data/acux.html

dos2unix and unix2dos

The utilities dos2unix and unix2dos are available for converting files from the Unix command line.
In Debian install the package dos2unix (apt-get install dos2unix)
The package comes with the following tools:
dos2unix
unix2dos
mac2unix
unix2mac

To convert a Windows file to a Unix file, enter:
dos2unix winfile.txt unixfile.txt
To convert a Unix file to Windows, enter:
unix2dos unixfile.txt winfile.txt

tr

You can use tr to remove all carriage returns and Ctrl-z ( ^Z ) characters from a Windows file:
tr -d '\15\32' < winfile.txt > unixfile.txt
However, you cannot use tr to convert a document from Unix format to Windows.

awk


To use awk to convert a Windows file to Unix, enter:
awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt
To convert a Unix file to Windows, enter:
awk 'sub("$", "\r")' unixfile.txt > winfile.txt
Older versions of awk do not include the sub function.
In such cases, use the same command, but replace awk with gawk or nawk.

Perl


To convert a Windows text file to a Unix text file using Perl, enter:
perl -p -e 's/\r$//' < winfile.txt > unixfile.txt
To convert from a Unix text file to a Windows text file, enter:
perl -p -e 's/\n/\r\n/' < unixfile.txt > winfile.txt
You must use single quotation marks in either command line. This prevents your shell from trying to evaluate anything inside.

vi


In vi, you can remove carriage return ( ^M ) characters with the following command:
:1,$s/^M//g
Note: To input the ^M character, press Ctrl-v , and then press Enter or return.

In vim, to convert to Unix; use :
set ff=unix
to convert to Windows, use:
set ff=dos