Using MyTop – Ubuntu

Posted by | Posted in Command-Line, Linux, MySQL, Ubuntu | Posted on 20-07-2009

If you ever need to know how well your databases are being queried, mytop is the key. MyTop is a clone of Top for MySQL.

Your Ubuntu server won’t come pre-installed with mytop, so you’ll have to to apt-get to install it.

/usr/bin/sudo /usr/bin/apt-get install mytop

Once mytop is installed and your mysql server is setup, you can view your database stats.

-u : user account
-p : password
-h : server, normally localhost
-d : database you want to view
-P : port, if different from default


You should see something like this come up.

MySQL on localhost (5.0.75-0ubuntu10.2)    up 0+00:32:06 [02:09:34] Queries: 5.0
qps:    0 Slow:     0.0         Se/In/Up/De(%):    00/00/00/00   qps now:    0 
Slow qps: 0.0  Threads:    1 (   1/   0) 00/00/00/00  Key Efficiency: 90.0%  
Bps in/out:   0.1/  6.9   Now in/out:   8.3/ 1.3k
 Id      User         Host/IP         DB      Time    Cmd Query or State 
 --      ----         -------         --      ----    --- ----------  
38      root       localhost      mysql         0  Query show full processlist

to exit mytop, press the “Q” key.

Using PHP to return an IP address

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

After I’ve determine which interfaces are active on my computer(s), I can use this script to report back the IP address of said interface.

# input String ("en0")
# output String IP
function get_network_interface_ip($interface) {
  @exec('/usr/sbin/ipconfig getifaddr '. $interface, $retval);
 return $retval[0];
}// end get_network_iterface_ip
echo get_network_interface_ip($interface);

Using PHP to report an active network interface

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script, after I determine what available interfaces the computer has, to report which interfaces are active.

# input String ("en0")
# output "NULL or "Active" 
function check_interface_activity($interface){
  @exec('ifconfig| awk \'/flags=|media|inet / {if (substr($2, 1, 6) == "flags=") printf("\n%s ", $1);
  else if ($1 == "inet") printf("%s ", $2);
  else if ($1 == "media:") printf(substr($0, 9))}\' | awk \'$1 ~ /^'.$interface.'/\' | awk \'{print $7}\'',$retval);
 return $retval[0];
} // end check_interface
echo check_interface($interface);

Using PHP to gather all active network interfaces

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script to gather all Ethernet based network activity to determine which network port is active and then determine what the hardware address and IP address are.

# input NONE
# output String ("en0, en1")
function get_all_active_network_interfaces() {
  $inclusion = array(en0,en1);
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
  foreach($interfaces as $interface){
     if (in_array($interface,$inclusion)){
       $ret.=$interface ." \n \r";
     } // end if
  }// end foreach
return $ret;
} // end get_all_active_network_interfaces
echo get_all_active_network_interfaces();

This script is similar to the above script, but doesn’t include the constraints of the inclusion list.

# input NONE
# output String ("en0, en1, etc") 
function get_all_active_network_interfaces() {
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
    foreach($interfaces as $interface){
      $ret .= $interface ." \n \r";
    }// end foreach
return $ret;
} // end get_all_active_network_interfaces
echo get_all_active_network_interfaces();

Using PHP to Connect to a MySQL server

Posted by | Posted in Code, MySQL, PHP | Posted on 18-07-2009

This is how I normally link PHP to my MySQL servers. I use this code normally for my shell scripting, but it also works for websites.

$mysql_server = "SERVER_ADDRESS";
$mysql_user = "SERVER_USER";
$mysql_password = "SERVER_PASSWORD";
$mysql_database = 'SERVER_DATABASE';
$mysql_table = 'SERVER_TABLE';
$mysql_connection = @mysql_connect($mysql_server,$mysql_user,$mysql_password);
if (!$mysql_connection)
  DIE ('ERROR 10: No Connection to '.$mysql_server.' '. mysql_error() ."\n");
$mysql_database_connection = @mysql_select_db($mysql_database,$mysql_connection);
if (!$mysql_database_connection)
  DIE ('ERROR 30 : Could not select database '. $mysql_database .' '. mysql_error() ."\n");
echo "MySQL : Connection Active \n";

How to use scp – Ubuntu

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 17-07-2009

I am currently switching web servers at work from a physical server to a virtual server. The differences are for a different topic, but during this switch, I am going to be copying files from my old server to my new one. Normally you’d copy your files down to your local computer, lets say your laptop or desktop, before upload them to your new server. The not only takes a lot of time, but also eats up hard drive space. I am going to show you how to copy directly from one server to another.

I am going to use SSH’s SCP application. /usr/bin/scp – secure copy (remote file copy program).

For general discussion, I am already remotely logged into my old server via SSH and I have the files that I want to transfer up already TAR’d up. I’ll cover SSH and TAR in later posts. But for right now, I am just going to demonstrate how you should use SCP.

The usage format to transfer a file “up” to another system normally looks something like this.,

/usr/bin/scp /path/to/local.file user@server.address:/path/to/new/location.file

I am going to copy a mysql (database) file from my old server’s backup directory to my home directory on the new server

/usr/bin/scp /backups/mysql_database_dump.sql.tar's password: 
mysql_database_dump.sql.tar              100%  391MB   6.4MB/s   01:01

Now, if I wanted to copy a file from my new server to my old server, while still on my old server, I would reverse the sequence.

scp /path/to/new/location/'s password: 
mysql_database_dump.sql.tar              100%  391MB   6.4MB/s   01:01

Family Commands of scp are

