Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Saturday, March 27, 2021

BASH tricks with strings

I've been trying to leave Bash Scripting for a long time, favoring Python scripting. But when I guess I can't go further with Bash scripting, it just brings me something cool and new!

I must accept ICMP from a few OVH IPs, and among them are 2 IPs from my server network, where the last octet will be 250 and 251. To make it easier to understand, suppose that my server IP is a.b.c.d. So I must ACCEPT ICMPs from a.b.c.250 and a.b.c.251.

So, this is the amazing solution Bash Scripting offers me:

# ==========================================

# Dedicated Server IP external interface

IF_EXT="enp2s0"    # define here your external interface, such as eth0, vmbr0, etc

IP_EXT=$(ip -4 a|grep "${IF_EXT}$"|awk '{print $2}'|cut -d/ -f1)


# OVH monitoring IPs array

#    Here is the thing: the last item of this array!

IP_MONITORING=(37.187.231.251 151.80.231.244 151.80.231.245 151.80.231.246 151.80.231.247 213.186.33.62 92.222.184.0/24 92.222.185.0/24 92.222.186.0/24 167.114.37.0/24 213.186.45.4 213.251.184.9 37.59.0.235 8.33.137.2 213.186.33.13 213.186.50.98 ${IP_EXT%.*}.25{0,1})


# And finally the iptables command

