Archive | General

Tags: , , , , , , , , , , , ,

Video Game Emulators For Ubuntu

Posted on 23 April 2013 by Johnny

Linux up until the past few years has been left out of the emulators market as far as availability luckily this has change and now there are ports of almost every major system emulator including some emulators that are written strictly for the linux operating system, the following site has a nearly complete list of all emulators that are available through the Ubuntu package manager and what repository they are in, The site is also slowly adding source files that can be compiled on any other linux distribution.

The list: http://retro-gaming-world.com/emulators-for-ubuntu/

Comments (0)

Tags: , , , ,

Bash For Loops

Posted on 27 March 2013 by Johnny

ow do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using for statement? How do I use three-parameter for loop control expression?

A ‘for loop’ is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script.

For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.

for loop syntax

Numeric ranges for syntax is as follows:

for VARIABLE in 1 2 3 4 5 .. N
do
	command1
	command2
	commandN
done

OR

for VARIABLE in file1 file2 file3
do
	command1 on $VARIABLE
	command2
	commandN
done

OR

for OUTPUT in $(Linux-Or-Unix-Command-Here)
do
	command1 on $OUTPUT
	command2 on $OUTPUT
	commandN
done

Examples

This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:

#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:

#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
  do
     echo "Welcome $i times"
 done

Sample outputs:

Bash version 4.0.33(0)-release...
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times

Three-expression bash for loops syntax

This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3).

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done

A representative three-expression example in bash as follows:

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
   echo "Welcome $c times"
done

Sample output:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

How do I use for as infinite loops?

Infinite for loop can be created with empty expressions, such as:

#!/bin/bash
for (( ; ; ))
do
   echo "infinite loops [ hit CTRL+C to stop]"
done

Conditional exit with break

You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop:

for I in 1 2 3 4 5
do
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  statements2
  if (disaster-condition)
  then
	break       	   #Abandon the loop.
  fi
  statements3          #While good and, no disaster-condition.
done

Following shell script will go though all files stored in /etc directory. The for loop will be abandon when /etc/resolv.conf file found.

#!/bin/bash
for file in /etc/*
do
	if [ "${file}" == "/etc/resolv.conf" ]
	then
		countNameservers=$(grep -c nameserver /etc/resolv.conf)
		echo "Total  ${countNameservers} nameservers defined in ${file}"
		break
	fi
done

Early continuation with continue statement

To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement.

for I in 1 2 3 4 5
do
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  statements2
  if (condition)
  then
	continue   #Go to next iteration of I in the loop and skip statements3
  fi
  statements3
done

This script make backup of all file names specified on command line. If .bak file exists, it will skip the cp command.

#!/bin/bash
FILES="$@"
for f in $FILES
do
        # if .bak backup file exists, read next file
	if [ -f ${f}.bak ]
	then
		echo "Skiping $f file..."
		continue  # read next file and skip cp command
	fi
        # we are hear means no backup file exists, just use cp command to copy file
	/bin/cp $f $f.bak
done

Comments (0)

Executing PHP scripts over HTTP Using Cron Jobs

Tags: , , ,

Executing PHP scripts over HTTP Using Cron Jobs

Posted on 23 March 2013 by Johnny

In programming there are a number of ways to execute on an idea and get a result, executing a php file on your web server is no different. Below I will provide examples of how to execute a php script or other type of script over http using configurable cron jobs.

Just about all webhosts also provide a method of adding cronjobs through their control panels including cpanel and plesk.

Method 1: Execute the script using php from the crontab

Just like how you call your shell script (As show in our crontab 15 examples article), use the php executable, and call the php script from your crontab as shown below.

To execute myscript.php every 1 hour do the following:

# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php

Method 2: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.randomlinux.com/myscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.

 

 

*/5 * * * * /usr/bin/curl -o temp.txt http://www.randomlinux.com/myscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.randomlinux.com/myscript.php

The -o and -O flags are only necessary if you want to log the output of the script that is being executed if this information is not needed these flags can be ignored.

Comments (0)

Adding Aliases to your .bashrc

Tags: , ,

Adding Aliases to your .bashrc

Posted on 21 March 2013 by Johnny


Ever wonder how you can make shortcuts for some of those long cumbersome Linux commands?  You can by making an alias for it.  What is an alias?  It is a shortcut, let me demonstrate.  Say you want to restart Gnome Network Manager.  To do that you would type the following in a terminal:

sudo /etc/init.d/networking restart

Now you could make an alias called
netre to save yourself some typing and this tutorial will show you how.

Remember this tutorial is for BASH and not SH, CSH, Korn, etc. and more specifically for use with Ubuntu.  I cannot guarantee that this will work with other Linux distributions.

The first thing you will need to do if open your .bashrc file located in your home directory.  To do this type the following in a terminal:

gedit ~/.bashrc

