Cheat Sheet

Tuesday, January 10, 2017

psql: INSERT, if already exists, SELECT (two cidr version)

0 comments
It is actually quite weird. I already set that the cidr is not null, but still, i can happily insert null value inside that.

Hence, it come the problem. I can't select if one of the column's value is null. COALESCE didn't help (on its own). Now we have to modify our script a little bit.

One of the solution is setting default value for cidr-typed column. But what value? 0.0.0.0/0 obviously didn't help. Because null is null. 0.0.0.0/0 is everyone.

Now here what i do.

Let say, we have



Hence, the (unoptimized, but working) query is like this:

*change 192.168.43.199 to Address Client and 0.0.0.0 to X Forwarded For Address, both nullable



tadah

Monday, January 9, 2017

psql: Insert, if already exists, select

0 comments
WITH new_browser AS (
INSERT INTO audit_log.browsers ( agent_string ) 
 SELECT 'test8' WHERE NOT EXISTS (SELECT * FROM audit_log.browsers WHERE agent_string = 'test8')
    RETURNING browser_id
)
SELECT * FROM new_browser
UNION
SELECT browser_id FROM audit_log.browsers WHERE agent_string = 'test8'
The only problem is that i have to write the insert item/select condition 3 times, for one particular column...

Friday, January 6, 2017

Script to rename hostname and append to /etc/hosts

0 comments
#!/bin/bash

# save this as anyfile.sh at webserver

# call curl -s http://somewhere.over.the.rainbow/anyfile.sh | sudo bash -s {hostname}

if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

if [ $# -eq 0 ]
  then  echo "Please supply new hostname for this machine."
fi

echo "Hostname entered: " $1

hostnamectl set-hostname $1

echo 127.0.0.1 $1 >> /etc/hosts

cat >> /etc/hosts << EOF
# list of hosts
EOF

systemctl restart systemd-hostnamed

Thursday, January 5, 2017

CentOS/Fedora/Ubuntu (FirewallD/ufw) allow specific port from specific IP

0 comments
I wanted to allow only certain port incoming from certain IP
Rich rules for FirewallD (CentOS 7+ or Fedora)
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=10.X.X.X/32 port port=6556 protocol=tcp accept'
or for full,
sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=10.X.X.X/32 port port=6556 protocol=tcp accept' && sudo firewall-cmd --reload
For Ubuntu with UFW;
sudo ufw allow from 10.X.X.X/32 to any port 6556