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/id_rsa.pub to your server using copy-ssh-id

ssh-copy-id -i ~/.ssh/id_rsa.pub myname@myserver.com

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.
[cpp]
$ sudo /usr/sbin/sshd -d -p 4444
[/cpp]

The client:

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

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

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

// 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.
// array.map(
// current item of the array,
// curent index,
// the entire array
// );
var result = people.map((person, index, persons) => {
  return `${person.name}: ${person.age}`;
});
console.log('map', result);

 

FILTER

// 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

// 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 person.name is going to be the key

var reducer = (accumolator, person) => {
  accumolator[person.name] = 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

[php]
<?php
sleep(3);
echo “Content 1 …”;

[/php]

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

http://toni-develops.com/sandbox/examples/php/curl-multi-fetch/fetched-content/fetch1.php
http://toni-develops.com/sandbox/examples/php/curl-multi-fetch/fetched-content/fetch2.php

[php]

<?php

$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, “http://www.example.org/yay.htm”);

// 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, “64.210.197.20:80”);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
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
curl_close($ch);
return $output;
}

echo “<textarea style=’width:100%;height:40%’>”;
echo fetchContent(‘http://toni-develops.com/sandbox/examples/php/curl-multi-fetch/fetched-content/fetch1.php’);
echo “</textarea>”;

echo ‘<hr>’;

echo “<textarea style=’width:100%;height:40%’>”;
echo fetchContent(‘http://toni-develops.com/sandbox/examples/php/curl-multi-fetch/fetched-content/fetch2.php’);
echo “</textarea>”;

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

 

[php]
<?php

// 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( “http://toni-develops.com/sandbox/examples/php/curl-multi-fetch/fetched-content/fetch2.php”,
“http://toni-develops.com/sandbox/examples/php/curl-multi-fetch/fetched-content/fetch2.php”);

$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);
}

curl_multi_close($mh);

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”;
[/php]

 

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.