Programmer's Blog

Programmer's reference

Monthly Archives: June 2014

[Linux] Setting Sync to Time Servers

1. check connectivity of time server
#> ntpdate -q 0.rhel.pool.ntp.org

2. test update once to the server
#> ntpdate 0.rhel.pool.ntp.org 1.rhel.pool.ntp.org

3. check the service is on
#> chkconfig ntpdate on

4. To receive periodical broadcasts from NTP server modify
/etc/ntp.conf

..
server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst

//ibrust is to speed up the synchronization at the beginning
//note that the ntp time is adjusted gradually.

5. restart ntp service
#> service ntpd restart

6. check ntpd is on
#> chkconfig ntpd on

check synchronization status
#> ntpstat

synchronised to NTP server (223.255.185.2) at stratum 2
 time correct to within 939 ms
 polling server every 64 s

#> ntpq -p

 remote refid st t when poll reach delay offset jitter
=====================================================
+223.255.185.2 .MRS. 1 u 38 64 177 6.715 -12.015 7.847
*118.143.17.82 .MRS. 1 u 39 64 177 4.685 -11.536 7.748


source : access.redhat.com

[Linux] Disable echo ^C for pressing Ctrl-C

Just change the terminal setting using stty

$> ^C   (Ctrl-C)
$> ^C   (Ctrl-C)
....

$> stty sane
$> stty -echoctl

....
$>      (Ctrl-C)
$>      (Ctrl-C)

[Linux] Add user to sudo list

The command sudo can run a root command from a non-root user like sudo reboot

To add a user to sudo, type

root#> visudo

This will open /etc/sudoers, Go to the line

root  ALL=(ALL)  ALL

add an record below this line and save

root  ALL=(ALL)  ALL
john  ALL=(ALL)  ALL

[Linux] Dependancy problem in rpm

When you install rpm files, very often they have mutual dependancies like glibc and glibc-common

#> yum install glibc-common-2.12-1.107.el6.x86_64.rpm
......
Error: Package: glibc-common-2.12-1.107.el6.x86_64 (@anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4)
           Requires: glibc = 2.12-1.107.el6
           Removing: glibc-2.12-1.107.el6.x86_64 (@anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4)
               glibc = 2.12-1.107.el6
           Updated By: glibc-2.12-1.132.el6.x86_64 (/glibc-2.12-1.132.el6.x86_64)
               glibc = 2.12-1.132.el6
Error: Package: glibc-2.12-1.132.el6.x86_64 (/glibc-2.12-1.132.el6.x86_64)
           Requires: glibc-common = 2.12-1.132.el6
           Installed: glibc-common-2.12-1.107.el6.x86_64 (@anaconda-RedHatEnterpriseLinux-201301301459.x86_64/6.4)
               glibc-common = 2.12-1.107.el6

glibc requires glib-common and vice versa, therefore try to install the two packages at one time

#> yum install glibc-common-2.12-1.107.el6.x86_64.rpm glibc-2.12-1.107.el6.x86_64.rpm

[RHEL] Allow SSH connection

1. open
/etc/ssh/sshd_config

2. unsharp the statement
PermitRootLogin=yes

3. restart the service by
#> service sshd restart

[RHEL] setting up network on eth0

1. use system GUI configuration
#> system-config-network-tui

2. since the onboot option is set to "no" we need to open up
/etc/sysconfig/network-scripts/ifcfg-eth0

and change to onboot=yes

3. then restart the network
#> /etc/init.d/network restart

[ZeroMQ] Client Server C++ example without middleman

 

fig55

The Freelance Model Two: Brutal Shotgun Massacre

  • We set things up, connecting to all servers.
  • When we have a request, we blast it out as many times as we have servers.
  • We wait for the first reply, and take that.
  • We ignore any other replies

Server Program

//-----------------ZMQ_CPP_FREELANCE2_SERVER---------------
#include <zmq.hpp>
#include <zmsg.hpp>
#include <iostream>

int main(int argc, char *argv[])
{
    if (argc < 2) {
        std::cout << "Error: syntax should be " << argv[0] <<
        " <endpoint> \n";
        return 0;
    }

    zmq::context_t * ctx = new zmq::context_t(1);
    zmq::socket_t * server = new zmq::socket_t(*ctx, ZMQ_REP);
    server->bind(argv[1]);

    std::cout << "Service is ready at " << argv[1] << std::endl;
    while (1) {
        zmsg *msg = new zmsg(*server);
        if (!msg)
        break;

        assert(msg->parts() == 2);

        std::string identity = (char *) (*msg).pop_front().c_str();
        std::string sequence = (char *) (*msg).pop_front().c_str();

        zmsg *reply = new zmsg();
        reply->push_front(const_cast<char*>(identity.c_str()));
        reply->push_front(const_cast<char*>(sequence.c_str()));
        reply->send(*server);
    }

    if (s_interrupted)
        std::cout << "Interrupted \n";

        ctx->close();
    return 0;
}

