Sunday, May 15, 2016

Two And More SSH Keys in OSX

Short note about maintaining several "public - private" ssh key pairs in OSX.

Let's assume that You already have one "public - private" key pair locally which You're using for accessing something (git, jenkins etc.) and You don't want to use same keys for another resource then just follow next steps.

1. Move to Your .ssh directory

 $ cd ~/.ssh  

2. Generate key pair with next command

 $ ssh-keygen -t rsa -f ~/.ssh/secondKey -C "email@email.com"  

if You won't add secondKey (or whatever You want it to be named) this command will override Your main key, also You can add a key phrase or leave it blank.

3. Add an entry to Your config file inside .ssh folder (if You don't have that just create one with - $ touch config)

 Host firsthost.com  
  User git  
  Hostname firsthost.com  
  PreferredAuthentications publickey  
  IdentityFile ~/.ssh/id_rsa  
 Host secondhost.com  
  User git  
  Hostname secondhost.com  
  PreferredAuthentications publickey  
  IdentitiesOnly yes  
  IdentityFile ~/.ssh/secondKey  

That's simply it and if You need more keys - just repeat those steps above.

Wednesday, May 11, 2016

Enum and Property file cooperation in Java

Short reminder how to populate Enum in Java with values from property file.

1. Create an Enum as described in example below:

 public enum Users {  
   USER_1, USER_2;  
   private static final String PATH = "/selenium/environment/users.properties";  
   private static Properties properties;  
   private String value;  
   private void init() {  
     if (properties == null) {  
       properties = new Properties();  
       try {  
         properties.load(Users.class.getResourceAsStream(PATH));  
       }  
       catch (Exception e) {  
         e.printStackTrace();  
       }  
     }  
     value = (String) properties.get(this.toString());  
   }  
   public String getValue() {  
     if (value == null) {  
       init();  
     }  
     return value;  
   }  
 }  

Of course it's not an ideal solution and You may come up with better one but this works for me as expected and that's all I need.

2. Create a properties file (short reminder - this file should be placed into resources folder in Your project)

 USER_1=secretlogin1  
 USER_2=secretlogin2  

in this case we have just two entries but You can expand that.

3. Call that from code

 journey.login(Users.USER_1);  

Well it's pretty much it and hopefully this will save me from "googling" next time I'll need such code.

Saturday, April 9, 2016

Change File Permissions in Terminal OSX

Knowledge about changing permissions for a specific file is pretty important and can even save Your information from being lost.

So let's see what we can check and how.

1. Execute next command in Your terminal window:

$ ls -la

You should see same information as on screen below.


What do those letters form first column stand for?
- d = directory
- r = read access
- w = write access
- x = execute access
- - = no permissions at all

Column 3 indicates owner of a file and column four shows us an user's group which might has or not permissions to read/write/execute file.

So if we'll check first column again from left to write we'll see that:

- first symbol / several symbols - user's permissions
- second symbol (after dash) / symbols - group permissions
- third symbol / symbols - all other users permissions

Let's analyse example.txt file:

- user has read and write permissions
- group has read and write permissions
- other users have only read permission

2. If You want to change ownership of the file just execute:

$ sudo chown NEW_USER:NEW_GROUP example.txt

this command will update owner and group for this file.

3. If You want just update permissions for specific file then You need to execute:

$ sudo chmod 000 example.txt

this command deny all actions with this file for all users.

4. Let's explore octets in general:

- 0 - No read, no write, no execute  - " - "
- 1 - No read, no write, execute       - " -x "
- 2 - No read, write, no execute       - " -w- "
- 3 - No read, write, execute            - " -wx "
- 4 - Read, no write, no execute      - " r- "
- 5 - Read, no write, execute           - " r-x "
- 6 - Read, write, no execute           - " rw- "
- 7 - Read, write, execute                - " rwx "

5. So according to this table if we'll execute next command:

$ sudo chmod 432 example.txt

- owner will be able only to read file;
- group will be able to write and execute;
- all others will be able only to write.

6. If You want to apply same permission to folder and all its content You have to add one more flag to Your command:

$ sudo chmod -R 777 Documents

This is pretty much core knowledge which You have to have in order to change permissions for folder / file in OSX terminal.

Monday, April 4, 2016

Use Multiple Python Versions In OSX

This post aims to help with managing several different Python versions on OSX without any troubles.

As usually just follow next steps:

1. $ brew update

2. $ brew install pyenv

3. Add into .bashrc: 

" if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi"

4. Shows list of options

$ pyenv install -l  

5. Installs needed python version

$ pyenv install X.XX.XX 

6. Shows installed python versions in system

$ pyenv versions 

7. Sets specific version as global one

$ pyenv global X.XX.XX 

8. Sets specific version as local one

$ pyenv local X.XX.XX 

9. Unsets local version for python

$ pyenv local --unset 

For further exploring:

10. Shows list of available commands for pyenv

$ pyenv commands 

This is pretty much enough for this topic (at least for now).

Tuesday, March 29, 2016

Verification vs Validation

Shortest answer You can give for this:

Verification - do we build the thing right?

Validation - do we build the right thing?

Monday, March 28, 2016

Use Multiple Java Versions In OSX

Really common question today is - "How I can install and use several Java versions on my OSX without a headache?", so here's my answer.

Just follow simple and straightforward steps below (yes I did check those before publishing):

1. Check if You already have java installed:

$ java -version

2. If You have java locally then check location of installed version:

- installed from Apple

/System/Library/Java/JavaVirtualMachines/1.X.X.jdk/Contents/Home/

- installed from Oracle

/Library/Java/JavaVirtualMachines/jdk1.X.X_XX.jdk/Contents/Home

Remember that path.

3. Check if You have homebrew installed if not install it first:

- http://brew.sh/

4. Install jenv with homebrew:

$ brew install jenv

5. Add information about jenv into Your .bash_profile or .bashrc file:

if which jenv > /dev/null; then eval "$(jenv init -)"; fi

6. Save and reload .bashrc / .bash_profile:

$ source .bashrc (.bash_profile)

7. Check that jenv works properly:

$ jenv versions

This command should have next output by default:

$ * system (set by /Users/ajones/.jenv/version)

8. Add Your installed local java to jenv:

$ sudo jenv add /Library/Java/JavaVirtualMachines/jdkX.X.X_XX.jdk/Contents/Home/

Check if this added with jenv versions command.

9. Install cask with homebrew:

$ brew install cask

10. Install latest java from Oracle:

$ brew cask install java

Currently (March 2016) this command installs jdk1.8.0_74.jdk from Oracle.

11. Add latest installed java into jenv as peviously:

$ sudo jenv add /Library/Java/JavaVirtualMachines/jdkX.X.X_XX.jdk/Contents/Home/

12. Run jenv versions and verify that now You have 2 java versions installed on Your local machine

13. If You want to switch to needed version run:

$ sudo jenv local oracle64-1.7.X.XX or
$ sudo jenv local oracle64-1.8.X.XX

14. After each command You can run:

$ java -version

in order to check which java version is currently active.

15. If You want to change java version to all users:

$ sudo jenv global oracle64-1.7.X.XX or
$ sudo jenv global oracle64-1.8.X.XX


You can install as many java versions as You want - just add them to the jenv and then simply do switch when needed.

Sunday, January 3, 2016

BASH cheat sheet

Traditionally I'll put here some well known BASH commands for keeping those in one place in case I'll forget them :)

