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!