Login Using SSH Private Key

Daily post #3 – Today I’ll share how to login to a server using SSH and a private key 🙂

#1. Save Your Private Key

The very first thing is to save your private key in a text file. It could be in .txt or .pem format.

Next, open terminal, navigate to the folder which contains your private key and change it’s permissions by typing in the following command:

[code]chmod 600 filename.txt[/code]

#2. Prepare Credentials

Before you login, you’ll need these SSH credentials:

  • hostname
  • username
  • port (default SSH/SFTP port is 22)

#3. Login Via SSH

Once you have the credentials, just open the terminal, navigate to the folder containing your private key file and type the following command:

[code]ssh -i filename.txt username@host -p port[/code]

You of course have to replace filename, username, host and port in that command with your respective server credentials.

If it returns a “permission denied” error, you’ll have to make sure you changed your private key’s permissions (check step #1).

#4. Other useful commands

Once you’re connected. You can make use of the following commands:

  • pwd (present working directory) – does pretty much what it says!
  • ls -al – shows a list of files and folders with their permissions
  • rm myFile.txt – delete a file
  • rm my*.txt – delete files starting with my and ending with .txt
  • rm -rf myfolder – delete a folder

That’s all in this post! Next post, tomorrow!

Change Text Using JS querySelector

This is my 2nd post for the daily-post project. Today I learned a bit about querySelector in JS. I suggest you copy paste the following code in your Sublime Text and save it as your-desired-filename.html on desktop or your sandbox folder.

This is such a simple trick to turn your static page into a dynamic one!

  1. We basically write `I love Batman` text inside the paragraph tag p.
  2. Then use the querySelector to select the first occurrence of p tag in the source code.
  3. Save that as reference in variable t.
  4. Add an event listener to it which will trigger myFunction when clicked
  5. myFunction will prompt us to enter a name and then modify the content inside the variable t.

That’s all. Simple but useful. Expect more JS in my future posts 🙂

How To Remove PPPoE Icon From The Menubar In Mac

I had configured PPPoE on my network at some point and this made my OS display an icon on the top right corner in the menu bar. I tried to find a way to remove it but nothing worked.

Then I finally found this simple solution on StackOverflow! It said, I can simply press CMD key and drag the icon out of the menu bar. This worked! You can also use this trick to rearrange the icons in the menubar!

This didn’t require a tutorial I know but I’m sharing this on my blog because I plan to write a post on a daily basis. I may not get time to write lengthy or super detailed posts but I will try to squeeze out a post everyday.

I guess short posts will help to achieve this! That’s my plan! Let’s see how it goes! 🙂

Java Basics In One Page

I realized I cannot really cover every part of Java by writing blog posts. I mean I can but there’s no point reinventing the wheel when so many tutorials are already available. What I can do though is write a short guide which I may use in the future to revisit Java basics. So here we go!

#1. Get the tools

To start with Java, you’ll need JDK installed. Get it from here. Once you install JDK in your computer. Check whether the following commands are valid in your terminal.

[code]
javac
java
[/code]

They should be valid and shouldn’t return an error! Once you got that, just install a good text editor like Sublime Text (I recommend Sublime).

#2. Print Hello World

Next, just copy paste the following code in a new file inside Sublime text and save it as hello.java on your desktop!

[code lang=”java”]
class hello {
public static void main (String args[]) {
System.out.println("Hello World");
}
}
[/code]

Now open your terminal, navigate to desktop (usually cd desktop) and then run the following commands:

[code]
javac hello.java
java hello
[/code]

First command should simply compile the code and create an object file like hello.class in the same directory. The next command will actually execute the class file which was created. This will print Hello World in the terminal.

#3. Variables And Basic Maths

Now create integer variables and so some basic maths using Arithmetic operators:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;
int y = 10;
System.out.println(x+y);
}
}
[/code]

This should create two variables (imagine boxes) containing 5 and 10. And then we simply output them on screen using the arithmetic operator plus (+). Try other operators for subtraction, multiplication, division and modulus (%). Modulus returns the remainder from the division.

#4. Strings

Like integers, you can also create string variables:

[code lang=”java”]
class hello {
public static void main (String args[]) {
String xyz = "Omkar";
System.out.println(xyz);
}
}
[/code]

This will simply print Omkar on screen!

#5. Concatenation

You can print them both together using the concatenation operator (+ operator).

[code lang=”java”]
class hello {
public static void main (String args[]) {
String xyz = "My number is: ";
int num = 5;
System.out.println(xyz + num);
}
}
[/code]

This will simply print “My number is: 5” on screen by concatenating the two variables before printing them.

#6. If Statement

Decision making is an important part in coding. You can do this using If statement. It’ll check whether a condition is true and then execute a certain piece of code:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;

if (x > 10) {
System.out.println("Batman");
}
}
}
[/code]

This will check if 5 is greater than 10 and return true or false. Since this condition returns false, Batman will not be printed.

#7. If Else

What if we want to print something when condition turns out to be false? We use Else block in that case:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;

if (x > 10) {
System.out.println("Batman");
}
else {
System.out.println("Superman");
}
}
}
[/code]

#8. Else If

We can have multiple condition checks using else if:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 5;

if (x > 10) {
System.out.println("Batman");
}
else if (x == 5) {
System.out.println("I’m a Number");
}
else {
System.out.println("Superman");
}
}
}
[/code]

This should print “I’m a Number” as x equals to 5.

#9. Switch

Switch is another decision making statement. Here’s how you can use it to choose or switch the flow using different conditions:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int x = 3;

switch(x) {
case 1: System.out.println("1");
break;
case 2: System.out.println("2");
break;
case 3: System.out.println("3");
break;
default: System.out.println("nothing");
}
}
}
[/code]

This should print 3 on screen as x is 3. If nothing returns true, default case is executed.

#10. Array

My previous post was about arrays but I’ll go ahead and add the code here again to keep everything on a single page –

[code lang=”java”]
class hello {
public static void main (String args[]) {
int[] orray = {1,2,3,4,5,6};

for(int i=0; i<orray.length; i++) {
System.out.println(orray[i] + " ");
}
}
}
[/code]

Note: there are multiple ways to create an array. I’ve just shared one of them in the example above!

#11. Loops

I’ve written a separate post about loops already! You can check it out by clicking here. I had missed the for each loop in that post, so let’s cover that here:

[code lang=”java”]
class hello {
public static void main (String args[]) {
int[] orray = {1,2,3,4,5,6};

for(int i:orray){
System.out.println(i);
}
}
}
[/code]

For each loop basically does the same thing as a for loop but is very useful for traversing an array!

#12. End

That’s all in this post. There’s so much more which I can cover but I’ll stop here for now. Maybe I’ll add more to this post in the future but I want to keep this simple so I’ll just leave it for now 🙂

Arrays in Java

So there’s something called Array in Java. In an array, you can store fixed-size sequential collection of elements of the same type.

The simplest way to create an array is using the following code:

[code lang=”java”]
int[] orray = {1,2,3,4,5,6};
System.out.println(orray[0]);
[/code]

This code declares and creates an array called orray of size 6 starting from index 0 to index 5. And on the next line we use print statement to print the element at position 0 in the array (which is 1).

The coolest part about arrays is that we can run a loop and print (or do something with) all the elements in the array as follows:
[code lang=”java”]
class hello {
public static void main (String args[]) {
int[] orray = {1,2,3,4,5,6};

for(int i=0; i<orray.length; i++) {
System.out.println(orray[i] + " ");
}
}
}
[/code]

This should print numbers from 1 to 6 (elements in the array), each on it’s own line. We can do so much more with arrays but that’s all you need to know for now!