Rocu.de

Love, caffeine and omlette

Tag: command line (page 1 of 2)

The awesomeness tool-chain

I have an awesomeness tool-chain — command line tools, that increase my productivity dramatically. In this blog post I want to introduce you to these powerful tools.

Cowsay

$>  echo "I love strawberries" | cowsay
  _____________________
 < I love strawberries >
  ---------------------
             ^__^
             (oo)_______
              (__)       )/
                  ||----w |
                  ||     ||

Ponysay

If you like ponies and colors more - I understand. Depends on the use case really. I found ponies way more effective if you want to communicate something that could trigger bad feelings.

sl - The ls for pros

I often mistype ls. Gone are the days were this was boring. I just lean back and enjoy the show. Steam Locomotives are a big part of being awesome.

Fortune

If you are as new to programming as me you need help, when someone asks you for you're opinion.

Fortune produces profound answers to all kind of questions you did not even ask.

$ fortune
I am the mother of all things, and all things should wear a sweater.

$ fortune
Any sufficiently advanced technology is indistinguishable from a rigged demo.
    -- Andy Finkel, computer guy

Conclusion

Being awesome requires a special tool-chain. Having ponies and quotes at your disposal is essential. I think you should give this a try.

Fighting bad habits with technology

I keep a list of stuff to improve. Whenever I catch something stupid it lands in there.

I try to get rid of these bad habits.

Often I find myself unable to solve the problem with willpower alone. Then I search for a solution that assists me. Maybe a little "barrier" or some kind of reminder. Let me show you, what I mean with this..

Looking at the keys all the time

When I started to program 5 years ago I looked onto my keyboard all the time. Sure - typing faster does not make you a better programmer - but typing with 2 fingers is not ergonomic.

I found a nice solution for my problem.

I bought a keyboard without letters. I was so bad to hit the proper keys, that nudged me towards learning it right. I have to admit though that I still have problems with some special keys 😉

Not moving enough during work

Like most people I had the problem that I did not take enough breaks. This was hard to change.

I started to work in Pomodoros (25 minutes of work and 5 minutes of break). Unfortunately I still did not leave my working space often enough.

So I took another iteration and added added a little AppleScript to my Pomodoro App..

After each Pomodoro my Pomodoro timer executes a little script that locks my Mac 😉

Surfing the web all the time

Unfortunately I am addicted to the internet. I am pretty OK with it most of the time — but not, when I want to be productive.

That's why i wrote a little script that blocks my internet access.

#!/bin/bash
echo "Enough of this filthy internet" | cowsay -s
sudo route -n delete default &> /dev/null

If I need to be more specific with the site I need to block, I use a program called SelfControl.

This works great! Of course as a computer guy I could circumvent this — but I do not want to.

Force pushing to master

It happened frequently that I forced pushed to master. This was really annoying for me.


Image Source: Flickr / Pascal (CC BY 2.0)

My colleague Nikolay proposed to fix it with a script. That's just what I did.

I have not force pushed to master since. This script is now also in use at some of my colleagues.

Checking working email at home

I checked working email at home. I love my work to much to let my fingers from it - but I realize that I need time of it in order to rest.


Image Source: Flickr / Tama Leaver (CC BY 2.0)

There's a simple remedy for not reading work related emails at home: Delete the freaking email account on your computers at home. It's baffling how often you will look into your empty email-client thereafter..

Checking Twitter really often

There's so much chatter at Twitter. I checked Twitter hourly. Here's what I did:

  • Remove people that write to much
  • Add a URL block-filter
  • Delete Twitter from my iPhone

    This works really well for me.

Not using arrow keys in VIM

Another stupid habit of mine was, that I used the arrow keys in order to move in VIM.

This was pretty simple to fix: I disabled them!

 not use the arrow keys any more
 nnoremap <up>    <nop>
 nnoremap <down>  <nop>
 nnoremap <left>  <nop>
 nnoremap <right> <nop>
 inoremap <up>    <nop>
 inoremap <down>  <nop>
 inoremap <left>  <nop>
 inoremap <right> <nop>

It took only a week until I was not trying to hit them any more 😉

Being to digital

Another problem of me is, that I use all my electronic devices to much. Here is what I build to prevent this: Productivity Protector 2000.

It works.

Conclusion

What is my point to show you this little collection of personal hacks?

Do not be a victim of you're bad habits.

As Nerd we have superpowers: We analyze problems. We solve them. All day long. Start applying these skills to the rest of your life!

