Tuesday, January 8, 2019

Bash scripts on command line not in a file

There are situation as a Linux System Administrator or related roles, we need to write a quick shell script on the command line instead of putting it in a file. When we actually use semicolon to delimit commands. However, in a bash file, we actually use "carriage returns" (Enter button) to separate commands. For example:

 For example, this is the command on the command line:

$ for i in `seq 5`; do if [[ $i == 3 ]]; then echo "number is $i"; fi;  done

number is 3

Sunday, October 7, 2018

How to find which applications are connecting to internet or local network on your Linux machine?

It has been a very long time since I posted something on my blog! However, I just thought I could come back with a small post! Imagine I want to find which I find out which processes on my machine is connected through TCP or UPD on my system. a simple way is to just run:

$lsof -i

 lsof actually only list the open files in the system. adding -i option will  option produces a listing of all network files. which is what we are interested. If they are multiple users and we want open files for a $USER we just add u option:

$lsof -u$USER (for example: ls -ufoo for user 'foo') 

If I want to see connections for example on a specific port  I do :

 $losf -i :port ---> example: lsof -i :80

Monday, January 11, 2016

How to solve shared Libraries problem in Linux

Sometimes when you try to compile a program in Linux you face the problem of the shared libraries. It happens when C Linker is not able to locate them in the File System. Shared libraries are loaded when a program starts. they include a ".so" as the file extension and are similar to dynamic-link library or DLL under Windows. There is a convention to name  them in linux:

lib + name of library + .so + version number

for example: libXrender.so.1

The shared libraries are usually placed in the /usr/lib (/usr/lib/x86_64-linux-gnu/ for x86 machine in Ubuntu 14.04) or /usr/local/lib.
If you can not compile a program because some shared library are not present on your system, you have to first make sure which library is missing. you can use "ldd" program to prints shared library dependencies.

$ldd ./libsqlncli-11.0.so.1720.0
  libcrypto.so.6 => not found
  libodbc.so.1 => /usr/local/lib/libodbc.so.1
  libssl.so.6 => not found

The above example tells us that the shared library itself depends on two other libraries which are: libcrypto and libssl.

To resolve this issues we have to make sure which path the linker should search to find the necessary libraries. To do this, we have to simply edit file /etc/ld.so.conf. To do so just put this line in the file :

include /etc/ld.so.conf.d/*.conf

it make sure to add all the config files on the directory. We could add the necessary paths in the file directly but this way is more convenient.

For the last step just make a new file and add the path of the library. for example:

# vim /etc/ld.so.conf.d/my_odbc.conf

and add this line

/usr/local/lib/

After these steps you have to run the following command as superuser:

#ldconfig

ldconfig configure dynamic linker run-time bindings. It creates the necessary links found in the file /etc/ld.so.conf.

when compiling the program we could also pass the path by using -L option to announce the shared library ( or static) to the linker. the shared library (or static) should be announced to the linker. for example:

gcc –o program program.o –L/usr/lib –lfoo

tells the compiler to look into /usr/lib for libraries and -lfoo options tells the compiler to use a library called libfoo.so.

Tuesday, June 2, 2015

take screen shots with terminal in Linux (bash script)

I recently needed to take screen shots in Linux with a tool automatically. I needed to have a 15 seconds time intervals between these screen captures and save them in a directory to do some image processing on them.

Since most linux distributaions use X Window system as their graphical system they are shipped with X utilities which can come handy here. xwd is a tool to  dump an image of an X window. It saves them in a special file format. but we can use pipes to convert it to for example png file format:

$ xwd | xwdtopnm | pnmtopng > /tmp/screenshot.png

We can have all of it as an bash script as well. name it myScript.sh for instance.

#!/bin/bash
# Produce a screen dump periodically and save as a JPEG
 

DELAY=15                   # seconds between screen dumps
DIR=/tmp/sceenshots  # directory to hold screen dumps
I=0                              # current image number
 

mkdir -p ${DIR}
while sleep $DELAY
 

do
xwd -root | xwdtopnm | pnmtopng >${DIR}/screendump.${I}.png
((I++))

done

just do

$chmod +x myScript.sh

and you are fine!

Tuesday, April 28, 2015

produce 100% CPU Load in Linux

Today, I was going to boot my old Thinkpad T61 with a live USB drive to test my CPU fan noise and performance. I was also using "Psensor", a tiny GTK+ based application to monitor the temperature of my CPU cores and fan speed. I wanted to have a 100% load for this matter.
I first thought of writing a CPU hungry program like Factorial calculation as recursive functions in C and run it on my Ubuntu Linux. One of my friends at work suggested a nice hack!

if you just enter this in Terminal:

dd bs=1024 if=/dev/urandom of=/dev/null

and then check resources usage through "htop" utility. you will be amazed! one of my CPU cores is running on 100%!







/dev/urandom is kernel's random number generator and what we do is to copy 512 Bytes into /dev/null! as simple as that! so CPU is busy creating random numbers!

We can run it again to have both cores run on 100%!
To give CPU a relief, we can send SIGTERM by pressing Ctrl+C to terminate the task. here is a an output after running for 10 minutes on my system:

4723473+0 records in
4723472+0 records out
4836835328 bytes (4.8 GB) copied, 426.123 s, 11.4 MB/s

Tuesday, April 7, 2015

bash completion in Ubuntu with root login

If you use sudo in bash to access root privilege when you are required to modify some system settings for example and use TAB key to have bash completion, you are fine.
But sometimes you want to completely switch to superuser you may invoke
su  command. In this case you are not able to use very handy bash TAB completion. To resolve this you should change .bashrc for the root user. Remember that with sudo you are still logged in with your current user not the root. so there is no need to alter bashrc for that user.

To enable it, you have to uncomment the lines in the picture to enable this.

Sunday, September 14, 2014

problem with Cisco Anyconnect VPN client on Ubuntu Linux 12.04 and later

I am using Cisco Anyconnect secure mobility client to make a secure VPN connection to my university Anyconnect VPN gateway and have access to online scientific database such as IEEE Xplore digital library.





I'm running Ubuntu 14.04 LTS on my laptop. The problem is that from Ubuntu 12.04 the DNS resolving has changed. Linux uses a plain text file called /etc/resolve.conf to configure the DNS resolver library. Until Ubuntu 12.04 anyone could manually add nameservers to this file but as I mentioned it has been changed so any changes manually done to /etc/resolv.conf will be lost as it gets overwritten next time something triggers resolvconf.

To be more precise, the package resolvconf is the only way to alter file /etc/resolv.conf. resolvconf itself  uses DHCP client hooks, a Network Manager plugin and /etc/network/interfaces to generate a list of nameservers and domain to put in /etc/resolv.conf.

lets get back to Anyconnect client! The problem is that this application directly put namesrver in /etc/resolv.conf and creates problems. If you get disconnected or forget to close this application properly(for instance killing by the process ID)
you will not be able to deploy DNS properly any more.

To resolve this issue, I decided to install the opensource tool openconnect. it is more convinient and you have the option to both use it as CLI or as a plug-in to network manager.

To install both of them simply run this command in terminal:

  $ sudo apt-get install openconnect network-manager-openconnect

I prefer command line option and I can pass parametrs to the command easily. To do so I just need to run this command:

$ echo $PASSWORD | sudo openconnect -v -u $USERNAME --authgroup='$GROUPNAME' --passwd-on-stdin https://sslvpn.ltu.se


you have to replace $PASSWORD, $USERNAME and $GROUPNAME. I have myself is using it as a simble bash script. It works great!