/usr/bin/rcp - alias of scp - secure copy (remote file copy program)
/usr/bin/sftp - secure file transfer program
/usr/bin/ssh - OpenSSH SSH client (remote login program)
/usr/bin/ssh-add - adds RSA or DSA identities to the authentication agent
/usr/bin/ssh-agent - authentication agent
/usr/bin/ssh-keygen - authentication key generation, management and conversion
/etc/ssh/ssh_config - OpenSSH SSH client configuration files
/usr/sbin/sshd - OpenSSH SSH daemon

How to change your default shell – Ubuntu

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 17-07-2009

If I am not using OS X, then I normally use Ubuntu. On a normal install of Ubuntu, your default command-line shell is “/bin/sh” – command interpreter (shell). To me, a low powered command shell. I normally switch it from “/bin/sh” to “/bin/bash” or “/usr/bin/bash” depending on where bash is installed.

The easiest way to do this is to use the “chsh” command. chsh stands for “change login shell”


A typical change shell sequence looks something like this.

1) Find the current location of the shell you want to run. To to this, you use “which”.
2) Use the “chsh” program.
3) Enter your password.
4) Enter the new shell you want to use.

$ which bash
$ chsh    
Changing the login shell for USER
Enter the new value, or press ENTER for the default
	Login Shell [/bin/sh]: /bin/bash
$ exit

In-order to use your new shell, you have to logout and log back in.

Family Commands for chsh are

/usr/bin/chfn - change real user name and information
/etc/login.defs - shadow password suite configuration
/usr/bin/passwd - change user password

Upgrading Software – Ubuntu Server Edition

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 17-07-2009

When it comes to any server, you normally have to update its software remotely. Either by command shell or remote desktop. For this demonstration, I am going to access my server via SSH and use the Ubuntu/Debian package manager called “/usr/bin/apt-get” – APT package handling utility — command-line interface.

user@server:~$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages have been kept back:
  linux-image-server linux-restricted-modules-server linux-server
The following packages will be upgraded:
  acpid apache2 apache2-mpm-prefork apache2-utils apache2.2-common ..... etc.....
47 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
Need to get 57.4MB of archives.
After this operation, 20.5kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
Get:1 jaunty-updates/main perl-modules 5.10.0-19ubuntu1.1 [3182kB]
etc until all the applications have downloaded
Fetched 57.4MB in 48s (1173kB/s)                                                                                                                                                  
Extracting templates from packages: 100%
Preconfiguring packages ...
(Reading database ... 24637 files and directories currently installed.)
Preparing to replace perl-modules 5.10.0-19ubuntu1 (using .../perl-modules_5.10.0-19ubuntu1.1_all.deb) ...
Unpacking replacement perl-modules ...
Now everything application that needed updated, will unpack and install.
If any application (daemons) that are running, are upgraded, apt-get automatically restarts those programs.
Setting up apache2 (2.2.11-2ubuntu2.2) ...
Setting up libapache2-mod-php5 (5.2.6.dfsg.1-3ubuntu4.1) ...
 * Reloading web server config apache2

and it is just as simple as that.

Family Commands for apt-get are

/usr/bin/apt-cache - APT package handling utility -- cache manipulator
/usr/bin/apt-cdrom - APT CDROM management utility
/usr/bin/dpkg - package manager for Debian
/usr/bin/dselect - Debian package management frontend
/etc/apt/sources.list -
/etc/apt/apt.conf -  Configuration file for APT
/usr/bin/apt-config - APT Configuration Query program
/usr/bin/apt-secure - Archive authentication support for APT
/etc/apt/preferences - Preference control file for APT - apt_preferences
APT User’s guide in /usr/share/doc/apt-doc/

How to change your password – Ubuntu

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 17-07-2009

Changing your password is something you should do on a monthly or weekly basis. This prevents anyone that has glanced over your shoulder while walking by from accessing your private information.

On a normal Linux computer, you can simply use the command “passwd” – change user password.

A typical Linux sequence looks like this

user@server:/$ passwd
Changing password for user.
(current) UNIX password: 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

Family Commands for passwd are

/etc/group - user group file - group
/usr/bin/passwd - change user password
/etc/passwd- encrypted password file - shadow

OS X’s scutil

Posted by | Posted in Bash, Code, Command-Line, OS X | Posted on 17-07-2009

scutil is OS X’s way of managing system configuration parameters. These parameters not only include HostName, ComputerName, LocalHostName, but basically anything that the configd daemon uses.

Most of those files are located in


To enter the scutil prompt, type


To view all the scutil commands, type

> help

To get a list of all the scutil keys, type “list” or “list + [Pattern]”

> list

To view the contents of a key, type “show + [Pattern”

> show Setup:/Network/Global/IPv4

This will result in a pattern like this.

< dictionary > {
  ServiceOrder :  {
    0 : 59267386-49A6-401F-A23E-BE372AD748EB
    1 : 68719F97-53F0-479E-A497-8A3B809728D1
    2 : E936454A-40CA-4867-A759-61D6C38F5972

To get your computer name, local host name or host name type,

/usr/sbin/scutil --get ComputerName
/usr/sbin/scutil --get LocalHostName
/usr/sbin/scutil --get HostName

And setting those names are just as easy.

/usr/sbin/scutil --set ComputerName NEWNAME
/usr/sbin/scutil --set LocalHostName NEWNAME
/usr/sbin/scutil --set HostName NEWNAME

On a side note. ComputerName HostName can be set to blank, but LocalHostName needs at least 1 character.

To quit scutil type,

> quit

Family Commands of scutil are

/usr/sbin/scutil  -- Manage system configuration parameters
/usr/sbin/scselect  -- Select system configuration "location"
/usr/sbin/configd -- -- System Configuration Daemon

Join the mailing list

Check your email and confirm the subscription