Linux Programmer | RHCE | RHCSA

Search This Blog

Thursday 29 December 2016

Receiving Brodcast Packets in Python

- Receive Brodcast Packets in Python Without Configuring Network (Network cable connection needed )

Sending Packets 

import os
import sys
import commands
from socket import *
macid = commands.getoutput('ifconfig | grep -i hwaddr | head -1')
os.system("echo '%s' | awk '{print$5}' >/tmp/.mac"%macid)
mac = commands.getoutput('cat /tmp/.mac')
hostname = commands.getoutput('hostname')
username = commands.getoutput('whoami')

info = "connect:"+'%s?%s?%s'%(username,mac,hostname)

s=socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s.sendto(info,('255.255.255.255',12345))

Receiving Packets

from socket import *
count = 0
while (count < 100):

s=socket(AF_INET, SOCK_DGRAM)
s.bind(('',12345))
m=s.recvfrom(1024)
print m[0]
    count = count + 1


Wednesday 28 December 2016

customize initrd in ubuntu

How to customize your initrd

The initrd contains all the programs and utilities the kernel needs in order to find the root partition. The initrd is "given" to the kernel by the boot loader (often isolinux on cds, or grub on hard drives).

Installed systems

The initrd used by an installed system is maintained by the mkinitramfs script, which is run automatically when for instance upgrading the kernel or any tool used inside the initrd. The correct and recommended way of modifying this initrd is to make hooks and scripts in the /etc/mkinitramfs directory tree. See /usr/share/initramfs for the scripts and hooks that are included by the system.

Live systems

You might find this information useful if you want to:
  • run the "Desktop" installer from a usb drive instead of from the cd
  • run the "Desktop" live system without using the cd player
  • debug and modify the live installer system
  • avoid burning all these cds
  • avoid using your slow and noisy cd player
The initrd used when booting a live cd ("Desktop CD") can be found in the casper directory of the cd. Below we will suppose you have downloaded an iso-file and mounted it with:  sudo mount -o loop edgy-desktop-i386.iso /mnt . If you otherwise have the burnt the cd inserted it, replace /mnt by /media/cdrom0

Modify the initrd

  1. Extract the contents of the initrd:
    •   mkdir initrd-tmp
        cd initrd-tmp
        gzip -dc /mnt/casper/initrd.gz | cpio -id
      or for Ubuntu 9.10 and later:
        lzma -dc -S .lz /mnt/casper/initrd.lz | cpio -id
  2. Modify the files
  3. Repack them into a new initrd:
    •   find . | cpio --quiet --dereference -o -H newc | gzip -9 > ~/new-initrd.gz
      or, if you want an initrd.lz (for Ubuntu 9.10 and later):
        find . | cpio --quiet --dereference -o -H newc | lzma -7 > ~/new-initrd.lz
You can now remaster a cd with this new initrd.gz replacing the old /casper/initrd.gz.
However, it is easier to put the initrd on one of your drives, and let a bootloader find it. The bootloader used by the Desktop/live cd, isolinux, is not able to look anywhere else than on the cd it is started from, so we will therefore use another boot loader, grub

Friday 9 December 2016

Encrypt shell script using shc in linux


For shell script encryption you need to install shc package

1. install shc

https://drive.google.com/open?id=0BwPBGrPmbppAYm9JRmhwTEp4ck0

2. extract shc-3.8.7.tgz and compile

tar xvfz shc-3.8.7.tgz

cd shc-3.8.7/

 make

How To Encrypt Shell Script ??

Create a sample bash shell script that you like to encrypt using shc for testing purpose.
For testing purpose, let us create the following random.sh shell script which generates random numbers. You have to specify how many random numbers you like to generate.

$ nano random.sh
#!/bin/bash

echo -n "How many random numbers do you want to generate? "
read max

for (( start = 1; start <= $max; start++ ))
do
  echo -e $RANDOM
done

$ ./random.sh
How many random numbers do you want to generate? 3
24682
1678
491
shc -r -T -f random.sh
This will create the following two files:
$ ls -l random.sh*
-rwxrw-r--. 1 purval purval 149 Mar 27 01:09 random.sh
-rwx-wx--x. 1 purval purval 11752 Mar 27 01:12 random.sh.x
-rw-rw-r--. 1 purval purval 10174 Mar 27 01:12 random.sh.x.c


  • random.sh is the original unencrypted shell script
  • random.sh.x is the encrypted shell script in binary format
  • random.sh.x.c is the C source code of the random.sh file. This C source code is compiled to create the above encrypted random.sh.x file. The whole logic behind the shc is to convert the random.sh shell script to random.sh.x.c C program (and of course compile that to generate the random.sh.x executable)