Save a file as root in vim

I often edit files without having the right permissions in VIM.

VIM can not safe a file

I changed the file and when VIM told me that I did not have the proper permissions to save it, I quit VIM and entered sudo !! to try it again.

This was of course stupid of me. Currently I try to be more mindful about this kind of problems and to fix them.

Here's the lines that I just added to my .vimrc

" Use :w!! to save as root!
cmap w!! w !sudo tee % >/dev/null

When I enter :w!! now VIM saves the file as root.

I hope I will not encounter this problem again.

How to not use the arrow keys any more in vim

I try to wrap my head around the VIM editor since a few months. Unfortunately I do not have enough discipline to use h-j-k-l as my movement keys. But there is a solution that will teach me..

I found it deep inside the .vimrc of my colleague Fabian.

nnoremap <up>    <nop>  
nnoremap <down>  <nop>  
nnoremap <left>  <nop>  
nnoremap <right> <nop>  
inoremap <up>    <nop>  
inoremap <down>  <nop>  
inoremap <left>  <nop>  
inoremap <right> <nop>  

This turns off the arrow keys. A really obvious way of doing it 😉

I will add this to my .vimrc right away!

Wish me luck!

Manage your Mac Apps with homebrew-cask

A while ago I became interested in automating the setup of my Macs. I started my journey with chef-solo, however this wasn't really practical for me, so I switched to homesick to manage my dotfiles.

Unfortunately that left me without a solution to install my apps automatically.

Not any more. homebrew-cask to the rescue!

Take a walk

Image Source: Flickr/megoizzy (CC BY-SA 2.0)

What is homebrew-cask

homebrew-cask is a Homebrew extension that allows you to install your apps like this:

brew cask install evernote

How to install homebrew-cask

If you already use homebrew, installing homebrew-cask is really simple:

brew tap phinze/homebrew-cask
brew install brew-cask

If you have Alfred installed you should also configure it, so that it recognizes apps managed by homebrew-cask.

brew cask alfred link

Using homebrew-cask

Using homebrew-cask is simple. You install apps using the brew cask install command. Evernote for example, you would install like this:

brew cask install evernote

Create a little script to install your Apps

I store all my apps in a file called ~/.brew. In order to install the apps I just have to run it.

This is how you create your own:

touch ~/.brew
chmod +x ~/.brew

Now fill it with the Apps you want to install across all your Mac's.

For me this looks like:

#!/bin/bash
brew cask install google-chrome  
…
brew cask install rubymine  
brew cask install charles  
brew cask install mou  
brew cask install bit-torrent-sync  

I have this file on GitHub and symlink it with homesick. When I am on a new Mac I just have to run

~/.brew

That installs all my common apps.

Create own casks

There are over 300 casks available already. You can view a list with

brew cask search

If your favorite app is missing it is really simple to add it.

In the example below I create a Cask for Soulver.

brew cask create soulver

An editor opens with the newly generated cask.

class Soulver < Cask  
   url ''
   homepage ''
   version ''
   sha1 ''
   link ''
end  

You just have to fill in the download URL of the app, the apps name, the version, the sha1 checksum of the download and it's homepage.

class Soulver < Cask  
  url 'http://www.acqualia.com/files/download.php?product=soulver'
  homepage 'http://www.acqualia.com/soulver/'
  version 'latest'
  no_checksum
  link 'Soulver.app'
end  

In Soulver's case the download URL does always refer to the latest version. So in this case I used no_checksum, so that this Cask will not break in the future.

Now audit your cask:

$ brew cask audit soulver
audit for soulver: passed

Now you can install Soulver. And don't forget to open a pull request 😉

Before you do — check the latest contribution guidelines.

Conclusion

I am really happy that homebrew-cask exists now. It closed a gap in my configuration management. I can set up a new Mac in no time. I hope you enjoy it as much as I do.

How to secure your Apache2 webserver with a free SSL certificate

Protecting your website with TLS (SSL) is generaly a good idea, as soon as you have a admin backend or an internal statistik tool installed.

One way to do it is to sign your own SSL certificate. This offers encryption but unfortunately no verification that you really talk to the right server. Additionally all your guests will receive warnings and have to click through warnings.

Thats why a Ceritficate Authority (CA) needs to sign your cerificate. StartSSL does this for free.

In this article I show you how you can get a SSL certificate and how you configure your Apache 2 webserver to use it.

Prerequisites

