Linux administration notes & code snippets
Set and Get environmental shell variable using c++
Last Updated on Sunday, 06 June 2010 07:43 Saturday, 05 June 2010 15:11
Here is a small example on how to set and get environmental variables using getnenv() and putenv() functions defined by C/C++ stdlib.h library. Environmental variable expansion is a great feature of a Linux shell as it enables programmers and users to rely on the environment settings of each user separately. C++ getenv() will read all exported environmental variables and putenv() will set existing or create new variables. Here is a small c++ program which can do this job:
#include <stdlib.h> #include <iostream> int main() { // get and print shell environmental variable home std::cout << "SHELL = " << getenv("SHELL") << std::endl; std::cout << "MYENV = " << getenv("MYENV") << std::endl; //set new shell environmental variable using putenv char mypath[]="TEMP=/my/new/temp/path/"; putenv( mypath ); std::cout << "TEMP = " << getenv("TEMP") << std::endl; return 0; }
Now lets' try to export new shell environment variable MYENV:
$ export MYENV=linuxconfig.org
Compile c++ program:
$ g++ shell_env.cpp -o shell_env
Run:
$ ./shell_env
Output:
SHELL = /bin/bash MYENV = linuxconfig.org TEMP = /my/new/temp/path/Add a comment
Clone / Burn Encrypted DVD using Linux
Last Updated on Saturday, 05 June 2010 15:49 Friday, 04 June 2010 22:19
By default K3b or brasero burning software will refuse burn encrypted dvd. One way to overcome this problem is by using libdvdcss library whish allows K3b or brasero to look at your encrypted DVD within DVD-device as a block device and thus disregarding its encryption. Before running K3b or brasero install libdvdcss library to allow it clone / burn encrypted DVD.
On Debian or Ubuntu  libdvdcss installation to allow K3b burn encrypted DVD's:
apt-get install libdvdcss2
If you are using debian and libdvdcss2 is missing make sure that you have www.debian-multimedia.org in your repository. Once done palce encrypted DVD into your DVD-Burner and start copy.
Add a commentHow to get wdiddle3 to set iddle timer for WD EARS drive
Last Updated on Thursday, 03 June 2010 00:06 Wednesday, 02 June 2010 23:53
Here are simple to follow steps on how to disable and set timer for head parking with WD EARS drive. To do this task we are going to use wdiddle3 utility developed by Wester Digital. According to the Western Digital the wdiddle3 utility was not designed to be used with EARS drives so no warranty ! In particula my own drive did not take it well when i completelly disabled parking idle timer as I could hear strange clicking sound almost every second.
Â
The utility runs only from a DOS so we need to use freedos.
Â
- get freedos iso ( fdfullcd.iso (153MB) ) and burn it to cd-rw ( 153MB ) with multi-session
- get wdidle3 utility: http://support.wdc.com/product/download.asp?groupid=609&sid=113
- there are suggestions to burn freedos and wdidle3 on the same cd using multi-session but that did not work for me from some reason. What i did was that I had 2 cd-rw cd's . One with freedos bootable and second with wdidle3 on it.
- reboot with freedos cd
- I had some problems with cdrom the only thing that worked was to after boot hit ENTER and then choose 1 " install to hard... " Select language to ENG and then Run free dos from CDROM
- then you get to the prompt with X: which is your cdrom. Swap disc's now. list files with dir command to make sure that wdidle3.exe is in your current directory and then enter:
X:>wdidle3 /D
to this will disable
Idle3Â Timer is Disabled
or
X:>wdidle3 /S255
to set timer to 4.5 min where you can choose number from 1 to 255 mil sec
Add a commentlinux nVidia MCP55 forcedeth module is not working
Last Updated on Friday, 04 June 2010 16:57 Wednesday, 02 June 2010 09:48
This problem with forcedeth module seem to affect all major Linux distributions. Year 2008 was the first time I reported this issue on a ubuntu bug tracking system. Just recently I have installed around 6 Linux distribution and in all of them my nVidia Corporation MCP55 Ethernet (rev a2) network card does not work and this includes latest fedora 13. The fix is still the same:
rmmod forcedeth modprobe forcedeth msi=0 msix=0
Ubuntu and Debian users can make this fix permanent with:
echo options forcedeth msi=0 msix=0 >> /etc/modprobe.d/options update-initramfs -u rebootAdd a comment
Time your off-peak download with at Linux command
Last Updated on Sunday, 30 May 2010 22:33 Sunday, 30 May 2010 22:26
Not everyone has unlimited Internet download. Sometimes Internet provides provide peak and off-peak hours and hardly is someone going to stay up at night to trigger their downloads.
There are many tools in Linux which allows user to set a timer to automatically start download without user intervention. Here is a simplest form of automatic off-peak download involving at, wget and shutdown command. First create a file which will contain wget and shutdown commands:
mydownloads.txt:
wget ftp://ftp.redhat.com/pub/redhat/rhel/beta/6/i386/iso/RHEL6.0-20100414.0-AP-i386-DVD1.iso
wget http://ftp.monash.edu.au/pub/linux/CentOS/5.5/isos/i386/CentOS-5.5-i386-bin-DVD.iso
shutdown -h now
Now use at command to execute all command within mydownloads.txt file:
at -m 02:00 < mydownloads.txt
At command will automatically start download at 2AM and when downloads are finished it will shutdown your PC.
NOTE: For shutdown you need sudo or run command as root
Add a commentBash script to test hard drive transfer speed
Last Updated on Saturday, 29 May 2010 12:54 Saturday, 29 May 2010 10:28
Here is a small bash script to test a hard drive transfer speed. It should be take as an approximation. The speed value is taken from Linux dd command output. One way to test your hard drive speed is to use hdparm command:
# hdparm -Tt /dev/sda
OUTPUT:
/dev/sda:
Timing cached reads: 7216 MB in 2.00 seconds = 3615.89 MB/sec
Timing buffered disk reads: 288 MB in 3.00 seconds = 95.87 MB/sec
However, in this case the hdparm command is accessing raw hard drive, disregarding all partitions and file systems. The weakness of the following script is that it does not take source hard drive reading speed into consideration, however it is accurate when measuring transfer speed between two hard drives or speed between two nodes over the network using NFS or samba. Run the script with 3 arguments, source file, destination file and number of runs to make an average:
NOTE: If you do not have a file to copy simply create one by running a following command for couple seconds and interrupt with CTRL+C:
$ cat /dev/zero > myfile.zero
speed_test.sh :
#!/bin/bash
# USAGE:
# ./speed_test.sh /path/to/my/file /path/to/destination number_of_tests
NUM_TESTs=$3
SUM=0
for i in $( seq 1 $NUM_TESTs ); do
REC=`dd if=$1 of=$2 2> some_random_file_ ; cat some_random_file_ | cut -d " " -f8 | tail -1`
SUM=`echo $SUM + $REC | bc`
done
RESULT=`echo $SUM / $NUM_TESTs | bc | awk '{ str1=str1 $0 }END{ print str1 }'`
echo $RESULT MB/s
#clean up
rm some_random_file_
rm $2
./speed_test.sh /mnt/sdb1/ubuntu.iso /mnt/sda1/ubuntu.dd 3
OUTPUT:
57 MB/sAdd a comment
How to crash your Linux system with fork bomb
Last Updated on Friday, 28 January 2011 19:55 Sunday, 23 May 2010 17:43
Here is a simple way to crash your Linux system as a non-root user with a bash function called recursively.
$ :(){ :|:& };:
:() is a function which gets called recursively from its body and cannot be killed since it is running on the background with &. : is actually the name of the function.
Here is the same function call in human readable format:
forkbomb(){ forkbomb | forkbomb & }; forkbomb
As you can see the function is calling its self twice in the body. This will start consume all resources of your system and eventually force your Linux system to crash. To get more understanding type simple function on your command line. The following function is harmless:
$ fork_bomb(){ echo "FORK BOMB"; };
$ fork_bomb
FORK BOMB
You can take same measures to ensure that your Linux users would not exploit fork bomb. Fork bomb is not a bug nor weakness of Linux system. The responsibility is in hands of systems administrators to limit number of processes available for a user by editing /etc/security/limits.conf file. To limit username forkbomb to only 50 processes add following line:
forkbomb hard nproc 50
If you want to limit entire group called forkbomb to only 100 processes add a line below:
@forkbomb hard nproc 100
To make limit of 100 processes as a default value for all users add a followng line:
@forkbomb hard nproc 100Add a comment
Change mac address with macchanger Linux command
Last Updated on Sunday, 23 May 2010 00:07 Saturday, 22 May 2010 23:54
In some situations you need to fake / change / spoof a MAC address of your network interface. macchanger Linux command does this job in no time. With this tool you can change your mac address of any Ethernet network device wired or wireless.
Here is a small example:
My original mac address:
# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:16:d3:23:7c:f7
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Memory:ee000000-ee020000
Turn off your network interface:
# ifconfig eth0 down
Use macchanger to randomly generate new MAC address and assign it to eth0 network interface:
# macchanger -r eth0 Current MAC: 00:16:d3:23:7c:f7 (unknown) Faked MAC: 32:cf:cb:6c:63:cd (unknown)
In case you see a following error message:
ERROR: Can't change MAC: interface up or not permission: Cannot assign requested address
Make sure that your interface is down and you are running macchanger as a root user.
Enable eth0 network interface and check new MAC address:
# ifconfig eth0 up
# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 32:cf:cb:6c:63:cd
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Memory:ee000000-ee020000
If for example a specific MAC address is required use macchanger as follows:
# macchanger -m b2:aa:0e:56:ed:f7 eth0 Current MAC: 32:cf:cb:6c:63:cd (unknown) Faked MAC: b2:aa:0e:56:ed:f7 (unknown)
macchanger also allows you to change mac address for a specific network card vendor. Use a -l option to print a list of all know network card vendors.
Add a commentExample of simple bash script ftp client
Last Updated on Sunday, 16 May 2010 18:49 Sunday, 16 May 2010 15:13
Very often I need to upload some files to my web server from a command line. entering a user name and password, changing directory can be little tedious work. Here is a small bash script which make this work easier. This script first defines a variables such as hostname of the ftp server, username and password and then it creates ftp session and uploads file into your selected directory:
#!/bin/bash ftp_site=myhostname username=myusername passwd=mypass PS3='Select a destination directory: ' # bash select select path in "." "public_html/" "public_html/myblog/" "backup/images/" do ftp -in <<EOF open $ftp_site user $username $passwd cd $path put $1 close bye EOF echo $1 uploaded to $path ! # Break, otherwise endless loop break done
EXECUTE:
$ chmod +x ftp_bash_script.sh $ ./ftp_bash_script.sh file1
OUTPUT:
1) . 3) public_html/myblog/ 2) public_html/ 4) backup/images/ Select a destination directory: 2 file1 uploaded to public_html/ !Add a comment
More Articles...
- Set boot password with GRUB boot Linux loader
- How to check a current runlevel of your Linux system
- Executing commands remotely with ssh and output redirection
- Example of binary search algorithm in C++
- Linux mediatomb installation and setup for PS3
- Ubuntu / Debian jdownloader linux installation howto
- /etc/network/interfaces to connect Ubuntu to a wireless network
- main decoder error: no suitable decoder module for fourcc `XVID'
- Enable multiple clone displays to VGA interface projector or TV
- Remove or ignore all comment lines from Linux config files
- Add character to the beginning of each line using sed
- Setting the timezone under Linux
- Setting the hardware clock under Linux
- Linux Poker Online client
- C++ code on how to read characters from a file
- Download YouTube videos using Linux command clive
- Linux commands to Backup and Restore MySQL database
- add user Linux command
- Linux Add User To Group
- GNU R - package not found - how to install
- Replace all TAB characters with spaces
- Change the priorities of linux processes with nice and renice
- Starting a process remotely with nohup command
- Identify if CPU is using 32-bit or 64-bit instruction set
- IBM ThinkPad x60s laptop battery life time test
- Linux Backup Restore Destroy and Install MBR - Master Boot Record
- FATAL ERROR: Bad primary partition 0: Partition ends in the final partial cylinder
- Using command line wodim tool to burn iso image
- ISP caching to reduce bandwidth - wget and meta workaround
- Example of C++ class template Array to instantiate an Array of any element type
- Remove white space from file name and rename it with bash command
- How to play wmv format on linux alias Video Codec: Unavailable ( MSS2 )
- Set default internet browser to firefox with KDE 3
- How to test microphone with Audio Linux Sound Architecture - ALSA
- grub loading stage 1.5 error 15
- How to Label hard drive partition under Linux
- Use OpenCV to separate RGB image into red green and blue components
- Installation of deb kernel in Debian chroot environment
- How to retrieve and change partition's UUID Universally Unique Identifier on linux
- OpenCV color to grayscale conversion program
- Reprogram keyboard keys with xmodmap
- Synchronization of your camera with PC using rsync
- xine was unable to initialize audio drivers
- Frequently used options for debian / ubuntu dpkg command
- Resize an image with OpenCV cvResize function
- Display Image Attributes with OpenCV
- C++ function to calculate Fibonacci number
- Google offers free Mysql database
- Connvert audio ogg files to mp3
- Intel Corporation PRO/Wireless 2200BG Network Connection install on Linux Debian Etch
- Cisco CCNA – IP address Class B range
- Display google search results from different contries
- Joomla – Error: the XML response that was returned from the server is invalid
- Setting up HP officejet – Ubuntu Linux Jaunty
- 101 howto start with opencv and computer vision on ubuntu linux
- VMware arrow keys problem on Ubuntu
- Saving an output of PostgreSQL query to a text file
- START and STOP rtorrent during peak and off-peak hour on thecus N2100
- Using .htaccess file to redirect to www host
- Turn Off directory browsing on Apache
Page 9 of 16