for i in $(seq 0 $[${#IP_MONITORING[@]}-1]) ; do

        iptables -A INPUT -p icmp -s ${IP_MONITORING[$i]} -d ${IP_EXT} -j ACCEPT

done

# ==========================================


Notes:

- Obviously, you must run this script as root!

- I know Python would do it too. What I didn't know is that Bash could carry it out so "gracefully"!!

- I welcome any suggestion that may improve this code!!!


I hope it can help somebody out there. If it does, make me aware of it!

Wednesday, December 21, 2016

Bash Script Check

You like to Bash Script, and would like someone to review your code?

KoalaMan is constantly working on such a project, and it looks to be very good! You just have to paste your Bash Script at the left box and it will be evaluated. Easy like that!

That's my example, a simple BashScript that does nothing. But KoalaMan implemented classic Bash scripting rules to it. Awesome!

This is a "MustHaveInMyList" link: http://www.shellcheck.net/


No automatic alt text available.

Saturday, June 20, 2015

Installing Java 8 in Ubuntu

I have another post here about how to do this. But it is outdated, and I decided to rewrite it.

There are more simple ways to have Java installed on your Linux. But I do prefer do make it step by step, in the command line. Let's suppose that we are installing version 8.45 of Java
  1. Download Java 8 JDK from Oracle. Accept it's License Agreement, and select jdk-8u45-linux-x64.tar.gz to be downloaded, if your system is 64bits or jdk-8u45-linux-i586.tar.gz if it is 32 bits.
    If you are in doubt about your system, you may confirm with the following command:


    uname -m

    If the answer is x86_64, you have a 64 bits Ubuntu. If the answer is  i686, you have a 32 bits Ubuntu.
  2. Create a variable jv that will point to the installation directory of Java. By downloading the jdk-8u45-linux-x64.tar.gz, we will have /usr/lib/jdk1.8.0_45 as the installation directory.
    We need to create another variable ub, that will point to the the system binaries.
    We do that with the following command:

    jv="/usr/lib/jdk1.8.0_45"
    ub="/usr/bin"
    ATTENTION:
    Not only copy and paste above lines.
    Update Java version in variable jv.
     
  3. I usually do not use the previous versions, so I do remove them Since I always install them in /usr/lib, I will remove them with the following command:
    sudo rm -rfv /usr/lib/jdk* /usr/lib/jvm/jdk*
    

    It is important to repeat to the novice: The command above wil permanently delete the contents of
     /usr/lib/ e /usr/lib/jvm/ that start with jdk. Be sure you do not have in there any other files or directories that may be deleted.
    ATTENTION:
    In case you are not sure whether you may delete this files or not, just do not do it.
      
  4. Open the file you have downloaded from Oracle in /usr/lib/:

    sudo tar xf jdk-8u45-linux-x64.tar.gz -C /usr/lib/
  5. Update alternatives system with the binaries of this version of Java:

    sudo update-alternatives --install ${ub}/java java ${jv}/bin/java 1
    sudo update-alternatives --install ${ub}/jcontrol jcontrol ${jv}/bin/jcontrol 1
    sudo update-alternatives --install ${ub}/javac javac ${jv}/bin/javac 1
    sudo update-alternatives --install ${ub}/javaws javaws ${jv}/bin/javaws 1
    sudo update-alternatives --install ${ub}/keytool keytool ${jv}/jre/bin/keytool 1
    sudo update-alternatives --install ${ub}/pack200 pack200 ${jv}/jre/bin/pack200 1
    sudo update-alternatives --install ${ub}/rmid rmid ${jv}/jre/bin/rmid 1
    sudo update-alternatives --install ${ub}/rmiregistry rmiregistry ${jv}/jre/bin/rmiregistry 1
    sudo update-alternatives --install ${ub}/unpack200 unpack200 ${jv}/jre/bin/unpack200 1
    sudo update-alternatives --install ${ub}/servertool servertool ${jv}/jre/bin/servertool 1
    sudo update-alternatives --install ${ub}/tnameserv tnameserv ${jv}/jre/bin/tnameserv 1
    sudo update-alternatives --install ${ub}/jexec jexec ${jv}/jre/lib/jexec 1

    The following command only applies to 32 bits desktop systems:

    sudo update-alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so ${jv}/jre/lib/i386/libnpjp2.so 1

    The following command only applies to 64 bits desktop systems:

    sudo update-alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so ${jv}/jre/lib/amd64/libnpjp2.so 1
     
  6. In case of having more than one Java version installed, only one will be the default version. The following command will perform this action:

    for i in keytool jcontrol pack200 rmid rmiregistry unpack200 servertool tnameserv jexec java javac mozilla-javaplugin.so javaws; do sudo update-alternatives --config ${i}; echo; done

    The above instruction will list all available versions of each binary, if they exist. Choose those listed in step 2. If not, you will obviously have nothing to choose.
After that, we should test the installation to confirm that it is all ok:
java -version
javac -version
Now, we need to set the environment variables correctly. I adapted it from WikiHow. You must edit /etc/profile, adding the following lines by the end of it:
# Environment Java Variables defined IF Java is listed in "alternatives"
if [ -h /etc/alternatives/java ]; then
     a=$(/bin/ls -l /etc/alternatives/java | cut -d \> -f2|cut -d' ' -f2)
     export JAVA_HOME=${a%/bin/java}
     export PATH=${PATH}:${JAVA_HOME}/bin
fi
After editing /etc/profile, we need to close this user section and open it again. Finally, to check it use the following commands:
echo $JAVA_HOME
You will have /usr/lib/jdk1.8.0_45 as answer, according to the version you installed.
echo $PATH
Will give you the whole PATH, and by the end the Java directory. On mine the answer was /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jdk1.8.0_45/bin


Most of this article was written after studying Ubuntu Documentation and resolving some problems on Tomcat7 instalation at a Ubuntu Server.

If you liked it, please write a comment or click on "+1" button.

Saturday, March 30, 2013

Ubuntu MD5SUM Hashes

Have you ever needed to check MD5SUM hashes of any downloaded file? Ubuntu ISOs can always be checked here:
https://help.ubuntu.com/community/UbuntuHashes
After downloading your ISO file, you should always check it's MD5 hash:

$ md5sum Documentos/ISOs/lubuntu-12.10-desktop-*
bddb521817360540c0e54616427c003e  lubuntu-12.10-desktop-amd64.iso
a7fed6c40b4969d4a3c9c0a9ee228cf2  lubuntu-12.10-desktop-i386.iso
You can read the lines above as follows:
1) MD5 hash for lubuntu-12.10-desktop-amd64.iso is bddb521817360540c0e54616427c003e
2) MD5 hash for lubuntu-12.10-desktop-i386.iso is a7fed6c40b4969d4a3c9c0a9ee228cf2

With both hashes, access the page mentioned above and search for the name of ISO file. If the hash from the page is EXACTLY the same you got, so the file is okay, and you can make the correct use of it.

Make me know if it was somehow useful for you!

Monday, March 4, 2013

Manual instalation of Oracle Java on Ubuntu

This is the English version of this article of mine.

I had some versions of Java JDK installed in my Ubuntu Server 10.04, and I needed to install a new version (7u15). Here how I did it, trying to go straight to the point. This works for every Ubuntu (Desktop or Server) up to 12.10 (Quantal Quetzal).

  1. This is my crowded JVM directory:

    $ ls -lh /usr/lib/jvm/

    total 12K
    lrwxrwxrwx 1 root root   21 2012-05-27 20:19 java -> /usr/lib/jvm/jdk1.7.0_15
    drwxr-xr-x 4 root root 4,0K 2012-07-16 21:19 java-1.5.0-gcj-4.4
    drwxr-xr-x 8 root root 4,0K 2012-07-26 11:17 java-7-oracle
    drwxr-xr-x 8  500  143 4,0K 2013-02-15 18:21 jdk1.7.0_15


     
  2. I do not want a messy system. So I wiped it all up.

    $ sudo rm /usr/lib/jvm/j*
     
  3. Download Java JDK from Oracle. Today, this is the correct link for it. Choose the .tar.gz version. I downloaded both architecture versions:
    1. jdk-7u15-linux-i586.tar.gz
    2. jdk-7u15-linux-x64.tar.gz
  4.  Unpack the tar file to the correct directory, which is /usr/lib/jvm/. If this does not exist, create it.

    $ sudo tar xf jdk-7u15-linux-x64.tar.gz -C /usr/lib/jvm/
     
  5. Make each binary of ths new version of Java visible to Alternatives system:

    $ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_15/bin/java" 1

    $ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_15/bin/javac" 1

    $ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0_15/bin/javaws" 1

    This is for 32bits systems:
    $ sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" "mozilla-javaplugin.so" "/usr/lib/jvm/
    jdk1.7.0_15/jre/lib/i386/libnpjp2.so" 1

    This is for 64bits systems:

    $ sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" "mozilla-javaplugin.so" "/usr/lib/jvm/
    jdk1.7.0_15/jre/lib/amd64/libnpjp2.so" 1
  6. Choose each one of the binaries correctly:

     $ sudo update-alternatives --config java
     $ sudo update-alternatives --config javac
     $ sudo update-alternatives --config mozilla-javaplugin.so
     $ sudo update-alternatives --config javaws

This last step will list the versions available, if any. Choose the one from /usr/lib/jvm/jdk1.7.0_15, for this example.


After that, you just need to test the version. Do this for each one of the binaries:
$ java -version  
$ javac -version
This article was built upon Ubuntu Documentation.

Saturday, August 27, 2011

Shell Expansion

This friday, my friend was developing a shell script in KSH for AIX. In this script, several files with a special naming convention should be selected, but only part of the name should be considered. The format is:
word1-word2-extension-serial1-serial2-name.cmt
Where:
word1: any length
word2: any length 
extension: any length 
serial1: 6 chars 
serial2: 6 chars 
name: any length
So, these name would be ok, for instance:
CONTAB1-PALAVRA2-NMT-123456-123456-gilberto.cmt 
CTB1-PLV2-XYZ-654321-000001-joaozinho.cmt
From those names, everything before the serials should be extracted. That is:
CONTAB1-PALAVRA2-NMT 
CTB1-PLV2-XYZ
At first, the nightmare are the 3 variable length parts and the NAME , which is also variable. But SHELL EXPANSION can beat it. And the best, not only BASH, but it does apply to KSH (I really didn't know that !!!).

So, the solution:
$ var='CONTAB1-PALAVRA2-NMT-123456-123456-gilberto.cmt' 
$ echo ${var%%-??????-??????-*.cmt} 
CONTAB1-PALAVRA2-NMT
The second example:
$ var='CTB1-PLV2-XYZ-654321-000001-joaozinho.cmt' 
$ echo ${var%%-??????-??????-*.cmt} 
CTB1-PLV2-XYZ
Once again, the apparently cryptographic hieroglyphic SHELL did it!!!

:D

Wednesday, October 7, 2009

Colored man pages

I found a really interesting post about in a brazilian site named DICAS-L, and thought it would be really helpful to have it here, for me or anyone else. Man pages may be read with colored keywords, what helps understanding the (usually complicated) text within.


Pagers
It should be good to first understand the idea behind pagers. They are tools that ease the reading of long texts, as man pages are.

For Unix, probably the oldest pager is more, created by Daniel Halbert, in 1978. With ENTER the text is rolled up line by line, with SPACE we can read the next page, and for search the "/" character is used, as it is in vim. Thus, more became very important for Unix operator.

After, less was created by Mark Nudelman, from 1983 to 1985, offering more resources than more. The names were meant to be a joke, obviously. less allows the use of arrow keys for "navigating" the document, and much more interesting features than it's predecessor.

And finally, John Davis, from MIT, creates most, a pager with more features than the well known less. It is the basis of our article.

MAN pages
I strongly believe that MAN pages are simultaneously the best and the worst Unix documentation source, because (generally) they are written by the software author, but at the same time, not only it does not offers (many) examples, as it mostly make wide use of technical jargon, which can be complicated to newbies. Another good point is that most of man pages are written in English, and usually not localized, or at least as it should.

As said before, man pages are huge, and a pager should be a great help. pager is the default pager, and it is a symlink to some pager, as we can see:
$ whereis pager
pager: /usr/bin/pager
$ file /usr/bin/pager
/usr/bin/pager: symbolic link to `/etc/alternatives/pager'
$ file /etc/alternatives/pager
/etc/alternatives/pager: symbolic link to `/usr/bin/less'
We will replace less by most. What we need to do is to declare it with the environment variable MANPAGER.

Installation
As usual, installing a software is really easy, specially in Debian-based distros:
sudo aptitude install most
Configuration
To manualliy configure most as man pager, type the following:
$ export MANPAGER="/usr/bin/most -s"
But, by finishing your user session, this configuration vanishes. We really need to make it permanent, and we do so making smooth changes to start scripts:

If this pager must be ready only for the current user:
$ echo 'export MANPAGER="/usr/bin/most -s"' >> ~/.bashrc
If this pager must be ready for all users:
$ sudo su -
# echo 'export MANPAGER="/usr/bin/most -s"' >> /etc/profile
# exit
From now on, man pages are all colored. Some tips about most
  • b - bottom of the text
  • t - top of the text
  • / - search for
  • q - quit the text

Of course, we should always try to edit the files above (~/.bashrc or /etc/profile) and add those lines manually, with correct comments, and on. Enjoy it !

Don't forget to let down here what you thought about this article: useful, interesting or unuseful for you ?
Comments are welcome.