Skip to content

Instantly share code, notes, and snippets.

@huyanhvn
huyanhvn / Shell string tricks
Last active January 26, 2017 15:18
shell string tricks
### REPLACE \n WITH LITERAL '\n'
sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g'
### SEARCH AND PRINT REGEX MATCH
echo ... | sed -n -e "s/^.*arn:aws:iam::\(.*\):user.*/\1/p"
### PRINT SEQUENCE
seq -f "10.235.5.%g" 117 126
### SEARCH AND REPLACE IN FILES
@huyanhvn
huyanhvn / linux-tricks
Last active September 3, 2018 15:37
Linux tricks
### Kill process and all its children
kill -- -$(ps -o pgid= $PID | grep -o '[0-9]*') (signal TERM)
kill -9 -$(ps -o pgid= $PID | grep -o '[0-9]*') (signal KILL)
### FIND TOP MEMORY USERS
ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 10
### SSH menu
~C
@huyanhvn
huyanhvn / sublime-key-bindings
Last active May 11, 2016 13:42
sublime user key bindings
[
{ "keys": ["ctrl+x"], "command": "cut" },
{ "keys": ["ctrl+c"], "command": "copy" },
{ "keys": ["ctrl+v"], "command": "paste" },
{ "keys": ["ctrl+shift+v"], "command": "paste_and_indent" },
{ "keys": ["ctrl+s"], "command": "save" },
{ "keys": ["ctrl+z"], "command": "undo" },
{ "keys": ["ctrl+shift+z"], "command": "redo" },
{ "keys": ["ctrl+y"], "command": "redo_or_repeat" },
{ "keys": ["ctrl+u"], "command": "soft_undo" },
@huyanhvn
huyanhvn / chef-stuff
Last active August 29, 2015 14:21
Chef stuff
### berks ignore ssl
$ cat ~/.berkshelf/config.json
{
"ssl": {
"verify": false
}
}
### berks Faraday SSL error
export SSL_CERT_FILE=/etc/openssl/cert.pem
@huyanhvn
huyanhvn / tcpdump
Last active August 29, 2015 14:23
tcpdump
# tcpdump -i eth0 -A -s65535 port 8080
intecepts all packets goving over port 8080 on eth0 network interface.
-s means size of each packet. so it dumps first 65k of the packet. default is much short and will truncate long http responses.
-A means show packets as ascii on screen
-w filename
for non plain text protocol (e.g. LDAP), write to file and use wireshark.
https://danielmiessler.com/study/tcpdump/
@huyanhvn
huyanhvn / apache-stuff
Created August 19, 2015 16:24
apache-stuff
### USING DBM MAP
RewriteEngine On
RewriteMap some_map dbm=sdbm:/var/www/some_map.map
RewriteCond %{QUERY_STRING} some[iI]d=(\d+)
RewriteCond ${some_map:%1} !=""
RewriteRule ^/some/thing/view\.asp$ /some/thing/else/${some_map:%1}? [P]
@huyanhvn
huyanhvn / chef-vault
Last active May 2, 2016 16:09
chef-vault
### knife.rb:
knife[:vault_mode] = "client"
data_bag_path "/Users/hnguyen/data_bags"
### Creating a Vault
Vault creation is very similar to data bag creation. Specify a Vault name, and Item name inside of that vault as well as the default JSON data to go into the vault. You can optionally specify a node search, and/or a list of Chef Server users who should have access to the vault.
knife vault create [vault] [item] '{}'
knife vault create [vault] [item] '{}' -S 'search' -A 'user1,user2'
@huyanhvn
huyanhvn / aws-s3
Last active March 11, 2016 14:49
aws-s3
### SIZE OF S3 BUCKET
aws s3api list-objects --bucket BUCKETNAME --output json --query "[sum(Contents[].Size), length(Contents[])]”
### BUCKET POLICY FOR ENCRYPTED BUCKET
{
"Version": "2012-10-17",
"Id": "PutObjPolicy",
"Statement": [
{
"Sid": "DenyUnEncryptedObjectUploads",
@huyanhvn
huyanhvn / jmeter-example.jmx
Created July 21, 2016 17:49
jmeter-example.jmx
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.9" jmeter="3.0 r1743807">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="cic-proxy-latency-test" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
@huyanhvn
huyanhvn / enable-luks-howto
Created July 25, 2016 18:12
Enable LUKS disk encryption with a key file
# Create strong LUKS key
openssl genrsa -out /root/luks.key 4096
chmod 400 /root/luks.key
# Fill random data to the device
shred -v --iterations=1 /dev/xvdb
# Format device
echo "YES" | cryptsetup luksFormat /dev/xvdb --key-file /root/luks.key