In order for you to obtain a certificate you need a domain under your control. Which means you need one of the following email adresses and you need to be able to read these mails.

  • postmaster@example.com
  • hostmaster@example.com
  • webmaster@example.com

You can install the certificate on any modern webserver. However in this article I describe how you install it with Apache2 and ModSSL.

Obtaining the certificate

Register with StartSSL

Visit StartSSL.com and click on the Sign-Up button.

Fill in your personal details. Provide true informations here. StartSSL will check your data.

After having supplied your details, you will receive a verification code via email, that you have to fill in.

Now you will get a client certificate, that you can use to verify your identity to StartSSL in the future.

Validate your domain name

Log into the Controll Panel and click onto the Validations Wizard tab.

Choose Domain Name Validation.

Enter the name of your domain.

Now you have to select one of the email adresses I mentioned. StartSSL uses this adress to verify, that you own the domain.

Now you will remove a confirmation code via email, that you have to enter into the next form.

Creating a Certificate Signin Request (CSR)

Now you have established, that the domain belongs to you and StartSSL is ready to sign a Cerificate Signing Request for you. So let's create a CSR!

Log into your webserver and do the following:

$ openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr

Enter the name of your domain, when you are asked for the common name. Here is a how the dialoge looks like:

$ openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr

Generating a 2048 bit RSA private key  
................+++
...................+++
writing new private key to 'example.com.key'  
-----
You are about to be asked to enter information that will be incorporated  
into your certificate request.  
What you are about to enter is what is called a Distinguished Name or a DN.  
There are quite a few fields but you can leave some blank  
For some fields there will be a default value,  
If you enter '.', the field will be left blank.  
-----
Country Name (2 letter code) [AU]:DE  
State or Province Name (full name) [Some-State]:Bavaria  
Locality Name (eg, city) []:Munich  
Organization Name (eg, company) [Internet Widgits Pty Ltd]:  
Organizational Unit Name (eg, section) []:  
Common Name (eg, YOUR name) []:example.com  
Email Address []:robert@example.com

Please enter the following 'extra' attributes  
to be sent with your certificate request  
A challenge password []:  
An optional company name []:  

Now copy the content of the CSR into your clipboard

cat example.com.csr

Let StartSSL sign your CSR

Visit the "Certificates Wizard" tab and choose Webserver SSL/TLS Certificate and click on continue.

Click on skip and paste the contents of the csr file you just created into the text field.

On the next step add www. as the subdomain (or another subdomain if you do not have www).

Now save your certificate. If your domain is example.com I would save it as example.com.crt

Configure Apache2 to use the certificate

Become root or execute the following stuff with sudo

First we have to activate ModSSL

a2enmod ssl

Create a directory for your certificates

mkdir /etc/apache2/ssl

Move the .crt and the .key into the new folder

mv example.com.* /etc/apache2/ssl

We also need a ca.pem file. This file contains both the intermediate and the root certificate. You can download a finished one.

curl -L http://www.rocu.de/x/5s > ca.pem

Now configure the Virtual Host for SSL

vim /etc/apache2/sites-enabled/example.com.ssl
<VirtualHost *:443>  
DocumentRoot /var/www/vhosts/example.com/  
ServerName example.com  
ServerAlias www.example.com

SSLEngine on  
SSLCertificateFile /etc/apache2/ssl/example.com.crt  
SSLCertificateKeyFile /etc/apache2/ssl/example.com.key  
SSLCertificateChainFile /etc/apache2/ssl/ca.pem  
</VirtualHost>  

Test your Apache config before restarting

apachectl configtest

Now restart your webserver

sudo /etc/init.d/apache2 restart

You're website can be viewed with SSL now.

Final words

The process at StartSSL is anti-intuitive. I hope you were able to follow this tutorial and that your website is SSL secured now.

Depending on your use case, there are more steps that you have to take. For example you should load all assets from HTTPS or your users will get mixed content warnings.

Another good idea is to redirect your visitors to HTTPS and to activate Strict Transport Security.

If you have user other then yourself at the website, you should also follow SSL best pratices. Here is a tool that you can use to find out, what you can improve.

I leave all this as an excercise for the reader. Please leave me a comment if you have any questions or corrections.

Backup a local directory to a FTP server

I had to backup a local directory to a FTP server.

My first impulse was to use Rsync but I was out of luck. Rsync doesn't work over FTP since it is a protocol of it's own.

That's when I stumbled over lftp:

