Extract values separated by character ( : , . – )

In a bash script if you find yourself with values presented that way

you might want to keep only the second or third of this values that are separated by the : character

like always , in bash there is a lot of ways to accomplish that , but i like to use a one that use the AWK utility because it’s very easy to read and understand.

my list of value is contained in a var name $var let’s extract the third value which is 18.5

Will return the value 18.5

but i can do some math operation in the utility, like :

this will return 326.5 the result of 345 – 18.5

awk is a fast and powerful text editor , there is a lot things you can do with it.

Linux : make an USB bootable thumb with an ISO file

you downloaded the iso of some distrib and you want to make a flash thumbdrive bootable by writing the ISO on the thumb ,

a simple copy and paste is not going to work :

On linux , there is a very simple command line utility to do just that.

first plug your usb thumbdrive on your computer , it will probably automount.

you need to unmount that drive before continuing.

to find out the name of thumbdrive , you can type :

You will get a result that look like that

/dev/sda1 on /media/flash type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=ascii,shortname=mixed,errors=continue)

my flash drive is mounted at /media/flash  the name of the device is /dev/sda

to unmount the drive , jsut do a :

then you need to

after a little while , the utility will exist

your thumbdrive is now a bootable thumbdrive.

Using sed to delete a word from a string

For example in a bash script you need to delete one or more word from the string , you can use sed to do just that

sed is searching for “word to delete” and replacing it with nothing ,

you can of course pipe to that command ,

example :

the var $visitor contain the string : ‘current number of users: 234’

But only the number (234) is usefull to you ,

your can do :

the result is going to be : 234

 

Renewing your IP address on Linux with dhclient

Let’s say you need to change you ip address because there was a change on the network and now ip settings changed,
you can at anytime request with the command : ‘dhclient‘ a new address to a dhcp server ,

if you only have a single network adapter, the command is very simple
just launch the command

to release the address currently associated to you
then just launch

and you computer will broadcast a dhcp request
if you have multiple adapter ,
you can do

dhclient wlan0[/code]
to spécify that you want a new ip address for you wlan0 adapter
by default the command dhclient is silent , meaning that you will not get any information about what the dhcp server answered.
to add verbosity to the command , just add a -v parameter

The new Orange pi  Zero Plus 2 |allwinner h3, 512mb, 4K hdmi, 8gb emmc

orange pi is an awesome sbc manufacturer known for excellent hardware and awful software support. 

I’m a very happy owner of the first generation orangepi zero witch I use as a headless Linux server on my network. 

Today orangepi release a new version of the orange pi zero called the “orange pi zero 2 plus” the cpu goes from the H2+ to the H3, the wlan controller is better, you get a csi camera interface. And 8gb onboard mmc flash. 

Also the board gain an hdmi port witch enable easy video output to a screen or a TV. 

The board also gain bluetooth support if that something that’s useful to you. 

Overall its seems the addition of the hdmi port is at the expense of the disappearance of the ethernet port witch was for me one of the best feature of the first edition of the orange pi zero. 

The board is made for a different purpose, the 2 plus is going to be ideal for a very small media player or a emulator gaming device. Since the video output and the 4k capability are adding  this kind of possibilities 

The board is also much more expensive than its predecessor at 18$90 compared to the original 512MB orange pi zero at 8$99.

Since I don’t require a video output the original orangepi zero suits better my purpose, but for most people a video output is mandatory, 

The orange pi zero 2 plus is going to be be an excellent board once the armbian team release an image for it. 

Ping a complete /24 subnet with one bash line

if you need to know what devices are currently connected on the same network as you ,
there is no very simple way ,

you could use the command arp -a to see your arp table but it’s not always up to date and can have false entries.

using the broadcast option is never working for me ,  nmap is slow and only show the results when the scan il finished and it’s not installed by default on most distrib.

Most ping loop to cycle through all available address on the network are going to deal with the high minimum timeout time of 1 second before trying the next address.
the Best solution is to launch the ping command in parallel , you could use xarg -P to do that , but xargs is not present on lightweight linux distribs like OpenWRT.

 

using that command , you will launch in parallel 255 time the program ping , ping an address from 192.168.1.1 to 192.168.1.254 with only one paquet and a timeout of 1 second.

the answer you will get will look like that

this loop use only very simple commands included in most linux operating systems

Linux epoch time or Unix time


If you need a convenient way to store the date in a database you will certainly use the unix format.
it’s very convenient because it doesn’t change notation from country to country.

You can later convert it back to a format that’s human readable.

in a shell on linux if you wish to display the current unix time. just type

date +%s

But if you need the unix time for a specific date ,

date –d=’03/14/2016 00:00:01′ +%s
or
date –d=’14 march 2016 00:00:01′ +%s

There is also the function to input an interval

date –d ‘1 hour ago’ +%s
date –d ‘1 month ago’ +%s

 

Grep a line before or after a match in bash

Sometime when you grep a file on Linux you need to not only display the line where the result is found but also several lines before or after the match.

Grep as a useful function permitting you to do just that.

Let’s take an example file.

file.txt:

Title : login issue
Ticket number : 16417
Details : user password is no longer working.

Title : Broken keyboard
Ticket number : 16418
Details : the space key on the user keyboard no longer work

 

If the file is list of hundreds of this informations and you need to see only the information related to ticket 16417 you need to

cat file.txt|grep 16417

Will only return the result Ticket number : 16417  living out the useful informations

If you need to display a line after the grep match

cat file.txt| grep -A1 16417

will return :

Ticket number : 16417
Details : user password is no longer working.

But there is also useful information before  the match

You can associate the 2 options to display the line after and before

cat file.txt| grep -A1 -B1 16417

this will return all the information i need :

Title : login issue
Ticket number : 16417
Details : user password is no longer working.

Mount a network share on linux

On linux you have the ability to mount network share as they were part of your local storage ,
On debian , you need to have a package named cifs-utils specialised to mount network share.

after installation of the package using the command :

apt-get install cifs-utils

You have to create a folder to attach the mount to

mkdir /media/netshare
mount -t cifs "//host-of-the-share/disk/" /media/netshare -o guest,iocharset=utf8,file_mode=0777,dir_mode=0777

if you have to identify yourself to use the network share the command is slightly different :

mount -t cifs "//host-of-the-share/disk/" /media/netshare -o user=username,iocharset=utf8,file_mode=0777,dir_mode=0777

the system should ask you for your password

to check if you mount was successfull , you can check the mount using

df

Change the default SSH port on your linux server

If you run a linux server with with a ssh service on the TCP port 22 , you are likely to get bombarded by automated login attempt by hundreds and hundreds of bots
You can look at the file  getting created by launching

tail -f /var/log/auth.log | grep Failed

the first thing you can do to stop that is to change the default port user by the SSH service on you linux server

for this you just have to edit the config file : /etc/ssh/sshd_config

Find the following lines

 # What ports, IPs and protocols we listen for
Port 22

And put the new port on which you would like to run your OpenSSH server.

After this little change restart the ssh service with the command

service ssh restart

You should see that the numerous and constant attacks sould instantly stop.

Security by obscurity is still security.