Programmer's Blog

Programmer's reference

Monthly Archives: February 2014

Capture packet using tcpdump

create a capture file for wireshark using tcpdump.

This is to monitor a particular host of the local machine.

The option “0” is zero.

tcpdump -s 0 -vvvv -w packet.pcap port <portnum>

See the list of interfaces on which tcpdump can listen:
tcpdump -D

Listen on interface eth0:
tcpdump -i eth0

Listen on any available interface (cannot be done in promiscuous mode. Requires Linux kernel 2.2 or greater):
tcpdump -i any

Be verbose while capturing packets:
tcpdump -v

Be more verbose while capturing packets:
tcpdump -vv

Be very verbose while capturing packets:
tcpdump -vvv

Be less verbose (than the default) while capturing packets:
tcpdump -q

Limit the capture to 100 packets:
tcpdump -c 100

Record the packet capture to a file called capture.cap:
tcpdump -w capture.cap

Record the packet capture to a file called capture.cap but display on-screen how many packets have been captured in real-time:
tcpdump -v -w capture.cap

Display the packets of a file called capture.cap:
tcpdump -r capture.cap

Display the packets using maximum detail of a file called capture.cap:
tcpdump -vvv -r capture.cap

Display IP addresses and port numbers instead of domain and service names when capturing packets:
tcpdump -n

Capture any packets where the destination host is 192.168.1.1. Display IP addresses and port numbers:
tcpdump -n dst host 192.168.1.1

Capture any packets where the source host is 192.168.1.1. Display IP addresses and port numbers:
tcpdump -n src host 192.168.1.1

Capture any packets where the source or destination host is 192.168.1.1. Display IP addresses and port numbers:
tcpdump -n host 192.168.1.1

Capture any packets where the destination network is 192.168.1.0/24. Display IP addresses and port numbers:
tcpdump -n dst net 192.168.1.0/24

Capture any packets where the source network is 192.168.1.0/24. Display IP addresses and port numbers:
tcpdump -n src net 192.168.1.0/24

Capture any packets where the source or destination network is 192.168.1.0/24. Display IP addresses and port numbers:
tcpdump -n net 192.168.1.0/24

Capture any packets where the destination port is 23. Display IP addresses and port numbers:
tcpdump -n dst port 23

Capture any packets where the destination port is is between 1 and 1023 inclusive. Display IP addresses and port numbers:
tcpdump -n dst portrange 1-1023

Capture only TCP packets where the destination port is is between 1 and 1023 inclusive. Display IP addresses and port numbers:
tcpdump -n tcp dst portrange 1-1023

Capture only UDP packets where the destination port is is between 1 and 1023 inclusive. Display IP addresses and port numbers:
tcpdump -n udp dst portrange 1-1023

Capture any packets with destination IP 192.168.1.1 and destination port 23. Display IP addresses and port numbers:
tcpdump -n "dst host 192.168.1.1 and dst port 23"

Capture any packets with destination IP 192.168.1.1 and destination port 80 or 443. Display IP addresses and port numbers:
tcpdump -n "dst host 192.168.1.1 and (dst port 80 or dst port 443)"

Capture any ICMP packets:
tcpdump -v icmp

Capture any ARP packets:
tcpdump -v arp

Capture either ICMP or ARP packets:
tcpdump -v "icmp or arp"

Capture any packets that are broadcast or multicast:
tcpdump -n "broadcast or multicast"

Capture 500 bytes of data for each packet rather than the default of 68 bytes:
tcpdump -s 500

Capture all bytes of data within the packet:
tcpdump -s 0

[Linux] Check memory status

To check memory status

$> cat /proc/meminfo

Example of output:

MemTotal:      1928752 kB
MemFree:         13996 kB
Buffers:        253188 kB
Cached:        1044864 kB
SwapCached:        408 kB
Active:         937040 kB
Inactive:       672096 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:      1928752 kB
LowFree:         13996 kB
SwapTotal:     2096472 kB
SwapFree:      2063280 kB
Dirty:              20 kB
Writeback:           0 kB
AnonPages:      310696 kB
Mapped:         141988 kB
Slab:           270332 kB
PageTables:      17188 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   3060848 kB
Committed_AS:  1077764 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    263920 kB
VmallocChunk: 34359473911 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB

[OracleDB] Start Listener by lsnrctl start

The command is to launch the listener

at /opt/oracle/product/<version>/db_1/network/admin/listener.ora

in order to accept foreign connection s to the oracleDB.

It requires the setting of ORACLE_HOME to the bash_profile.

$> lsnrctl start

[OracleDB] Drop and add constraints

To ensure integrity drop option is placed before add option

ALTER TABLE <TABLENAME> DROP CONSTRAINT pk_primkey;

ALTER TABLE <TABLENAME> ADD CONSTRAINT pk_primkey primary key (id);

[OracleDB] some useful administration views

The following views are very useful for administration, you’ll need to login as system DBA using “sqlplus / as sysdba” command.

The following views will be added from time to time.

Method to use: just issue ‘SELECT * FROM <VIEW>;’ to view

v$tablespace       //shows all the tablespaces (inc undo) in the DB
v$datafile         //shows the path, date created, size of datafile

[OracleDB] Check constraints of table