$ file random.sh
random.sh: Bourne-Again shell script text executable

$ file random.sh.x
random.sh.x: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

$ file random.sh.x.c
random.sh.x.c: ASCII C program text

Now, let us execute the encrypted shell script to make sure it works as expected.
$ ./random.sh.x
How many random numbers do you want to generate? 3
7489
10494
29627

Tuesday 6 December 2016

install GtkDialog in ubuntu

Steps for installing GtkDialog in ubuntu 

1. Download tar from below link

https://drive.google.com/open?id=0BwPBGrPmbppAaHIzVWdPbU04RUE

2. install Gtk version 2.0

apt-get install gtk2.0

3. Extract tar file

tar -xf gtkdialog.tar

4. Configure Gtk Dialog

cd gtkdialog/

sudo ./configure

sudo make

sudo make install


Saturday 3 December 2016

pressed ldap configuration

How To Pressed LDAP Configuration in non-interactive mode ?

LDAP Server Configuration :

1. install the following packages using apt-get 

 apt-get install -y aptitude
 apt-get install -y expect
 apt-get install -y debconf-utils

2. install slapd in noninteractive mode 

aptitude update
aptitude install expect

VAR=$(expect -c '
spawn aptitude -y install slapd
expect "New password for the slapd \"root\" user:"
send "123\r"
expect "Repeat password for the slapd \"root\" user:"
send "123\r"
expect eof
')

echo "$VAR"

aptitude -y install slapd

3. Configure slapd from backend 

debconf-get-selections | grep "slapd" (Displays all parameters of "slapd")


echo slapd slapd/no_configuration boolean false | sudo debconf-set-selections
echo slapd slapd/domain string ldap.com | sudo debconf-set-selections
echo slapd shared/organization string ldap.com | sudo debconf-set-selections
echo slapd slapd slapd/password1 password 123 | sudo debconf-set-selections
echo slapd slapd slapd/password2 password 123 | sudo debconf-set-selections
echo slapd slapd slapd/internal/adminpw password 123 | sudo debconf-set-selections
echo slapd slapd slapd/internal/generated_adminpw password 123 | sudo debconf-set-selections
echo slapd slapd/backend select HDB | sudo debconf-set-selections
echo slapd slapd/purge_database boolean false | sudo debconf-set-selections
echo slapd slapd/move_old_database boolean true | sudo debconf-set-selections
echo slapd slapd/allow_ldap_v2 boolean false | sudo debconf-set-selections

4. Change in file Configurations 

sed -i 's/#BASE/BASE/g'  /etc/ldap/ldap.conf
sed -i 's/dc=example/dc=ldap/g' /etc/ldap/ldap.conf

sed -i '9d' /etc/ldap/ldap.conf
sed -i "9i URI     ldap://localhost" /etc/ldap/ldap.conf


LDAP Client Configuration :



1. install Dependency packages

sudo apt-get install -y debconf-utils
sudo apt-get install -y aptitude

2. install ldap-auth-client package in noninteractive mode 

sudo DEBIAN_FRONTEND=noninteractive aptitude install -y -q ldap-auth-client

3. Configure ldap-auth client package from backend

echo ldap-auth-config ldap-auth-config/ldapns/ldap-server string ldap://192.168.1.1/ | sudo debconf-set-selections
echo ldap-auth-config ldap-auth-config/ldapns/base-dn string dc=ldap,dc=com | sudo debconf-set-selections
echo ldap-auth-config ldap-auth-config/ldapns/ldap_version select 3 | sudo debconf-set-selections
echo ldap-auth-config ldap-auth-config/dbrootlogin boolean true | sudo debconf-set-selections
echo ldap-auth-config ldap-auth-config/dblogin boolean false | sudo debconf-set-selections
echo ldap-auth-config ldap-auth-config/rootbinddn string cn=admin,dc=ldap,dc=com  | sudo debconf-set-selections

4. make changes in configuration files 

- set the ldap server ip  in ldap.conf

sed -i 's/uri ldapi:\/\/\//uri ldap:\/\/192.168.1.1\//g' /etc/ldap.conf

- set base dn in /etc/ldap.conf

sed -i 's/base dc=example,dc=net/base dc=ldap,dc=com/g' /etc/ldap.conf
sed -i 's/rootbinddn cn=manager,dc=example,dc=net/rootbinddn cn=admin,dc=ldap,dc=com/g' /etc/ldap.conf

- save password in /etc/ldap.secret file 
echo "123" >/etc/ldap.secret
sudo chmod 600 /etc/ldap.secret


Tuesday 29 November 2016

Make your keyboard “special keys” (Alt+Gr, Up,Down,..) working when using XRDP

- xkbevd

$sudo apt-get install xbindkeys

$xbindkeys --defaults > /home/username/.xbindkeysrc

now "xbindkeys -k" command for choosing key press combination
$xbindkeys -k

( press the key combination for e.g. Ctrl+Alt+Space)

- copy the key combination
m:0x14 + c:28

paste into  /home/username/.xbindkeysrc

"/usr/bin/script.sh"  (path of the script you want to run on some key combination)
m:0x14 + c:28

run below command for starting xbind key start
$xbindkeys

to make it permenant add this command into .Xsession of into users home file

Wednesday 16 November 2016

use of dd command

dd if=/dev/sr0 of=myCD.iso bs=2048 
conv=noerror,sync
Creates an ISO disk image from a CD-ROM; in some cases the created ISO image may not be the same as the one that was used to burn the CD-ROM.
dd if=system.img of=/dev/sdc bs=4096  
conv=noerror
Restores a hard disk drive (or an SD card, for example) from a previously created image.
dd if=/dev/sda2 of=/dev/sdb2 bs=4096  
conv=noerror
Clones one partition to another.
dd if=/dev/ad0 of=/dev/ad1 bs=1M  
conv=noerror
Clones a hard disk drive "ad0" to "ad1".

Monday 14 November 2016

bash: file not Found error

bash: file not found error occured even file is exists 

solution:

32-bit executable file is not execute in linux 64-bit systems

so you have to install i386 32-bit architecture 

try this :

1. open terminal  ( ctrl + alt+ T)

2. sudo dpkg --add-architecture i386

 sudo apt-get update

 sudo apt-get install libc6:i386

 sudo apt-get install libncurses5:i386
 
 sudo apt-get install libstdc++6:i386



Saturday 12 November 2016

Fix Broken initrd.img in linux

error : file /boot/initrd.img-3.10.12-generic' not found

if error found during booting system
follow the steps below

1.Boot your machine with a Live Media

2. open terminal and get partition 

fdisk -l

3. mount Filesystem
sudo mount /dev/sda1 /mnt
 
sudo mount --bind /proc /mnt/proc
sudo mount --bind /dev /mnt/dev
sudo mount --bind /sys /mnt/sys

4.Chroot /mnt and creating a Backup of the initrd image

sudo chroot /mnt

ls /boot/initrd*

or ls /lib/modules

mv /boot/initrd.img-3.11.0.12-generic /boot/old-initrd.img-3.11.0.12-generic-old

5. Building initrd.img

mkinitrd /boot/initrd.img-3.11.0.12-generic 3.11.0.12-generic-old

update-initramfs -c -k 3.11.0.12-generic

 

6. Finalizing Grub loader and unmounting

grub2-mkconfig -o /boot/grub2/grub.cfg

update-grub

 sudo unmount /mnt/proc

sudo unmount /mnt/sys

sudo unmount /mnt/dev

sudo unmount /mnt

init 6

Friday 11 November 2016

java applications are not working in Firefox linux ?

if applications based on java is not working in firefox 

follow steps below (only for linux 32 bit systems)

1. Download bin file from the link below:

https://drive.google.com/open?id=0BwPBGrPmbppATjVtVUNYOEN4Mnc

2. chmod 0777 jdk-6u45-linux-i586.bin

3. ./jdk-6u45-linux-i586.bin

4. mv jdk1.6.0_45 /opt/

5. cd /usr/lib/firefox-addons/plugins

6. ln -s /opt/jdk1.6.0_45/jre/lib/i386/libnpjp2.so .

7. close firefox and restart firefox

to checkj add on is added or not

open firefox -> options -> add-ons -> Plugins
check javaTM(plugin is installed or not ? upgrade it if you want to upgrade )

if you are not able to make This plugin Always activate then Follow the steps below

How to set old versions of plugins to 'Always Activate'?

 
1. open firefox
2. open about:config (have to enter 2-3 times to open it)
3.this will list configurations of firefox
4. Look for 'extensions.blocklist.enabled' and set it to false
5. This will remove the 'is known to be vulnerable message and enable the previously greyed out 'ask to activate' & 'Always active' options.

You can then set the plug in to always enable by setting the relevant value for 'plugin.state.<plugin_name>:

0 - Never Active
1 - Ask to Active
2 - Always Active

I saw 'plugin.state.java' & 'plugin.state.ndeployjava' when looking during my testing.

Tuesday 8 November 2016

Add/Remove Main Menu From List in Lubuntu

Script :

File For menu is Located At: /etc/xdg/lubuntu/menus/lxde-applications.menu
(Chnange The Path of menu file in script)

menu="$1"

check=`grep -i "<Name>$menu<\/Name>" /tmp/lxde-applications.menu`
if [ -n "$check" ];
then
line0=`cat /tmp/lxde-applications.menu | grep -n "<Name>$menu<\/Name>" | cut -d$
line1=$((line0-1))
line2=`cat /tmp/lxde-applications.menu | grep -B 1 -A 25 -n "<Name>$menu<\/Name$
bline=$((line2+1))
sed -n "${line1},${line2}p" /tmp/lxde-applications.menu  >/tmp/"$menu"
app="<!--$menu-->"
sed -i ''$line1'i\'$app'\' /tmp/lxde-applications.menu

echo "$line1==>$line0==>$bline"
sed -i ''$line0','$bline'd' /tmp/lxde-applications.menu

else
        echo "Menu Not Exists"
fi

To Run Script:
./script_name.sh {menu-name}

To Add Deleted Menu :
sudo sed -i '/<!---DesktopSettings--->/r /tmp/DesktopSettings' lxde-applications.menu

sudo sed -i '/<!--DesktopSettings-->/d' /tmp/lxde-applications.menu

Friday 4 November 2016

Add Password Policy in LDAP

Step-1 : Enable ppolicy overlay
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/ppolicy.ldif

Step-2 Create file ppmodule.ldif load the pp module
dn: cn=module{0},cn=config
changetype: modify
add: olcModuleLoad
olcModuleLoad: ppolicy

Step-3 load the module ppmodule.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f ppmodule.ldif

Step-4 Prepare for Overlay Create file ppolicyoverlay.ldif
dn: olcOverlay={0}ppolicy,olcDatabase={1}hdb,cn=config
objectClass: olcOverlayConfig
objectClass: olcPPolicyConfig
olcOverlay: {0}ppolicy


Step-5 Add ppolicyoverlay.ldif using ldapadd commandb  
sudo ldapadd -Y EXTERNAL -H ldapi:/// -f ppolicyoverlay.ldif

Note : this 5 steps are done only once before applying password policy, no need to repeat this everytime while creating password policy


Step-8 Create passwordpolicy.ldif for MyOrganization
dn: cn=MyOrgPPolicy,ou=Policies,dc=ldap,dc=com
cn: MyOrgPPolicy
objectClass: pwdPolicy
objectClass: device
objectClass: top
pwdAttribute: userPassword
pwdMaxAge: 3024000
pwdExpireWarning: 1814400
pwdInHistory: 4
pwdCheckQuality: 1
pwdMinLength: 9
pwdMaxFailure: 4
pwdLockout: TRUE
pwdLockoutDuration: 600
pwdGraceAuthNLimit: 0
pwdFailureCountInterval: 0
pwdMustChange: TRUE
pwdAllowUserChange: TRUE
pwdSafeModify: FALSE

Step-9 Add passwordpolicy.ldif in LDAP
ldapadd -D cn=admin,dc=ldap,dc=com -w password -f passwordpolicy.ldif


[ Your Policy named Default Policy is Applied , Remember You have To create OU named Policies defore adding this policy ]

-----------------------Apply policy to Users ---------------------

dn: cn=purval,ou=Users,dc=ldap,dc=com changetype: modify add: pwdPolicySubentry pwdPolicySubentry: cn=MyorgPolicy,ou=Policies,dc=ldap,dc=com

---------------------- Remove Policy from User ----------------------------

dn: uid=purval,ou=People,dc=ldap,dc=com changetype: modify delete: pwdPolicySubentry







Configure LDAP Server

Create Ldap Server in Tiguin14.04:

1.Change Host name
  • nano /etc/hostname
server.ldap.com
2. Reboot system
  • Init 6
3. apt-get install slapd ldap-utils
  • Enter administrator password:

4. nano /etc/ldap/ldap.conf
  • Remove hash brefore BASE ans URI line ,and add foillowing
BASE dc=ldap,dc=com
URI ldap://localhost:389

5. dpkg-reconfigure slapd
You will be asked a series of questions about how you'd like to configure the software.
  • Omit OpenLDAP server configuration? No
  • DNS domain name?
    • ldap.com
  • Organization name?
    • ldap.com
  • Administrator password?
    • 123
    • 123
  • Database backend to use? HDB
  • Remove the database when slapd is purged? No
  • Move old database? Yes
  • Allow LDAPv2 protocol? No
6. ldapsearch -x

7. apt-get install phpldapadmin

8. nano /etc/phpldapadmin/config.php

  • line  no. 286 “My Ldap server”     you can add anything here
  • Line no 293      Enter ip address of your machine e.g. 192.168.1.226
  • Line no. 300             dc=ldap,dc=com
  • Line no. 326            dc=ldap
  • Line no. 161 true

9. /etc/init.d/apache2 restart

10. Open browser
-login with password which is given during ldap installation
Click on login -> type password -> login

Click on
dc=ldap,dc-com(1) -> create new entry here -> create organizational unit -> e.g sales -> commit

Click on
ou=sales -> create child entry -> Generic : Posix Group -> e.g. sales-group -> commit

Click on
ou=sales-group -> create child entry -> Generic: User Account ->
Fill the following Text boxes
Common name , GID number, Home Directory, login shell, last name, password
-> ok -> commit

[ NOTE : We can change the path of users home Directory on Client system by just defining it into the Home Directory text Box.]






================= LDAP Commands ======================
- list all the users under some group 

ldapsearch -x -b $dn -s sub "objectclass=posixGroup" | sed -n '/cn: $grp_name/,/dn:/p' | grep "^memberUid:" | awk '{print$2}' 

- List all created Users in LDAP
ldapsearch -x -b dc=ldap,dc=com -s sub "objectclass=posixAccount" | grep -i "^uid: " | awk '{print$2}' 


Modify LDAP group name usig LDIF file : /tmp/.modify.ldif
dn: cn=php,ou=Groups,dc=ldap,dc=com
changetype: modrdn
newrdn: cn=purval
deleteoldrdn: 1


ldapmodify -x -D "cn=admin,dc=ldap,dc=com" -w 123 -f /tmp/.modify

- Search All Users Password history

ldapsearch -LLL -x -h localhost -Z -D cn=admin,dc=ldap,dc=com "(&(objectclass=Person))" pwdHistory -w {password}

dn: cn=purval,ou=Users,dc=ldap,dc=com
pwdHistory: 20170131064459Z#1.3.6.1.4.1.1466.115.121.1.40#6#123456
pwdHistory: 20170131093602Z#1.3.6.1.4.1.1466.115.121.1.40#6#123456
pwdHistory: 20170201053907Z#1.3.6.1.4.1.1466.115.121.1.40#6#123456
pwdHistory: 20170204074307Z#1.3.6.1.4.1.1466.115.121.1.40#6#123456

dn: cn=bhavesh,ou=Users,dc=ldap,dc=com

dn: cn=mehul,ou=Users,dc=ldap,dc=com

dn: cn=lokesh,ou=Users,dc=ldap,dc=com

dn: cn=khushbu,ou=Users,dc=ldap,dc=com

 

- if Print All the password History of purval user then,


ldapsearch -LLL -x -h localhost -Z -D cn=admin,dc=ldap,dc=com "(&(objectclass=Person))" pwdHistory -w 123 | sed -n '/cn=purval/,/dn:/p' 

- Print all Users Password change time
ldapsearch -LLL -x -h localhost -Z -D cn=admin,dc=ldap,dc=com "(&(objectclass=Person))" modifyTimestamp -w {password}  

========Convert LDAP timezone to UTC to IST ==============

- default LDAP server timezone is Asia/Zulu
time is displayed in UTC standard format e.g. 20170206055543Z

- To covert this time format in normal time

echo "20170402125623" | sed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})$/\1\\ \2:\3:\4/' | xargs date -u -d   >/tmp/.date
output is: Mon Feb 6 11:25:43 UTC 2017 

 dateis=`cat /tmp/.date`
(This date format is UTC format, convert it to IST) 

eval "date --date='TZ=\"Asia/Zulu\" $dateis'"
output :Mon Feb 6 11:25:43 IST 2017


 

SSH not working with password after upgrade ubuntu 22.04

Issue: In recent upgrade of ubuntu 22.04 we are not able to login server with SSH password. but when we try to login with key then it allow...