Archive for July, 2011

command line network tools on linux

July 29th, 2011

netstat

  • netstat -tulpan:
  • netstat -tl
  • netstat -anp –tcp –udp: will show all open tcp and udp connections.

lsof

  • lsof -i
  • lsof -i | grep 80
  • lsof -i | grep apache2

iftop

display bandwidth usage on an interface by host

configure mail server EXIM on debian

July 29th, 2011

Configuration

use the command:

dpkg-reconfigure exim4-config

and follow the instructions.

Error handle

If there is an error message saying:

ALERT: exim paniclog /var/log/exim4/paniclog has non-zero size, mail system possibly broken ... failed!
You can fix it by simply removing the log file by:
rm /var/log/exim4/paniclog
And then restart the server:
 /etc/init.d/exim4 restart

Install & use mysql++ on linux(debian squeeze, ubuntu)

July 28th, 2011

The key to use mysql++ on debian/ubuntu system is to find where the mysql++ .h head files and the .so binary files.

Firrst install mysqlclient and mysql++ on debian squeeze:

root# apt-get install libmysqlclient-dev libmysqld-dev libmysql++-dev libmysql++-doc libmysql++3

This will install the mysql head files in /usr/include/mysql, mysql++ head files in /usr/include/mysql, and libmysqlpp.so and libmysqlclient.so in /usr/lib. These are all the files we need.

Now let us use the mysql++ library.

Save the simple code below as test.cpp

#include <iostream>
#include <mysql++.h>

int main() {

mysqlpp::String s(“hello, world”);

std::cout << s << std::endl;

return 0;

}

And save the makefile as Makefile:

CXX := g++
CXXFLAGS := -I/usr/include/mysql -I/usr/include/mysql++

LDFLAGS := -L/usr/lib -lmysqlpp -lmysqlclient -lnsl -lz -lm
EXECUTABLE := main

all: test
clean:        rm -f $(EXECUTABLE) *.o

Finally, excute

make all

The compile result would be test.

javaps – list java processes on linux

July 28th, 2011

here is a script that you can use to list java processes on linux.

http://www.cs.columbia.edu/~akonstan/javaps/javaps

here is code:

#!/bin/bash
#
# Copyright (c) 2000 Alexander V. Konstantinou 
#
# Permission to use, copy, modify, distribute and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation. Alexander V. Konstantinou makes no
# representations about the suitability of this software for any
# purpose. It is provided "as is" without express or implied warranty.
#
# Description: prints out the java processes running on a Linux host.
# Optional "-noflags" argument controls verbosity
#
# Requirements: Linux host with a /proc filesystem
#
# Updates: http://www.cs.columbia.edu/~akonstan/javaps
#
# $Id: javaps,v 1.3 2002/01/21 02:32:12 akonstan Exp $

PATH=/bin:/usr/bin

SYSNAME=`uname -s`

if [ "$SYSNAME" != "Linux" ]; then
echo "$0: must be executed on Linux system (found $SYSNAME)" >&2
exit 1
fi

if [ ! -d "/proc" ]; then
echo "$0: Linux host does not appear to have a /proc filesystem" >&2
exit 1
fi

NOFLAGS=0
if [ -n "$1" ]; then
if [ "$1" = "-noflags" ]; then
NOFLAGS=1
else
echo "Usage: $0 { -noflags }" >&2
exit 1
fi
fi

echo "PID Process"

# For every file in the /proc file system

FILES=`/bin/ls -1 /proc`

for FILE in $FILES; do
PROCESS_ID=$FILE
STATUS_FILE=/proc/$FILE/status
CMDLINE_FILE=/proc/$FILE/cmdline

# Check if it is a process directory and that we have read access
if [ -d "/proc/$FILE" -a -r $STATUS_FILE -a -r $CMDLINE_FILE -a "$FILE" != "/proc/self" ]; then

PROCESS_NAME=`grep 'Name:' $STATUS_FILE | awk '{print $2}'`

# Only interested in java processes
if [ "$PROCESS_NAME" = "java" ]; then
PARENT_PID=`grep 'PPid' $STATUS_FILE | awk '{print $2}'`

# Figure out if process has a parent that is a java process
ISROOT=0
if [ $PARENT_PID -eq 1 ]; then
ISROOT=1
else
PARENT_NAME=`grep 'Name:' /proc/$PARENT_PID/status | awk '{print $2}'`
if [ "$PARENT_NAME" != "java" ]; then
ISROOT=1
fi
fi

# If root java process print out
if [ $ISROOT -ne 0 ]; then
PROCESS_CMDLINE="`cat $CMDLINE_FILE | tr '00' ' '`"

IGNORE_ARG=0
DESCR=
for ARG in $PROCESS_CMDLINE; do
if [ $NOFLAGS -eq 0 ]; then
DESCR="$DESCR $ARG"
else
if [ $IGNORE_ARG -eq 0 ]; then
if [ $ARG = "-classpath" -o $ARG = "-cp" ]; then
IGNORE_ARG=1
elif [ -z "`echo $ARG | grep '^-D'`" -a -z "`echo $ARG | grep '^-X'`" ]; then
DESCR="$DESCR $ARG"
fi
else
IGNORE_ARG=0
fi
fi
done
echo "$PROCESS_ID $DESCR"
fi
fi
fi
done


allow mysql to get outer connections

July 26th, 2011

If you want your mysql for outer connections, do not forget to set :

bind-address            = 0.0.0.0

in my.cnf.

Otherwise, you will get the error message like this: ”the target machine actively refused it”.

problem of returning *this in C++

July 5th, 2011

I have a small class, in one of whose methods I want to return the instance itself so I can continue to use the instance and thus save inputting. However, “return *this” seems not working as waht I expected. The class is like this:

UndirectedGraph.h

/*
* UndirectedGraph.h
*/

#ifndef UNDIRECTEDGRAPH_H_
#define UNDIRECTEDGRAPH_H_

#include <utility>
#include <set>

using namespace std;

namespace wp {

class UndirectedGraph {
public:
UndirectedGraph(size_t n);
virtual ~UndirectedGraph();

UndirectedGraph
//void
addEdge(size_t node1, size_t node2);

private:
size_t len;
set *nbs;
};

}

 

#endif /* UNDIRECTEDGRAPH_H_ */

UndirectedGraph.cpp

/*
* UndirectedGraph.cpp
*/
#include <utility>
#include <set>
#include <iostream>
#include "UndirectedGraph.h"

using namespace std;

namespace wp {

UndirectedGraph::UndirectedGraph(size_t n) : len(n) {
std::cout << "constructor with len: " << this->len << std::endl;
for(size_t node = 0; node != n; ++node) {
nbs = new set[this->len];
}
}

UndirectedGraph::~UndirectedGraph() {
delete[] nbs;
std::cout << "destructor finished" << std::endl;
}

UndirectedGraph
//void
UndirectedGraph::addEdge(size_t node1, size_t node2) {
if(node1 >= this->len || node2 >= this->len)
throw 1;
if(node1 == node2)
throw 2;
this->nbs[node1].insert(node2);
this->nbs[node2].insert(node1);
return *this;
}

 

 

}

And in the main() function:

int main() {
UndirectedGraph g(7);
g.addEdge(0, 1).addEdge(0, 2)
.addEdge(1, 3).addEdge(1, 5);
return EXIT_SUCCESS;
}

What am I missing?

————– EDIT ————-
The solution to the problem is to return *this as a reference to itself, as below:
UndirectedGraph & addEdge(size_t node1, size_t node2);