There are several types of constraints in oracle e.g. PK (primary key), FK (foreign key),  UK (unique keys), CK (check if values comply), etc. Once a constraint is registered in the database, they will be consolidated in a table called user_constraints.

The following SQL is to check the constraint by table_name or constraint_name

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME ='<TABLENAME>';
SQL> SELECT * FROM USER_CONSTRAINTS WHERE CONSTRAINT_NAME LIKE '%ID%';

The schema of the table user_constraints:
 Name                   Null?    Type
 --------------------- -------- ------------------------------------
 OWNER                  NOT NULL VARCHAR2(30)
 CONSTRAINT_NAME        NOT NULL VARCHAR2(30)
 CONSTRAINT_TYPE                 VARCHAR2(1)
 TABLE_NAME             NOT NULL VARCHAR2(30)
 SEARCH_CONDITION                LONG
 R_OWNER                         VARCHAR2(30)
 R_CONSTRAINT_NAME               VARCHAR2(30)
 DELETE_RULE                     VARCHAR2(9)
 STATUS                          VARCHAR2(8)
 DEFERRABLE                      VARCHAR2(14)
 DEFERRED                        VARCHAR2(9)
 VALIDATED                       VARCHAR2(13)
 GENERATED                       VARCHAR2(14)
 BAD                             VARCHAR2(3)
 RELY                            VARCHAR2(4)
 LAST_CHANGE                     DATE
 INDEX_OWNER                     VARCHAR2(30)
 INDEX_NAME                      VARCHAR2(30)
 INVALID                         VARCHAR2(7)
 VIEW_RELATED                    VARCHAR2(14)

[OracleDB] Import and Export Database tables

The following script is executed in bash console to import or export database tables, the *.dmp file exported contains both the schema and the data.

NOTE: If the schema exists when importing, the import may stop without the optionignore=y‘ –> it stands for ignore the already exist schema.

export table(s):
$> exp username/password@database tables=table1,table2 file=data.dmp

import dmp file:
$> imp username/password@database file=data.dmp ignore=y

[OracleDB] Modify column

Different from normal sql, the modification of oracle DB is using the MODIFY syntax rather than the ALTER COLUMN

SQL> ALTER TABLE <TABLENAME> MODIFY(<COLUMNNAME> NULL);
SQL> ALTER TABLE <TABLENAME> MODIFY(<COLUMNNAME> NOT NULL);
SQL> ALTER TABLE <TABLENAME> MODIFY(<COLUMNNAME> VARCAHR2(200));

[Mysql] install DB with directory configuration

The following code is the install script for mysql with specification of directories.

p.s. mysql version 5.6

$> bin/mysql_install_db --user=mysql \
         --basedir=/opt/mysql/mysql \
         --datadir=/opt/mysql/mysql/data

[OracleDB] Create database by using init.ora

The following configuration is the initialize script for database,

the creation will not be successful without the init<sid>.ora and the orapw<sid>.ora

in the path $ORACLE_HOME/dbs, sid is the $ORACLE_SID

/opt/oracle/product/10.2.0/db_1/dbs/init<sid>.ora

background_dump_dest='/u01/oracle/admin/<dbname>/bdump'
user_dump_dest='/u01/oracle/admin/<dbname>/udump'
core_dump_dest='/u01/oracle/admin/<dbname>/cdump'

compatible='10.1.0.2.0'  //old version compatible

control_files=('/u01/oracle/oradata/<dbname>/control01.ctl', //line break
'/u02/oracle/oradata/<dbname>/control02.ctl')

db_block_size=8192
db_cache_size=25165824
db_file_multiblock_read_count=16
db_name='<dbname>'
#db_recovery_file_dest='/u01/app/oracle/oradata/<dbname>/flash_recovery_area'
#db_recovery_file_dest_size=2147483648
db_unique_name='<dbname>'

java_pool_size=50331648
job_queue_processes=10
large_pool_size=8388608

log_archive_dest_1='LOCATION=/u01/oracle/oradata/<dbname>/arch'
log_archive_dest_2='LOCATION=/u02/oracle/oradata/<dbname>/arch'
log_archive_dest_state_1='ENABLE'
log_archive_dest_state_2='ENABLE'
log_archive_format='%t_%s_%r.arc'
log_archive_max_processes=10

open_cursors=300
pga_aggregate_target=25165824
processes=250
remote_login_passwordfile='EXCLUSIVE'
sort_area_size=65536
standby_file_management='AUTO'
undo_management='AUTO'
undo_tablespace='UNDOTBS1'
# audit_trail = true            # if you want auditing
# timed_statistics = true       # if you want timed statistics
max_dump_file_size = 10240      # limit trace file size to 10 Meg each

shared_pool_size=99614720
#shared_pool_size = 3500000                                            # SMALL
# shared_pool_size = 5000000                                          # MEDIUM
# shared_pool_size = 9000000                                          # LARGE

#processes = 50                                                        # SMALL
#processes = 100                                                     # MEDIUM
processes = 250                                                     # LARGE

parallel_max_servers = 8                                              # SMALL
# parallel_max_servers = 4 x (number of CPUs)                         # MEDIUM
# parallel_max_servers = 4 x (number of CPUs)                         # LARGE

log_buffer = 524288                                                    # SMALL
# log_buffer = 32768                                                  # MEDIUM
# log_buffer = 163840                                                 # LARGE

nls_territory = 'HONG KONG'