Category Archives: Mac OS

Viewing files in hex mode

Sometimes I need to look at files in their actual hexadecimal format. I use the combination of vi and hexdump to fulfill this requirement.

To simply view a file’s content in hex format I use this command on my mac.

hexdump -C <file_name>

This typically is enough for the job at hand. However there is also another trick of using hexdump by leveraging vi command. I follow the following steps:

  • Start vi by typing “vi <file_name>” on the command line.
  • Type “:%!hexdump -C”
  • This will replace the contents of the vi screen with the hex view of the current file.
  • Save the file for future use.

Bash Shell – Folder as tree

I like using tree command in Linux, thankfully it has been ported to Mac as well and it is quite easy to get tree representation of files and folders inside a directory on a shell. An article on StackOverFlow talks about it aptly. The original site is here.

In case you are using brew then it is quite easy peasy as shown below:

brew install tree

The following commands generate a tree view:

Generates a tree of all files and folders in the current directory:

tree

Generates a tree containing only folders in the current directory:

tree -d

Generates a tree containing only folders for 3 levels depth.

tree -d -L 2 .

In case you want to use vanilla shell without installing anything. (doesn’t generate a tree though).

find . -maxdepth 3 - type d

Taking backup zip from multi-module Java project

I like to keep an archive of my code on a monthly basis. I wrote a small bash script to create zip out of my multi module Java projects. It gets the job done and can be improved to include resources and test folders as well. As of now I am only interested in the Java code zip backup.

!/bin/bash
 if [ -d "combined" ]; then
     echo "Removing existing zip files in combined .."
     rm combined/*.zip
 else
     echo "Creating combined folder.."
     mkdir combined
 fi
 for d in */ ; do
     if [ -d "$d/src/main/java" ]; then
         var="$(echo $d | sed 's/.$//')"
         echo "Processing $var .."
         cd $d/src/main/java
         zip -r ../../../../combined/$var.zip com
         cd ../../../..
     else
         echo "Ignoring .. $d"
     fi
 done

ssh_exchange_identification: read: Connection reset by peer

I recently upgraded my Mac Desktop and I noticed a strange problem that I was not able to SSH into my desktop from my laptop. I ultimately found out that the SSH setup that came with MacOS was having some issue as when I did ssh user@localhost on the desktop I got the same error:

ssh_exchange_identification: read: Connection reset by peer

I ended up tailing the system log using the following command:

sudo tail -f /var/log/system.log

I observed the following entries in the log whenever a SSH was attempted. 

com.apple.xpc.launchd[1] (com.openssh.sshd.[UUID][NUM]): Service exited with abnormal code: 1

It was clear that there was setup issue with the SSH that came with default MacOS installation. So I decided to spawn a separate SSH instance and watch it’s log:

sudo /usr/sbin/sshd -d -p 2222

This command showed up a lot of issues related to file permissions.

Permissions 0644 for '/etc/ssh/ssh_host_dsa_key' are too open.
Permissions 0644 for '/etc/ssh/ssh_host_ecdsa_key' are too open.
Permissions 0644 for '/etc/ssh/ssh_host_ed25519_key' are too open.

I fixed these permission issues by changing their permission to 400:

sudo chmod 400 /etc/ssh/ssh_host_dsa_key
sudo chmod 400 /etc/ssh/ssh_host_ecdsa_key
sudo chmod 400 /etc/ssh/ssh_host_ed25519_key

After this change the following command succeeded and I was able to do successful SSH connection to port 2222.

sudo /usr/sbin/sshd -d -p 2222

So I killed this process and decided to restart SSH:

sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist 
sudo lsof -i:22
echo $?
sudo launchctl load /System/Library/LaunchDaemons/ssh.plist 

Once SSHD was restarted I could successfully do logins using ssh user@localhost from Desktop as well as remote login via SSH from my laptop. 

Prettify JSON on Terminal

I love using curl command on my Mac terminal to debug my REST endpoints. However the REST call JSON output used to come in a blob of text which required further formatting in an Editor like Visual Studio Code. To allay this problem I ended up installing “jsonpp” using homebrew.

brew install jsonpp

So now I just pipe the output of my curl command to the jsonpp program and I get a fully formatted JSON.

$ curl http://localhost:8080/test | jsonpp
{
"year": 2018,
"month": 2,
"worked": 18,
"leaves": 2
}

 

 

Curl Command New Line Post Output

I like to use curl instead of UI tools like Postman for debugging my RESTful web services traffic whenever possible. I however didn’t like my output being messed up by the bash prompt being suffixed to the output. Something like the following:

$ curl -H "$auth_token" http://localhost:8080/xyz/abc-efg
["-","A","B","C","D","E"]$

So basically what I needed was to have a new line forced after the curl output. A quick search on internet yielded this article. So I executed the following command on my terminal.

$ echo '-w "\n"' >> ~/.curlrc

After doing this when I execute the same curl command I get the following output.

$ curl -H "$auth_token" http://localhost:8080/xyz/abc-efg
["-","A","B","C","D","E"]
$

So now the bash prompt is actually coming on a new line by default!

Uninstalling and reinstalling brew

I use brew utility for all my terminal based installation needs. However recently I had to deal with a corrupted brew install. To fix this I followed this link. The uninstallation and reinstallation worked fine and I now have a working brew instance.

The commands as mentioned in the above link are:

$ cd `brew –prefix`
$ rm -rf Cellar
$ brew prune
$ rm -rf Library .git .gitignore bin/brew README.md share/man/man1/brew
$ rm -rf ~/Library/Caches/Homebrew
$ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

 

Installing pip on MacOS

I recently started exploring python and immediately ran into the following issue:
$ pip install requests
-bash: pip: command not found

A quick google search yielded this result. So basically on MacOS you have Python preinstalled along with easy_install command. So the following command installs pip:

$ sudo easy_install pip
Searching for pip
Reading https://pypi.python.org/simple/pip/
Best match: pip 9.0.1
....
....
Processing dependencies for pip
Finished processing dependencies for pip

A quick and handy solution indeed!