Client Program

//---------------ZMQ_CPP_FREELANCE2_CLIENT------------
#include <zmq.hpp>
#include <zmsg.hpp>
#include <zhelpers.hpp>
#include <iostream>

#define GLOBAL_TIMEOUT 2500

typedef struct _flclient_t flclient_t;
flclient_t *flclient_new(void);
void flclient_destroy(flclient_t ** self_p);
void flclient_connect(flclient_t * self, char *endpoint);
zmsg *flclient_request(flclient_t * self, zmsg ** request_p);

struct _flclient_t {
    zmq::context_t * ctx;
    zmq::socket_t * socket;
    size_t servers;
    uint sequence;
};

flclient_t *flclient_new(void)
{
    flclient_t *self;
    self = (flclient_t *) calloc(1, sizeof(flclient_t));
    self->ctx = new zmq::context_t(1);
    self->socket = new zmq::socket_t(*(self->ctx), ZMQ_DEALER);
    return self;
}

void flclient_destroy(flclient_t ** self_p)
{
    assert(self_p);
    if (*self_p) {
        flclient_t *self = *self_p;
        (self->ctx)->close();
        free(self);
        *self_p = NULL;
    }
}

void flclient_connect(flclient_t * self, char *endpoint)
{
    assert(self);
    (*(self->socket)).connect(endpoint);
    self->servers++;
}

zmsg *flclient_request(flclient_t * self, zmsg ** request_p)
{
    int debug = 0;
    assert(self);
    assert(*request_p);
    zmsg *request = *request_p;

    char sequence_text[10];
    sprintf(sequence_text, "%u", ++self->sequence);
    request->push_front(sequence_text);
    request->push_front("");

    int server;
    for (server = 0; server < self->servers; server++) {
         zmsg *msg = new zmsg(*request);
         msg->send(*(self->socket));
    }

    zmsg *reply = NULL;
    std::string part1, part2, part3;
    part1.clear();
    part2.clear();
    part3.clear();
    uint64_t endtime = s_clock() + GLOBAL_TIMEOUT;
    while (s_clock() < endtime) {
        std::cout.clear();
        zmq::pollitem_t items[] = {
{
        *(self->socket), 0, ZMQ_POLLIN, 0}
};
    int rc = zmq::poll(items, 1, (endtime - s_clock()) * 1000);
    if (rc == -1)
            break;
    std::cout.clear();
    if (items[0].revents & ZMQ_POLLIN) {
        reply = new zmsg(*(self->socket));
        assert(reply->parts() == 3);

        part1 = (char *) reply->pop_front().c_str();
        part2 = (char *) reply->pop_front().c_str();
        part3 = (char *) reply->pop_front().c_str();

        int sequence_nbr = atoi(part3.c_str());

        if (sequence_nbr == self->sequence)
            break;
        reply->clear();
}
    }

    (**request_p).clear();
    return reply;
}

int main(int argc, char *argv[])
{
    if (argc == 1) {
        std::cout << "Error: syntax should be " << argv[0] <<
        " <endpoint>... \n";
        return 0;
    }

    flclient_t *client = flclient_new();
    int argn;
    for (argn = 1; argn < argc; argn++)
        flclient_connect(client, argv[argn]);

        int requests = 10000;
        uint64_t start = s_clock();
        while (requests--) {
            zmsg *request = new zmsg();
            request->push_front("random name");
            zmsg *reply = flclient_request(client, &request);
            if (!reply) {
                std::cout << "name service not availbale, aborting...\n";
                break;
            }
            reply->clear();
    }
    std::cout << "Average round trip cost: " << (int) (s_clock() -
        start) / 10 << " usec \n";

    flclient_destroy(&client);
    return 0;
}

[Linux] Intersting Commands

There are some interesting commands in linux that you may not discover before

1. convert awk into perl code
$> ps -ef | awk '{print $2}' | a2p

2. system testing (memory and io)
$> amtu

3. Login time statistics (daily and user)
$> ac -d
$> ac -p

4. check last login of users
$> last
$> lastlog

4. spell check tool
$> aspell check test.txt

5. write file to cd
$> cdrwtool -f file.name

6. change finger information
$> chfn

7. change shell (bash/tcsh/zsh)
$> chsh

8. check sum and file size
$> cksum file.name

9. remove dangling symbolic link and empty folders
$> cleanlinks

10.interactive c program examiner
$> cscope

11. display the html content of website
$> curl

12. convert image to and from jpeg
$> cjpeg
$> djpeg

13. eject media
$> eject

14. convert tab to sapces/reverse
$> expand file.name
$> unexpand file.name

15. factoring number
$> factor 1236

