Category Archives: Notes for self

Linux: How do you find out what your server’s outgoing ip is?

There are many times when I needed to find out my outgoing (or external) IP for the servers which are behind load balancers or firewalls.  I used to just login to another external server from the server in question and find out by looking at “who” what my external ip is.  Even though it works and I am so used to it, today I decided to figure out a more graceful way of finding my outgoing ip.  As most of us already know, whatismyip.com is the quickest way to find out your outgoing ip from the browser.  So I decided to use the same way on the servers.  So I issued a wget:

wget http://www.whatismyip.org

Well that does the trick.  But being lazy as I am, I did not want to have to cat the output file to find out the ip (plus there is no point of creating extra files and doing extra work to remove them).  So if you are ssh’ed in, you can issue following command (I am sure there is another way of doing it, but this is the quickest way I could think of):

wget -q -O - http://www.whatismyip.org

-O tells wget to redirect output to the following file (- being the standard out ).  So it basically echo’s output to our console.

-q makes wget run in  quiet mode so you do not see all of the connection/download/etc output.

That is it!  I am curious to know what other ways people use to get the same information.  Please share your way if possible.

————————————-

DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

MySQL: How do I import individual table dump files in to MySQL using shell script?

After I wrote the post: How do I dump all tables in a database into separate files? I got emails from couple people asking how to import the individual table files back in to MySQL. First way to import each sql file created by the post is to import each file individually by typing:mysql db_name < table1.sql This will work as long as you are only importing few files. But if you need to import all of the files in the directory, which could be in 100’s, this method does not scale well. To achieve this I wrote a shell script which does the work for me. Of course, there are other ways to do this and I am only showing you one way of doing it. This works for me so here it is:

#!/bin/bash
db=$1
if [ "$db" = "" ]; then
echo "Usage: $0 db_name"
exit 1
fi
mkdir done
clear
for sql_file in *.sql; do
echo "Importing $sql_file";
mysql $db< $sql_file;
mv $sql_file done;
done

Related posts:

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

Mcache: Install and configure mcache (msession) to be used for session caching in PHP.

Installing mcache, previously known as msession, on CentOS 32 bit system.

If you are on 64 bit system, you will get errors, lots of them.  I went through and fixed “some” errors by modifying code but it was just taking too much time so I decided not to go 64 bit route.  But below are the efforts I made.  Maybe somebody can help with rest of the steps.   Following instructions work fine with 32 bit systems. For more help look at the MCache Handbook.
Thank you Mohawk Software for all your efforts developing this!

Web site: http://www.mohawksoft.org/?q=node/32
PHP reference:  http://us2.php.net/manual/en/ref.msession.php

yum install ncurses-devel #otherwise you might see errors about no such file curses.h

wget http://www.mohawksoft.org/download/mcache-070415-M2.0b6.tar.gz
wget http://www.mohawksoft.org/download/phoenix-070415-M2.0b6.tar.gz
tar zxf mcache*.gz
tar zxf phoenix*.gz
cd phoenix
ln -s Linux.mak config/config.mak
make dirs
make links
make butils
export PATH=$PATH:/opt/mohawk/bin
make libs
vi sqldrv/Makefile # about line 24, comment out POSTGRES=1 and ODBC=1
cd ../mcac*
make server
vi tools/Makefile # after line which says: CARGS+=-DBINDIR=\"$(BINDIR)\"
                  # add:  CARGS+=-DSBINDIR=\"$(SBINDIR)\"
make utils
ln -s /opt/mohawk/lib/libphoenix.so.2.2.2 /lib/
/opt/mohawk/sbin/mcache &

In your php.ini, add:   
[Session]
; Use mcache as the save handler
session.save_handler = mcache
; Set the host which runs the mcache daemon
session.save_path = localhost

Let us test if mcache server is running:

/opt/mohawk/bin/mping
You should see:
Usage: /opt/mohawk/bin/mping host
Pings a session daemon
Using localhost
localhost:8086 is alive

Great! You mcache server is now up and running and listening for connections.

You can add above command to your start up scripts so server will run next time you reboot.  Easiest way to achieve this is to add that command to end of /etc/rc.local

32 bit installation is now complete! 

———————

I could not get 64 bit install to go but here are my notes for whoever wants to try it.  If you get it working, please come back and comment on how you got it installed.

64 bit install notes:First error:  CPU you selected does not support x86-64 instruction set
to fix this, edit config/unixgcc.mak and remove all instances of -mtune=pentium3

Following are code changes to fix some other errors:

line 83 in phmalloc.h
virtual void *memdup(void *mem, unsigned int cb);
to
virtual void *memdup(void *mem, size_t cb);

line 446 in mexpat.cpp
static void *xmlalloc(void *context, unsigned int cb)
to
static void *xmlalloc(void *context, size_t cb)

static void *xmlrealloc(void *context, void *p, unsigned int cb)
to
static void *xmlrealloc(void *context, void *p, size_t cb)

TIP: How do I rename multiple files to another extension?

Let us say that you want to rename all of your “.php3” files to “.php” files. How you do this with minimal effort? Answer lies in for loop.

for old in *.php3; do cp $old `basename $old .php3`.php; done

Thats all there is to it. Let us say you need to rename index.php3 to index.php. The way above snippet works is that it loops through current directory and finds all the files with extension “.php3” and processes ’em one by one. In our index.php3 file example, it finds index.php3 file, does a cp index.php3 `basename index.php3 .php3`.php <- basename returns “index” so you add .php to it and now you are left with index.php. Of course you can do mv command if you want to just move the file to new name. I would suggest doing cp first to make sure things work the way you want ’em to.

/bin/rm: Argument list too long

You type: rm * -f
You get: /bin/rm: Argument list too long
Reason: in shell when you do * after rm and get Argument list too long, your shell actually expands that * and puts all the filenames after rm and since length of the command line is limited, you end up getting argument list too long.

Possible solutions:

  1. You can remove the whole directory and recreate the directory
  2. When 1 is not an option, you can you “find” to delete for you. I actually use this method more often then removing/creating directory.
    find . -exec rm {} \; This will delete everything in current directory on. Be careful with this command since you could potentially delete things you may not wanted to. I suggest running find . |more first to see what will be affected.  To read more about find see:  man find

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.