Category: Linux

BASH : Delete the last X chars from a string

When you scrap data in HTML , it’s common , depending on the quality of you regex to end up with a string with some useless character at the end (Or the Beginning),

If you string looks like that :

My useful data</a> <

You can use the command head to drop the X last character of your string ,
So in my Case , to Keep only the string My useful data and drop the </a> <

I’m going to pipe my string to

head -c -6

This will delete the last 6 chars of my string , giving the data that i need later in my script

 

 

Linux : Sort a list of IP address

like we saw previously in that article , it’s possible to ping an entire /24 subnet with a little convenient command.

64 bytes from 192.168.1.21: icmp_seq=1 ttl=64 time=0.163 ms
64 bytes from 192.168.1.31: icmp_seq=1 ttl=64 time=0.360 ms
64 bytes from 192.168.1.8: icmp_seq=1 ttl=128 time=66.8 ms
64 bytes from 192.168.1.32: icmp_seq=1 ttl=64 time=25.1 ms
64 bytes from 192.168.1.42: icmp_seq=1 ttl=255 time=1.93 ms
64 bytes from 192.168.1.46: icmp_seq=1 ttl=255 time=60.8 ms
64 bytes from 192.168.1.41: icmp_seq=1 ttl=255 time=67.0 ms
64 bytes from 192.168.1.254: icmp_seq=1 ttl=64 time=0.344 ms

 

The Problem is , the IP adresse are going to be displayed in the order they answer the ping , and some devices are going to answer faster than other , making a list that is not in order

the sort command in her default setting will sort using the first changing character , which is going to give me a list like that : 21, 254, 31, 32, 41, 42, 46, 8

If you have installed on you host a recent version of the sort command , you will be able to use sort -V
but on older and some embedded devices the -V option is not available ,

You can then launch the sort command with these options :

sort -n -t . -k 4,4

the result will be :

64 bytes from 192.168.1.8: icmp_seq=1 ttl=128 time=0.978 ms
64 bytes from 192.168.1.21: icmp_seq=1 ttl=64 time=0.246 ms
64 bytes from 192.168.1.31: icmp_seq=1 ttl=64 time=0.383 ms
64 bytes from 192.168.1.41: icmp_seq=1 ttl=255 time=76.5 ms
64 bytes from 192.168.1.42: icmp_seq=1 ttl=255 time=2.47 ms
64 bytes from 192.168.1.46: icmp_seq=1 ttl=255 time=65.4 ms
64 bytes from 192.168.1.254: icmp_seq=1 ttl=64 time=0.290 ms

 

if you need to sort a /16 list of ip address :

you can just add -k 3,3 to the command : sort -n -t .-k 3,3 -k 4,4

Display your current external IP address with WGET

To know your ip address , you can use the command ifconfig
You will know the ip address for each of your network interfaces.

But if you are not directly connected to the internet and use a Gateway that’s nating the internet traffic for you , ifconfig is not going to be helpful to know the ip that connect you to the internet.

with this command the current external ip address associated with you default gateway will display

wget -qO - ident.me

bash : Put the source of a webpage in a variable

When you do some bash scripting , this is often useful to get data from  webserver because they are a very simple way to exchange data from computer from computer.
to put the data that’s available on a webserver into a $var you just have to use the command :

 var=$(wget -qO- http://server.com/file.htm)

this will fetch the data from http://server.com/file.htm in a non verbose way and put it in variable named var .

you can now do whatever you want with that $var , like greping it for example.

 

Extract values separated by character ( : , . – )

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

Hello:345:18.5:TRUE:000

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

echo $var | awk -F’:’ ‘{print $3}’

Will return the value 18.5

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

echo $var | awk -F’:’ ‘{print $2-$3}’

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 :

mount

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 :

umount /media/flash

then you need to

dd bs=4M if=debian-8.7.1-amd64-netinst.iso of=/dev/sda

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 -e "s/word to delete//g"

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 :

echo $visitor | sed -e "s/current number of users: //g"

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

dhclient -r

to release the address currently associated to you
then just launch

dhclient

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

dhclient -r wlan0
dhclient wlan0

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

dhclient -r -v wlan0
dhclient -v wlan0

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.