16. display font list
$> fc-cat
$> fc-list

17. find executable path (same as whereis -b, but output pure path)
$> fexe

18. show file type and information (e.g. compress mode)
$> file

19. compress executable, but run with lower performance
$> gzexe mainprogram

20. perform get post action in command line
$> HEAD get http://www.google.com

21. display file in dex form
$> hexdump file.name

22. get id for host
$> hostid

23. generate diff file, difference between two files
$> diff a.txt b.txt

24. show difference between 2 diff files
$> interdiff a.diff b.diff

25. display library information of executable
$> ldd executable

26. get locale information
$> locale

27. display lines start with 'somestring'
$> look somestring

28. generate md5 or shas
$> md5sum filename
$> sha1sum filename
$> sha224sum filename
$> sha256sum filename
$> sha384sum filename
$> sha512sum filename

29. input mouse movement to console
$> mev

30. display CPU stats
$> mpstat

31. scan open ports
$> nmap -vAP0 localhost

32. see synchronize clock info
$> ntpstat

33. user information
$> finger
$> pinky

34. display memory map of a process
$> pmap pid

35. print stack
$> pstack pid

36. display working directory of a process
$> pwdx pid

37. print environment
$> printenv

38. display process tree
$> pstree

39. display value of symbolic link
$> readlink symlink

40a. reverse all the charcter in lines of a file
$> rev file.txt

40b. print file in reverse lines order
$> tac file.txt

41. display hourly system information
$> sar

42. show pci devices
$> scanpci

43. print sequence of numbers
$> seq 100
$> seq 1 50
$> seq 1 2 100

44. set leds on keyboard (not supported on red hat)
$> setleds +num
$> setleds +scroll
$> setleds -caps

45. show RGB colors and names
$> showrgb

46. detailed analysis of C program code
$> splint main.cc

47. display file modifications
$> stat file.name

48. write typed content (stdin) to file
$> tee file.txt

49. the vim tutorial
$> vimtutor

49. print all possible terminals
$> toe


[Linux] scanning open ports using nmap

nmap -v -A http://mysite.com

To search for the operation system
namp -O http://mysite.com

to bypass ping
nmap -v -A -P0 http://mysite.com

TCP Connect scanning for localhost and network 192.168.0.0/24
# nmap -v -sT localhost
# nmap -v -sT 192.168.0.0/24

nmap TCP SYN (half-open) scanning
# nmap -v -sS localhost
# nmap -v -sS 192.168.0.0/24

nmap TCP FIN scanning
# nmap -v -sF localhost
# nmap -v -sF 192.168.0.0/24

nmap TCP Xmas tree scanning
Useful to see if firewall protecting against this kind of attack or not:
# nmap -v -sX localhost
# nmap -v -sX 192.168.0.0/24

nmap TCP Null scanning
Useful to see if firewall protecting against this kind attack or not:
# nmap -v -sN localhost
# nmap -v -sN 192.168.0.0/24

nmap TCP Windows scanning
# nmap -v -sW localhost
# nmap -v -sW 192.168.0.0/24

nmap TCP RPC scanning
Useful to find out RPC (such as portmap) services
# nmap -v -sR localhost
# nmap -v -sR 192.168.0.0/24

nmap UDP scanning
Useful to find out UDP ports
# nmap -v -O localhost
# nmap -v -O 192.168.0.0/24

nmap remote software version scanning
You can also find out what software version opening the port.
# nmap -v -sV localhost
# nmap -v -sV 192.168.0.0/24

[C] Binary semaphore example

/*a synchronization example*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

void *thread_fnc(void * arg);

sem_t my_semaphore;
char my_string[100];

int main()
{
   int value;
   pthread_t my_thread;
   void *ret_join;

   if(sem_init(&my_semaphore, 0, 0)) {
     perror("semaphore init failed\n");
     exit(1);
   }

   if(pthread_create(&my_thread, NULL, thread_fnc, NULL)) {
     perror("pthread_create failed\n");
     exit(1);
   }

   while(strncmp("quit", my_string, 4) != 0) {
     fgets(my_string, 100, stdin);
     //sem_post will resume the thread which waiting at sem_wait()
     sem_post(&my_semaphore);
   }

   if(pthread_join(my_thread, &ret_join)) {
     perror("pthread_join failed");
     exit(1);
   }
   printf("Thread joined(%s\n)", (char *) ret_join);
   sem_destroy(&my_semaphore);
   exit(0);
}

void *thread_fnc(void *arg)
{
   int val;

   while(strncmp("quit", my_string, 4) != 0) {

   sem_wait(&my_semaphore);
   sscanf(my_string, "%d", &val);

   printf("%d \n",val);

   }
   pthread_exit(NULL);
}


result:
89   //input
89   //display from thread_fnc
2449
2449
...