Category Archives: Cheat sheet

Quick tutorials

Passwordless SSH log in for MAC OS X

On the machine from you want to ssh to ypur dream server:

  • Create id_rsa key.

when prompted use the suggested filename, and (I strongly recommend to select a passphrase)

ssh-keygen -t rsa

– Copy public key from ~/.ssh/ to your server using copy-ssh-id

ssh-copy-id -i ~/.ssh/

Now you should be able to ssh without using password.


If you add a passphrase

You will need one more extra step to store the passphrase in the keychain so you won’t be asked to type it over and over again.
– Create a file in ~/.ssh/config
– Add these contents

Host *
AddKeysToAgent yes
UseKeychain yes


Now once you enter the passphrase, OS X won`t ask you again.

Debugging SSH issues on OS X

Most common problems could be:

Directory permissions

directory permission permission code
/Users/[usename] 755 rwxr-xr-x
/Users/[usename]/.ssh 700 rwx——
/Users/[usename]/.ssh/id_rsa 600 rw——-

Modifications in SSH config files /etc/ssh/sshd_config

How to debug 

On the server:

– You could run another SSH process on different port, and monitor console log.
$ sudo /usr/sbin/sshd -d -p 4444

The client:

– Connect to the newly instantiated SSH process on port 4444 (-p 4444) with -v (verbose) option and monitor the log.
ssh -v -p 4444 tmux@ -i ~/.ssh/tmux_ssh

Once you find the issue and fix it, you could restart the SSH server with the following commands:
$ sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
$ sudo launchctl load /System/Library/LaunchDaemons/ssh.plist

Understanding Java Script Map, Filter and Reduce


The array

// The array
var people = [
  { name: 'john', age: 24, weight: 84 },
  { name: 'tim', age: 34, weight: 76 },
  { name: 'Jack', age: 26, weight: 65 },



// map - use it if I want to do the same operation for each element in the array
// and return the same amount of items in the array.
// current item of the array,
// curent index,
// the entire array
// );
var result =, index, persons) => {
  return `${}: ${person.age}`;
console.log('map', result);



// filter - return only items that match certain criteria.
// array.filter(
// current item of the array,
// curent index,
// the entire array
// )

var result = people.filter((person, index, persons) => {
  return person.age < 30;
console.log('filter', result);



// reduce - return something completely new after iteration through each element
// in the array
// array.reduce(
// current value of the end value,
// current item,
// curent index,
// the entire array
// )
var initialValue = 10;
var combined_weight = people.reduce((weight, person, index, persons) => {
  return weight += person.weight;
}, initialValue);
console.log('reduce', combined_weight);

Another example of reduce, that will convert the array of objects, to object of objects, where is going to be the key

var reducer = (accumolator, person) => {
  accumolator[] = person;
  return accumolator;

var initialValue = {};

// reduce takes reducer function as first parameter, and initial value
var result = people.reduce(reducer, initialValue);

console.log('reduce', result);


Multiple asynchronous http calls using curl

If you ever needed to fetch data from multiple sources on the backend, probably you already explored the benefits of using curl multi fetch.
In the examples below, I’m using curl to fetch data from two urls, which simply have

echo “Content 1 …”;


 intentionally to delay the content generation so we could see the difference between regular http call and asynchronous curl multi fetch.



$t1 = microtime(true);

function fetchContent($Url) {
// is cURL installed yet?
if (!function_exists(‘curl_init’)){
die(‘Sorry cURL is not installed!’);

// OK cool – then let’s create a new cURL resource handle
$ch = curl_init();

// Now set some options (most are optional)

// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);

// Set a referer
curl_setopt($ch, CURLOPT_REFERER, “”);

// User agent
curl_setopt($ch, CURLOPT_USERAGENT, “MozillaXYZ/1.0”);

// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);

// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

//Proxy if needed
//curl_setopt($ch, CURLOPT_PROXY, “”);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

// Download the given URL, and return output
$output = curl_exec($ch);

// Close the cURL resource, and free system resources
return $output;

echo “<textarea style=’width:100%;height:40%’>”;
echo fetchContent(‘’);
echo “</textarea>”;

echo ‘<hr>’;

echo “<textarea style=’width:100%;height:40%’>”;
echo fetchContent(‘’);
echo “</textarea>”;

echo ‘fetched for: ‘ . (microtime(true) – $t1) . “\n”;



// is cURL installed yet?
if (!function_exists(‘curl_init’)){
die(‘Sorry cURL is not installed!’);

$ch = array();
$mh = curl_multi_init();
$total = 100;

$t1 = microtime(true);

$URLs = array( “”,

$i = 0;
foreach($URLs as $url) {
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_URL, $url);
curl_setopt($ch[$i], CURLOPT_HEADER, 0);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);

curl_multi_add_handle($mh, $ch[$i]);
$i ++;

$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
//usleep(100); // Maybe needed to limit CPU load (See P.S.)
} while ($active);

$content = array();

$i = 0;
foreach ($ch AS $i => $c) {
$content[$i] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);


echo “<textarea style=’width:100%;height:40%’>”;
echo $content[0];
echo “</textarea>”;

echo ‘<hr>’;

echo “<textarea style=’width:100%;height:40%’>”;
echo $content[1];
echo “</textarea>”;

echo ‘fetched for: ‘ . (microtime(true) – $t1) . “\n”;


So this way we are reducing the amount of time to fetch data from multiple URLs to the amount needed to complete the longest transaction.