Sunday, August 14, 2016

SCP (Secure Copy) Examples


SCP (Secure Copy) Examples

Copy the file "foo.txt" from a remote host to the local host

$ scp /local/dir

Copy the file "foo.txt" from the local host to a remote host

$ scp foo.txt

Copy the directory "foo" from the local host to a remote host's directory "bar"

$ scp -r foo

Copy the file "foo.txt" from remote host "" to remote host ""

$ scp

Copying the files "foo.txt" and "bar.txt" from the local host to your home directory on the remote host

$ scp foo.txt bar.txt

Copy the file "foo.txt" from the local host to a remote host using port 2264

$ scp -P 2264 foo.txt

Copy multiple files from the remote host to your current directory on the local host

$ scp\{a,b,c\} .
$ scp\{foo.txt,bar.txt\} .

SSH Cheatsheet


SSH Notes

Configure aliases for hosts

For example, to set up named aliases for dev and qa, create a file at ~/.ssh/config:

Host dev
    User dev_username

Host qa
    User qa_usermname


$ ssh dev

Passwordless auth with ssh-copy-id

Use ssh-copy-id to copy your public key to the remotehost. After you've logged in once, you get passwordless authentication thereafter.

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

This will not work if you are connecting to a Windows remotehost.

Executing Multiple Commands with ssh

the here document syntax

The here document syntax lets you include everything in between << EOF and EOF as a list of commands. Like so:

$ ssh remotehost << EOF
  cd some_folder
  git pull

a script with commands only

Use a script of just the commands.


#!/usr/bin/env bash
cd some_project


$ ssh user@remote 'bash -s' < /path/to/

Transfer files with SFTP

You can use sftp in the same way ssh is used, to get an old-school ftp cli interface.

Compare files with diff

$ ssh user@host cat /path/to/remotefile | diff /path/to/localfile –

Port forwarding

Connects to a remote port, forward it to local.

$ ssh -L 4444:localhost:4000 remotehost

After that, from localhost, requests to http://localhost:4444 will get a response from http://remotehost:4000

Friday, March 25, 2016

Hello World with Node, Express, and React

Node and npm handle the server-side components of the app. We will build a basic Express web framework serving Jade templated files, which is the default for the express-generator.

On the client-side, React will be used for the View layer. React lets you write your client-side code in clean javascript components, rather than mucking around with multiple blocks of html and scripts.

Set up the Express server

  • Install node on your system.
  • Globally install express-generator

    npm install express-generator -g
  • Use express-generator to create a basic app structure.

    $ express demo-react
       create : demo-react
       create : demo-react/package.json
       create : demo-react/app.js
       create : demo-react/public
       create : demo-react/public/images
       create : demo-react/routes
       create : demo-react/routes/index.js
       create : demo-react/routes/users.js
       create : demo-react/public/stylesheets
       create : demo-react/public/stylesheets/style.css
       create : demo-react/views
       create : demo-react/views/index.jade
       create : demo-react/views/layout.jade
       create : demo-react/views/error.jade
       create : demo-react/bin
       create : demo-react/bin/www

install dependencies:

    $ cd demo-react && npm install

run the app:

    $ DEBUG=demo-react:* npm start

Then load http://localhost:3000/ in your browser to access the app.

Set up the client-side React app

For React to work, it has to transpile it's .jsx componets. The easiest way to do this is with In-browser transformation. This method is not designed for production use, since it slows down the pages somewhat. But it's perfectly fine for dev and prototyping work.

We will use Bower to gather the React and Babel scripts needed for transformation.

Install Bower

    $ npm install bower -g

Initialize bower, accepting the defaults.

    $ bower init

We will make one customization here. Let's put the bower_components directory into the public dir, for convenience.

  • Create a .bowerrc file in the root
  • In the .bowerrc, write this one line: {"directory" : "public/bower_components"}
  • Then install your bower components

    bower install babel --save
    bower install react --save

Configure the Jade templates for React

Here's how you set up your Jade templates to include your React components.

The main views/layout.jade needs to be updated to:

  • source the react script,
  • source the babel browser transformation script,
  • and provide a new block called called morejs

See the new bottom three lines in layout.jade

    doctype html
        title= title
        link(rel='stylesheet', href='/stylesheets/style.css')
        block content
        block morejs

In our views/index.jade,

  • Add a #greeter element, which is where React is going to inject our component.
  • Add a reference to that new component in the morejs block. Note the type="text/babel"

    extends layout
    block content
      h1= title
      p Welcome to #{title}
    block morejs
      script(src='/javascripts/helloworld.jsx', type="text/babel")

And the greeter component React JSX code itself, at /public/javascripts/helloworld.jsx is:

        <h1>Hello, world from React.js!</h1>,

That's it. Refresh your browser at http://localhost:3000/ . You should see


Welcome to Express

Hello, world from React.js!

Saturday, May 31, 2014

Longest Common Substring Algorithms

Given strings "ABAB" and "BABA"...

A dynamic programming style algorithm constructs a matrix of the substring lengths.

    |   |   | A | B | A | B |
    |   | O | O | O | O | O |
    | B | O | O | 1 | O | 1 |
    | A | O | 1 | 0 | 2 | 0 |
    | B | O | 0 | 2 | 0 | 3 |
    | A | O | 1 | 0 | 3 | 0 |

In this matrix, the 5th column tests the substring ABA of the ABAB sequence. So cell [5, 5] compares ABA to BAB ... the lcs is BA, length 2.

The other approach is to construct a generalized suffix tree for the strings, then find the deepest internal nodes which have leaf nodes from all the strings in the subtree below it.

A Simple Suffix Tree Implementation:

Dijkstras Algorithm for shortest path:

Or, here's a more complete implementation.

Thursday, May 29, 2014

Function Caching via Memoization

To explain memoization, see these different versions of Fibonacci:

Purely recursive. Highly inefficient. Recalculates every value for every calculation.

Iterative. Much faster.

Here is the memoization technique. A 'cache' dictionary of { (args) : (return_val), } is created. This allows us to quickly locate and reuse any value that has already been calculated.

Here is the same memoization design, implemented as a decorator.

Monday, May 5, 2014

Update Evernote in Wine on Ubuntu

Wine on Ubuntu has problems running the Evernote updates. You get something like: "Evernote was already installed by another user...." The 'find/remove regedit keys' method, described here, and here, did not work for me. I didn't find the reversed the digits in regedit. Well, I only use wine for Evernote. So I used this subtle technique to remove all wine installation. (Don't do this if you have other Wine apps you are keeping.)
sudo apt-get purge wine
rm -rf ~/.wine
sudo apt-get install wine
Then ran the Evernote installer as usual (from files, rt-click on installer).

Monday, April 28, 2014

Fabric is cool.

[Fabric]( is cool. More python, less Bash. For example, I can automate my django db rebuild process, like so: