Skip to content

Instantly share code, notes, and snippets.

View dineshsprabu's full-sized avatar

Dineshprabu S dineshsprabu

  • Bangalore, India.
View GitHub Profile
@dineshsprabu
dineshsprabu / MySQL_query_adding_auto_increment_field.txt
Created February 8, 2016 12:42
[MySQL] Adding Auto Increment Field to an Existing Table Using SELECT
/* This can be used if the table doesn't have an existing Primary Key */
SELECT @s:=@s+1 as id, <table-name>.* from <table-name>, (SELECT @s:= 0) as s;
@dineshsprabu
dineshsprabu / return_promise_from_function.js
Created February 11, 2016 13:17
[NodeJS] Return a promise from your own function
var mongo = require('mongodb');
var monk = require('monk');
var Promise = require('bluebird');
/* DB connection */
var dbName = ''
var db = monk('localhost:27017/'+dbName);
/* replace <collection-name> with your collection*/
var collection = db.get(<collection-name>);
@dineshsprabu
dineshsprabu / iptable_mongo_allow_localhost_only.sh
Created February 13, 2016 02:27
[IPTable] Make Mongo Accessible only by localhost
iptables -A INPUT -p tcp -s localhost --dport 27017 -j ACCEPT
iptables -A INPUT -p tcp --dport 27017 -j REJECT
@dineshsprabu
dineshsprabu / backup_restore_mongo_db_collection.txt
Created February 13, 2016 12:09
[Mongo] Backup and Restore Mongo DB and Collection
/* dumping a particular collection */
mongodump --host 127.0.0.1 --port 27017 --out <backup-path> --db '<db-name>' --collection '<collection-name>'
/* dumping the entire DB */
mongodump --host 127.0.0.1 --port 27017 --out <backup-path> --db <db-name>
/* restoring backup/dump */
mongorestore --host 127.0.0.1 --port 27017 <backup-path>
@dineshsprabu
dineshsprabu / phantomjs_selenium_webdriver_server.js
Created February 16, 2016 04:50
[Selenium][Phantomjs] How to use PhantomJS web driver server with NodeJS?
/*
Bring up the selenium webdriver server using below command
phantomjs --proxy=104.131.57.228:8090 --proxy-type=http --webdriver=8543
The value in --webdriver is the port binded on wd server
*/
var webdriver = require('selenium-webdriver');
var browser = webdriver.Capabilities.phantomjs();
browser.set('phantomjs.page.settings.userAgent', "Mozilla");
@dineshsprabu
dineshsprabu / phantomjs_service_screen.txt
Created February 16, 2016 12:58
[Phantom] Run PhantomJS WebDriver as Service in Ubuntu
/*
Make sure you have scree on your server using screen --version
if not, install using 'sudo apt-get install screen'.
Make sure you have phantomjs installed and accessible on the path
'/usr/bin/phantomjs'.
*/
screen -dmS test bash -c '/usr/bin/phantomjs -w ; exec bash'
@dineshsprabu
dineshsprabu / install_phantomjs_2_0_ubuntu_14_04_x64.txt
Created February 16, 2016 19:43
[PhantomJS] Installing PhantomJS 2.0 on Ubuntu 14.04 x64
cd ~
/* Below is the direct download link for compiled binary */
wget https://github.com/Pyppe/phantomjs2.0-ubuntu14.04x64/raw/master/bin/phantomjs
/* Making PhantomJS accessible */
mv ~/phantomjs /usr/local/share/phantomjs
ln -s /usr/local/share/phantomjs /usr/local/bin/phantomjs
ln -s /usr/local/share/phantomjs /usr/bin/phantomjs
@dineshsprabu
dineshsprabu / phantomjs_restart_automatically_after_crash.txt
Last active February 17, 2016 20:36
[PhantomJS] How to make PhantomJS to restart automatically after a crash?
/*
Make sure you install phantomjs 2.0 which is really stable
Follow the below link for instructions to install it,
https://gist.github.com/dineshsprabu/543de6b41a7597967ea7
*/
>> vi /usr/bin/runphantom.sh
#!/bin/bash
bash -c '/usr/bin/phantomjs -w ; exec bash' &
@dineshsprabu
dineshsprabu / check_port_for_hosts.js
Created February 18, 2016 10:46
[NodeJS] Check port availability for list of IPs
var net = require('net');
var Promise = require('bluebird');
function checkConnection(host, port, timeout) {
return new Promise(function(resolve, reject) {
timeout = timeout || 10000; // default of 10 seconds
var timer = setTimeout(function() {
reject("timeout");
socket.end();
}, timeout);
@dineshsprabu
dineshsprabu / nodejs_events_example.js
Created February 21, 2016 05:16
[NodeJS] Simple example of Events in NodeJS
/* npm install events */
var EventEmitter = require('events').EventEmitter;
/* creating an event emitter object */
var eventObj = new EventEmitter();
/* making it to listen for the event (catchme) with resulting action (console.log) */
eventObj.on('catchme', function(){
console.log(" You Catched Me! ");
});