This will bring up the Gedit text editor with the contents of the bashrc file.  Netx step is to locate the Alias definitions in the file.  You will need to uncomment the if statement to activate the alias definitions in a file or just add them directly in the .bashrc file.  The cleaner approach is to add them in a separate file for separation.  We will proceed with putting all your aliases in  separate file.  As you can see from the example listing I have uncommented the if statement to put all my definitions in a separate file.  Once this is done seave and exit gedit.

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

The next step is to create a new file called .bash_aliases in your home directory.  You can substitute another name other than .bash_aliases if you like.  To create the new file type the following in a terminal:

gedit ~/.bash_aliases

This will once again bring up the Gedit text editor with a blank text file.  We can now sart adding aliases to the file.  The format for adding an alias is as follows:

alias <desired alias>=’<linux_command>’

Here is an example:

alias netre=’sudo /etc/init.d/networking restart’

Make sure you use the tick mark ‘ when enclosing the linux command.  Once you are done adding the command hit the “Save” button.  Now the next time you bring up a terminal and type netre you will restart network manager.

You can add numerous alias commands to the file for your command line happiness.

Comments (0)

Tags: , , ,

Show Plesk Admin Password

Posted on 18 March 2013 by Chris

If you are using Plesk on Linux, have root access and need to recover the admin password, you can view it in plain text using the following command.




/usr/local/psa/bin/admin --show-password


This will pull the admin password from the database.  Simple enough, right?

Comments (1)

Tags: , , , , , , , ,

Using df to Show Disk Space Usage in Linux

Posted on 18 March 2013 by Chris

To display the disk space usage for your drives in Linux, you are able to use the df command.  By default, it will show the usage of all the mounted drives in 1k blocks.  If the POSIXLY_CORRECT environment variable is set though, it will show in 512 byte blocks by default.  Below shows the command run with no options.


chris@Desktop:~$ df
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sdc1      575758492 126365356 420575064  24% /
udev             1538192        12   1538180   1% /dev
tmpfs             619612      1088    618524   1% /run
none                5120         0      5120   0% /run/lock
none             1549020       144   1548876   1% /run/shm

I personally like to use the -h flag with it, which will show the space in human readable format, in KB and GB.


chris@Desktop:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc1       550G  121G  402G  24% /
udev            1.5G   12K  1.5G   1% /dev
tmpfs           606M  1.1M  605M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.5G  144K  1.5G   1% /run/shm

The -a flag will show all of the file systems, including dummy file systems.

chris@Desktop:~$ df -a
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sdc1      575758492 126365360 420575060  24% /
proc                   0         0         0    - /proc
sysfs                  0         0         0    - /sys
none                   0         0         0    - /sys/fs/fuse/connections
none                   0         0         0    - /sys/kernel/debug
none                   0         0         0    - /sys/kernel/security
udev             1538192        12   1538180   1% /dev
devpts                 0         0         0    - /dev/pts
tmpfs             619612      1088    618524   1% /run
none                5120         0      5120   0% /run/lock
none             1549020       144   1548876   1% /run/shm
binfmt_misc            0         0         0    - /proc/sys/fs/binfmt_misc
rpc_pipefs             0         0         0    - /run/rpc_pipefs
nfsd                   0         0         0    - /proc/fs/nfsd

Using df with the –total flag will show the drive spaces as well. The total space of all the drives together.

chris@Desktop:~$ df -h --total
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc1       550G  121G  402G  24% /
udev            1.5G   12K  1.5G   1% /dev
tmpfs           606M  1.1M  605M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.5G  144K  1.5G   1% /run/shm
total           553G  121G  405G  23%

Using the -i flag will show the number of inodes on the drives.  Inodes are the number of files and directories the disk contains.

chris@Desktop:~$ df -i
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
/dev/sdc1      36028416 525729 35502687    2% /
udev             210007    628   209379    1% /dev
tmpfs            215421    607   214814    1% /run
none             215421      5   215416    1% /run/lock
none             215421      4   215417    1% /run/shm

The -T flag can also be added to show the filesystem type, such as ext3 or NTFS.

chris@Desktop:~$ df -T
Filesystem     Type     1K-blocks      Used Available Use% Mounted on
/dev/sdc1      ext4     575758492 126365408 420575012  24% /
udev           devtmpfs   1538192        12   1538180   1% /dev
tmpfs          tmpfs       619612      1088    618524   1% /run
none           tmpfs         5120         0      5120   0% /run/lock
none           tmpfs      1549020       144   1548876   1% /run/shm

The df command also has a couple other options, but I won’t be covering those today since the aren’t usually needed regularly.  The help info for the df command is below for reference.

Usage: df [OPTION]... [FILE]...20         0      5120   0% /run/lockShow information about the file system on which each FILE resides,
or all file systems by default.

Mandatory arguments to long options are mandatory for short options too.