echo $BASH - should be /bin/bash - make sure that U're using bash :)

nano (vim) ~/.bash_history - shows all history of commands in bash
reset - resets terminal after crashing (eg trying to output binary file with less command etc.)

ls - list of content in current directory
ls -a - list of content in current directory including hidden files
ls -l - list of files in long format
ls -a /bin - list of all files in bin directory
ls -a Documents/ - list of all files in Documents directory without entering into that
ls -t - list by modification date
ls -S - list by size
ls -R - recursively list files in subdirectories
ls -d - don't go into subdirectories just list them
ls | wc -l - show how many files in the current directory

man - help for comands (Manual pages)
man ls - shows help related to ls command

Navigation inside man:
space - scrolls page down
b - scrolls page up
/ <option> - performs search inside help; for next occurrence of searched term press / + enter
q - exits help

pwd - Path to working directory
cd - changing directory / if without arguments navigates to Home directory
cd / - navigates to Root directory

file <filename> - shows information about file without opening it
file * - shows information about all files in current directory
open <filename> - opens file with default (specified in system) application
open <filename> -a <application> - opens file with wanted application
open . - opens current directory in Finder
cat <filename> - print content of file into console
less <filename> - show content of file with possibility of navigation (like inside of man)

tail <filename> - show end of the file
tail <filename> -n 5 - show last five lines of file
tail <filename> -f - show latest (realtime) updates in the file, suitable for debugging
head <filename> - show beginning of the file

mkdir <name> - creates a directory
touch <filename> - create an empty file (updates modification and access dates in case file already exists)
touch {a,b,c,}.txt ==> touch a.txt b.txt c.txt
touch {a..c}{1..3}.txt ==> touch a1.txt a2.txt ... c2.txt c3.txt

cp <filename with relative path> . - copy file to current directory
cp <filename with relative path> <new file name> - copy file to current directory with different name
cp -R <source directory / file> <target directory> - copy source directory / directories / files into target directory
rm <filename> - removes a file
rm -r <directory> - removes a whole directory
rmdir <directory> - removes an empty directory
mv <filename> - move a file (also can be used during renaming)
mv <filename>.. <directory> - move file / files into specified directory
mv file.{txt,jpg} dir/ ==> mv file.txt file.jpg dir/
cp / mv / rm -i - security switch - will ask if you are sure to perform an action

ls > <filename> - write content of directory into file - will override file  if exists
ls >> <filename> - write content of directory into file - will append information at the end of the existing file, otherwise will create a new file

ls | less - get output from first command and send it to input of second command

cut -f 3 <filename> | grep 4 | wc -l - get 3 rd column from file (assuming it's csv) then get all rows which contains 4  and then count how many lines contains that number

CTRL-r - reverse search in bash history - search for the latest occurrence. If wee need previous one just press CTRL-r again and so on. To exit from search mode without execution command we can use arrow (left, right) key

sort -nk2 <filename> - sort file by using second column (in space-separated file) as a key
sort -r <filename> - sort in reversed order
sort <filename> | uniq -c - sort file then counts how many time each line appeared
wc <filename> - show number of lines, words and bytes of the file
wc -l <filename> - show opnly number of lines

grep <search term> <filename> - search for all occurences in the file
grep <search term> *<filename pattern> - search for all occurences in several files
grep -i <search term> <filename> - search for all occurences in the file ignoring case
grep -v <search term> <filename> - invert search

find /bin - search for all files in bin directory
find /bin -name test - search for all files in bin directory with name test

ps ax - show all running processes
ps aux - show all running processes with owner
top - show more detailed information about running processes
CTRL-z bg - put program into background
CTRL-z fg - put program into foreground

open -a <program name> - start a program from shell
kill -KILL <process id> - hard kill for process which not responding

As usually I'm not pretending for full list of BASH options - as it's really huge but those listed above are the most common for daily usage :)