Category Archives: Mac OS

Using SSH to clone Git repository with multiple private keys

I prefer to use the multiple private keys and avoid using the same private key with multiple services. Recently I decided to switch from HTTPS based Git clone of my bitbucket repositories to SSH based Git clone. So created a new private key added them into bitbucket.org and then I expected that my git clone would work. But it didn’t.

git clone git@bitbucket.org:mybitbucketid/mygitrepository.git
Cloning into 'mygitrepository'...
conq: repository access denied.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I knew I had to point the git client to my private key so I created the following entry in my ~/.ssh/config file.

Host bitbucketrepo
HostName bitbucket.org
IdentityFile ~/.ssh/bitbucket_private_key
User git

Now I felt it would work. I tried again and it still didn’t work.

git clone bitbucketrepo:mybitbucketid/mygitrepository.git
Cloning into 'mygitrepository'...
conq: repository access denied.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Now I was confused as to why it was not working. On further research I found this link. Now I updated the entry in my ~/.ssh/config file to this.

Host bitbucketrepo
HostName bitbucket.org
IdentityFile ~/.ssh/bitbucket_private_key
IdentitiesOnly yes
User git

Now I tried again. This time it worked perfectly.

git clone bitbucketrepo:mybitbucketid/mygitrepository.git
Cloning into 'mygitrepository'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

Well the addition of “IdentitiesOnly yes” line in my config file did the trick. It seems that when we do an SSH connection it’s default behavior is to send the identity file matching the default filename for each protocol. So if you have a file named ~/.ssh/id_rsa then that will get tried before your private key which in my case was ~/.ssh/bitbucket_private_key. So by using the “IdentitiesOnly yes” line I explicitly asked my ssh client to use my identity file and nothing else and it worked like a charm.

Awesome tmux

I have always found moving between tabbed interfaces cumbersome in my Mac terminal. Finally when debugging 7 different log files sitting on 7 different servers I felt enough was enough and started looking for a solution. Enter ‘tmux’.

tmux is a terminal multiplexer which supports multiple windows inside a single terminal session and allows me to create horizontal as well as vertical panes. So I can debug the logs sitting on different servers by tailing them in different pane and then still can have one pane dedicated for executing commands. I found a very good tutorial at this and this location.

Now I don’t want to go back to the old way of having multiple tabs for multiple logs. Agreed tabs in terminal have their own place and usage but for this particular case where I am doing development and debugging multiple servers I don’t have time for a tabbed interface instead ‘tmux’ is the way to go.

Fixing “Write failed: Broken pipe” on Yosemite

After latest update to my macbook pro I noticed that my SSH connections started dropping if I kept them idle for few minutes. Each time the session terminated with the text “Write failed: Broken pipe”. I have observed that the connection used to hang for a long while before this error message was shown.

This was not the behavior before so I suspected that recent updates might have changed some configuration and hence I started looking around. I found an article which explained how to configure my Macbook.

Based on the inputs provided in the article I edited the file /etc/ssh_config using the following command:
sudo vi /etc/ssh_config

And changed/uncommented the following lines:
Host *
ServerAliveInterval 60
TCPKeepAlive yes

I learnt another point in this article that during the SSH session if I press “~” followed by “.” then the connection terminates immediately. If it doesn’t then pressing enter before doing this helps.

Searching for file containing keyword in Linux

Grep suits the bill for all my requirement for efficient search of files containing text in Linux and Mac. The following commands detail the use cases of grep.

Search for pattern
grep -rnw 'folder' -e 'text'

-r stands for recursive.
-n is the line number.
-w stands for whole word match.

Example: grep -rnw . -e 'import'
This searches for the the text ‘import’ in the current directory recursively.

Found this tip at stackoverflow.com.

Debugging HTTP Traffic in Mac

I use Fiddler extensively in Windows for debugging any HTTP traffic in my web applications. However on Mac Fiddler is not available. On some search I have found out the following tools which can do the job:

Chrome
In Chrome just type “chrome://net-internals/#http2” in the address bar and you will be able to see all the HTTP traffic that is going on in your system. Not sure when it got added to Chrome but it is a very simple yet powerful utility.

Charles Proxy
This is Java based commercial utility which can be used for almost Fiddler like functionality. This works in Windows, Linux and Mac so it is good deal I think for any developer.

Update: On using Charles Proxy I found it to be dead simple to use and it fulfilled all the requirements I had on my Mac. There is 30 minute lockout feature for unlicensed version which seems fair. It is completely worth the 50$ price tag.

Using grep, sed to filter data in Linux, Mac OS

I recently received a blob of data which needed some alteration to properly suit the CSV format. I found that the data file ‘xyz.dat’ contained close to 600,000 rows so writing a VBA script in Excel to format the data was not an option. I opted to do it using some plain commands in Unix. I had to attain the following purpose:

1. Remove all rows with blank lines.
2. Remove all rows that didn’t start with the number ‘1’
3. Trim the file to 1000 rows to sample the data.

I executed the following commands to attain the above objectives.

To remove all blank lines I used this command. I am basically asking grep to treat the text as ASCII text and using a regular expression to identify blank rows:
cat xyz.dat | grep -a -v -e '^[[:space:]]*$' > xyz_no_space.dat

To remove all rows that don’t start with ‘1’ I used this command. This command is similar to above command except for the regular expression to identify rows that start with ‘1’.
cat xyz_no_space.dat | grep -a '^1' > xyz_no_space_start_with_1.dat

At this point we have the data we want but for sampling purpose I need to copy first 1000 rows into another file. This command uses ‘sed’ to do it.
sed -n -e '1,1000p' xyz_no_space_start_with_1.dat > xyz_trimmed_1_1000.dat

This command basically renames the *.dat file into *.csv for convenience.
mv xyz_trimmed_1_1000.dat xyz_trimmed_1_1000.csv

A summary of all commands I executed is listed below:
cat xyz.dat | grep -a -v -e '^[[:space:]]*$' > xyz_no_space.dat
cat xyz_no_space.dat | grep -a '^1' > xyz_no_space_start_with_1.dat
sed -n -e '1,1000p' xyz_no_space_start_with_1.dat > xyz_trimmed_1_1000.dat
mv xyz_trimmed_1_1000.dat xyz_trimmed_1_1000.csv

Synergy for Multi-Computer Control

I have to work with Windows, Linux and Macbook environment and with time I have no issues working with all of these environments simultaneously. What used to irk me the most was the need to physically use multiple keyboards and mouse and control my Windows laptop and Macbook. Doing VNC / Remote Desktop into another computer didn’t cut it. So I found out about Synergy project long time ago. This is a free and open source keyboard/mouse sharing solution.

Basically the computer whose keyboard/mouse you want to use becomes the Synergy server and all other laptops / computer would become Synergy client. It is very easy to setup and once it is done I can use the keyboard / mouse of of my Macbook to control my windows laptop as well as Linux laptop simultaneously.

See hidden files in Mac OS Finder

I like to see all the files in the project folder where I am working. So it is inconvenient not being able to see “.gitignore” and “.git” inside the folder where I have git repository. On a brief lookup of the internet I found this article which proposes two approach of making hidden files visible in finder.

Approach #1: Using Terminal

To show hidden files
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

To turn off showing of hidden files
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

Approach #2: Using an App
Funter is a small app which allows the user to toggle the hidden file visibility on and off at the click of a button which is quite convenient.

What I used?
I preferred the terminal approach as I am fairly comfortable with it and I didn’t want to add a new application into my mac’s start-up and menu bar.