Programmer's Blog

Programmer's reference

Monthly Archives: July 2014

[iptables] blocking IPs for connection

Sometimes you may encounter some malicious connections to your server
through the checking of /var/log/secure-<DATE>
by using the following iptables rules, you can filter out some malicious IPs

//checking iptable rules
$> iptables -L

//blocking an IP range
$> iptables -I INPUT -m iprange --src-range 192.168.1.10-192.168.1.13 -j DROP

//blocking a subnet
$>  iptables -A INPUT -s 192.168.100.0/24 -j DROP

//blocking particular ip
$> iptables -I INPUT -s {IP-HERE} -j DROP

//delete a rule where the numebr is the row numebr in the iptables -L
$> iptables -D INPUT 4

[C++] Parse text file lines into vector

//use back_inserter in istream to insert each line in text file into vector

std::ifstream myfile("myfile.txt");
    std::vector<std::string> myLines;
    std::copy(std::istream_iterator<std::string>(myfile),
              std::istream_iterator<std::string>(),
              std::back_inserter(myLines));

//to call individual item (e.g. first line) in vector, use at

std::string mystring(myLines.at(0));

[Postgresql] Login Postgresql

psql -h myhost -d mydb -U myuser -W

[OracleDB] set format of output

To enable default formatting of output of queries

$ORACLE_HOME/sqlplus/admin/glogin.def

//add the following lines

set pagesize 0
set linesize 9999
set trimspool on
set tab off
set echo off
set feedback off
set recsep off

[OracleDB] Clean archive logs

$> RMAN target /

RMAN> delete archivelog all;
OR
RMAN> delete archivelog all backed up 1 times to device type disk;
OR
RMAN> delete archivelog until time '2003-03-24:02:38:10' backed up 1 times to device type disk;
OR
RMAN> delete archivelog until time 'sysdate-2' backed up 1 times to device type disk;
OR
RMAN> delete force archivelog until time 'sysdate-2';
OR
RMAN> backup archivelog all not backed up 1 times delete input;

[Linux] chat and execute bash command with netcat nc

install netcat, then

listening side

$> nc -lvp 8000

where 8000 is the port opened

send side

$> nc 127.0.0.1 8000

Then it will establish the chat, for remote execution of bash command, use

$> nc 127.0.0.1 8000 -e /bin/bash

[Linux] CentOS 6 repositories

Create /etc/yum.repos.d/centos-6-x86_64-rpms.repo

[centos-6-x86_64-rpms]
name = CENTOS 6 x86_64 (RPMs)
baseurl = http://mirror.bitco.co.za/centos/6/os/x86_64
enabled = 1
gpgcheck = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[Linux] How to rescue yum after major update

It is a serious problem when you upgrade or downgrade a critical package in the redhat linux,

the libxx.so.x will broke yum, rpm and the c compiler, the following is an output of yum after broken dependancies

[root@localhost ~]$ yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   libz.so.1: cannot open shared object file: No such file or directory

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)]

If you cannot solve this problem yourself, please go to
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

The solution is very simple, just to rebuild the linkage of libraries

#> ldconfig

[Linux] Check chmod for a file

stat --format '%a' root