-a, --all             include dummy file systems
-B, --block-size=SIZE  scale sizes by SIZE before printing them.  E.g.,
`-BM' prints sizes in units of 1,048,576 bytes.
See SIZE format below.
--total           produce a grand total
-h, --human-readable  print sizes in human readable format (e.g., 1K 234M
-H, --si              likewise, but use powers of 1000 not 1024
-i, --inodes          list inode information instead of block usage
-k                    like --block-size=1K
-l, --local           limit listing to local file systems
--no-sync         do not invoke sync before getting usage info (defau
-P, --portability     use the POSIX output format
--sync            invoke sync before getting usage info
-t, --type=TYPE       limit listing to file systems of type TYPE
-T, --print-type      print file system type
-x, --exclude-type=TYPE   limit listing to file systems not of type TYPE
-v                    (ignored)
--help     display this help and exit
--version  output version information and exit

Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

SIZE may be (or may be an integer optionally followed by) one of following:
KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Report df bugs to bug-coreutils@gnu.org

GNU coreutils home page:

General help using GNU software:

For complete documentation, run: info coreutils 'df invocation'



Know some other things you can do with the df command?  Tell us about it in the comments!

Comments (0)

Tags: , ,

Apache Error Codes

Posted on 05 March 2013 by Chris

Below are a list of apache error codes. These can be helpful when checking for issues in the apache logs.

Continue Reading

Comments (0)

Tags: , , , , ,

Install and run ClamAV

Posted on 13 January 2012 by Chris

ClamAV is a popular malware scanner that can help to find malware on your accounts. You are able to find more information about that at the following link:

http://clamav.net

This software has many built in definitions that will find *most* of the malicious files under your accounts. It can find many shells, phishing sites and other malware. We won’t be able to cover all of the different options available in ClamAV in this article, but we will cover the parts that you will need to initially locate the malware so that it an be removed.

To install that, all you will need to do is run the following command.

If you are on a RedHat based OS, such as CentOS, you can install it with

yum install clamav

If you are using debian, you can use

apt-get install clamav

Once that is installed, you will want to run the freshclam command so that the definitions are updated to the most recent.

root@server [/home/user]# freshclam
ClamAV update process started at Thu Jan 12 04:41:48 2012
main.cvd is up to date (version: 54, sigs: 1044387, f-level: 60, builder: sven)
daily.cld is up to date (version: 14300, sigs: 70715, f-level: 63, builder: guitar)
bytecode.cvd is up to date (version: 160, sigs: 38, f-level: 63, builder: edwin)

Then, you can use the clamscan command to run the scan. You will also want to use a couple of flags to only show the infected files, to search recursively, and to log your findings to a log file. The i limits the output to only infected files, the r flag means to recurse through the directoies and the l flag with a file name will log the scan to that file.

root@server [/home/user/public_html]# clamscan -ir -l log.txt

———– SCAN SUMMARY ———–
Known viruses: 1113857
Engine version: 0.97.3
Scanned directories: 139
Scanned files: 1602
Infected files: 0
Data scanned: 29.30 MB
Data read: 15.53 MB (ratio 1.89:1)
Time: 6.608 sec (0 m 6 s)

If a malicious file is found, it will show the path to the file and why it was flagged.

root@server [/home/user/public_html]# clamscan -ir -l log.txt
/home/user/public_html/thing.php: PHP.Shell-38 FOUND

———– SCAN SUMMARY ———–
Known viruses: 1113857
Engine version: 0.97.3
Scanned directories: 2412
Scanned files: 20511
Infected files: 1
Data scanned: 354.85 MB
Data read: 832.57 MB (ratio 0.43:1)
Time: 102.922 sec (1 m 42 s)

The output of the scan will also be logged to a file called log.txt if you run the command as it is in the example. You can then get the timestamps from that file and find the source, remove the file and patch the problem.

Comments (0)

Create an ISO Image File from a CD or DVD

Posted on 17 April 2011 by Chris

To create an exact image of a cd or dvd, you are able to use the dd command. If your disk is mounted at /mnt/disk, you can use this command to make an image file named disk.iso.

dd if=/mnt/disk of=/home/disk.iso

In that command, dd is the program that is used, if is the input file, and of is the output file. The result of that will be an exact copy of the disk in /home named disk.iso.

Comments (0)

Create and Remove Directories

Posted on 25 March 2011 by Chris

To create a directory in Linux, you will use the mkdir command with the name of the directory that your want to create.

mkdir foldername

To delete an empty directory, you can use the rmdir command with the path to the folder that you want to remove.

rmdir foldername

If the folder is not empty though, it will give you an error if you use the rmdir command. Instead, you will have to use the rm command to delete the folder and everything in it. So if you are totally sure you do not need anything in the folder, you can use this:

rm -rfv foldername

That command will remove everything in the folder, outputting what it’s doing as it’s doing it.

Comments (0)





Download v1 of the RandomLinux.com Android app! Stay up to date on everything that's Linux right from your phone or tablet! Download below!