LFTP is a sophisticated ftp/http client, and a file transfer program supporting a number of network protocols. Like BASH, it has job control and uses the readline library for input. It has bookmarks, a built-in mirror command, and can transfer several files in parallel.

In order to use lftp you have to install it using the package manager of your choice.

brew install lftp

Now you create a simple script. We use the mirror command here in order to sync a local directory onto the server.

SERVER="your.ftp.server"
USERNAME="user"
PASSWORD="pass123"

LOCAL_DIR="/source/dir"
REMOTE_DIR="/target/dir"

lftp -c "set ftp:list-options -a;
open ftp://$USERNAME:$PASSWORD@$SERVER;
lcd $LOCAL_DIR;
cd $REMOTE_DIR;
mirror --reverse"

The script uses your credentials to log in to the ftp server. The it starts to mirror your local directory to the directory on the server. The flag "reverse" is appended to the mirror command because per default lftp mirrors the content from the server into your local directory.

The mysterious line

lftp -c "set ftp:list-options -a;

is necessary if you want to transfer hidden files as well and your FTP server does not list them per default.

Conclusion

Often FTP backup space is one of the cheapest ways to back up a server, that's when lftp comes really handy. It is way slower then Rsync - but works fine in most situations.

I am glad I have lftp in my toolchain now.

How to prevent a cron job from running multiple times

Sometimes a cron job might run slower, than you anticipated and many instances of it stack up.

You can increase the time interval during starts of the cron job - this can lead to problems in the future however.

Fortunately there's a easy and clean fix.

Just replace your cronjob with this:

/usr/bin/flock -n /var/lock/<foobar> <long-running-task>

Whenever your cron job is started a lock file will be created and no other instance can start.

Conclusion

This a simple remedy for colliding cron job's. I use it frequently.

Create lorem ipsum text with TextExpander and lorem

Often I need dummy text to fill out a form.

I use a combination of TextExpander and the Lorem Gem that makes this
super simple.

Step 1: Install the "lorem" gem

Install the gem lorem using

sudo gem install lorem

It's simple to use:

# For 1 paragraph dummy text
lorem 1 paragraph

# For 140 chars dummy text
lorem 140 chars

Lorem is a Unix tool - you can combine it with other commands:

lorem 140 chars | pbcopy

Copies 140 chars into your clipboard.

Textexpander + Lorem

I dont want to go into my Terminal, whenever I fill out a
form in Chrome, so I created a TextExpander snippet, that executes lorem
for me and fills in the dummy text.

#!/bin/bash
lorem %filltext:name=paragraphs:default=1%

A nice additional feature is that it asks you how many paragraphs of dummy text you want.

Conclusion

Sometimes simple hacks like this save you a lot of time. I hope you find it as useful as me!

How to prevent yourself from force pushing to master

I am pretty reckless sometimes. I forced pushed to master several days in a row.

What does a real programmer do? - He writes a script that prevents him from doing it ever again, of course..

For the longest time this was a little complicated - you had to
install a hook onto the remote repro. For Git >1.8.2, there's a pre-push hook that was just perfect for my script.

If the hook returns an error the push is not executed.

The script

So this is the git hook i came up with.

#!/usr/bin/env ruby
# Pre-push hook that rejects force pushes to master.
# Requires Git > 1.8.2

class PushRejecter  
  def run
    if pushing_to?(:master) and forced_push?
      reject
    else
      allow
    end
  end

  def pushing_to?(branch)
    `git branch | grep "*" | sed "s/* //"`.match(branch.to_s)
  end

  def forced_push?
    `ps -ocommand= -p #{Process.ppid}`.match(/force|pfush/)
  end

  def reject
    puts "Force push to master rejected."
    exit(1)
  end

  def allow
    exit(0)
  end
end

PushRejecter.new.run  

Ignore the over engineering please..

If the current branch is master and i am force pushing it does return an error and the push is not executed.

How to install it

All my hooks are stored in a folder called ~/.git_template.

It is configured as my template directory for git:

[init]
  templatedir = ~/.git_template

In order to activate the hook you have to go into all your existing repros and execute

git init

This will install the hook for you. As I mentioned you need Git >1.8.2.

Alternatively you could place the hook in the .git/hooks/ directory manually in each of your repros.

Conclusion

I am more confident, that I will not cripple a master branch again. A nice feeling. If you like to have a look at the rest of my setup I recommend a look at
my dotfiles.

Older posts

© 2017 Rocu.de

Theme by Anders NorenUp ↑