Thursday, January 14, 2021

Common vim and linux terminal command, and setx for Windows

1. vim:

switch to input mode from command mode


[esc]

switch to command mode from input mode


:wq 

save and quit in command mode


:/

search a text in command mode


:dd

delete a line in command mode


:set number

show line numbers


:u

undo


2. Linux bash

grep textToSearch

Text search in lines


grep -i textToSearch

Text search in lines, ignore case


clear

clear screen


ps -aux

print current running process information, like PID, command line parameters


watch command -n watchinterval

execute command repeatedly in the specified interval (default is 2s)

watch date


cat filename

show file content


pwd

get current directory


ls

list all file and folder in the current directory


env
show all environment variables


mkdir -p path
Create path as well as all missing parent path


sed -i -e "s|old1|new1|g" -e "s\old2|new2|g" path/filename
in place replacement (-i) of strings in path/filename from "old1" to "new1","old2" to "new2"

echo 'some string' | base64 
base64 encode an string

echo 'some encoded string' | base64 --decode
decode base64 encoded string

base64 filename > encodedfilename
base 64 encode a file to a new file

base64 --decode encodedfilename > decodefilename
base64 decode a file to decoded file


3. openssl

openssl genrsa -out ca.key 4096
Create key for root ca

openssl req -x509 -new -nodes -key ca.key -sha256 -days 1024 -out ca.crt

create self signed root ca certificate 

openssl genrsa -out server.key 2048
create key for server

openssl req -new -key server.key -out server.csr

create signing request for server certificate


openssl x509 -req -in server.csr -CAcreateserial  -CA ca.crt -CAkey ca.key -out server.crt

create server certificate from signing request by signed with root CA


openssl x509 -in /etc/kubernetes/pki/apiserver.crt -text
view certificate information


4. network

Route table is used for ip to ip routing. Arp table is used for ip to mac address mapping.


show which ip and port are listening from which process

netstat


ip link
show list of network interface on the host

ip addr
show the address of network interface on the host

ip addr add 192.168.1.10/24 dev ech0
set ip address on a network interface ech0

ip addr del 192.168.1.10/24 dev ech0
remove ip address from network interface ech0

ip route
view routing table

ip route add 192.168.1.0/24 via 192.168.2.1
add an entry into the routing table

IP forwarding config file to decide whether network requests can cross different network interface
/proc/sys/net/ipv4/ip_forward
1 is enabled, 0 is disabled

To persist the above config across system restart, update file 
/etc/sysctl.conf

net.ipv4.ip_forward=1


Host name resolution file

/etc/hosts


DNS config file

/etc/resolv.conf


Change DNS lookup order

/etc/nsswitch.conf

hosts:          files dns


Show dns lookup information

nslookup hostnametolookup


list network namespace

ip netns


add network namespace red

ip netns add red


run ip command from namespace of red

ip -n red link

or 

ip netns exec red ip link



create a virtual link (veth type) to connect two interfaces you named: vethred and vethblue

ip link add vethred type veth peer name vethblue


set interface vethred to a particular namespace red

ip link set vethred netns red

ip link set vethblue netns blue



set ip address for the network interface link vethred and vethblue

ip -n red addr add 192.168.15.1/24 dev vethred

ip -n blue addr add 192.168.15.2/24 dev vethblue


enable the interface link to up state

ip -n red link set vethred up

ip -n blue link set vethblue up


ping from one namespace to another using link interface ip

sudo ip netns exec red ping 192.168.15.2


delete interface link

ip -n red link delete vethred


add a virtual bridge v-net-0 of bridge type as switch

ip link add v-net-0 type bridge


bring bridge interface up

ip link set v-net-0 up


verify the status by 

ip link


create link to the bridge

ip link add vethred type veth peer name veth-red-br

ip link add vethblue type veth peer name veth_blue_br


set link to namespace

ip link set vethred netns red

ip link set vethblue netns blue

ip link set veth-red-br master v-net-0


set ip address for the link

ip -n red addr add 192.168.15.1/24 dev vethred

ip -n blue addr add 192.168.15.2/24 dev vethblue



bring link interface up

ip -n red link set vethred up

ip -n blue link set vethblue up


set ip address for bridge

ip addr add 192.168.15.5/24 dev v-net-0

ip addr add 192.168.15.5/24 dev veth-blue-br


ping from one namespace to another

ip netns exec red ping 192.168.15.2


display and config network configuration

ifconfig



setX for windows

Sometimes IT may disable user to access user account settings from control panel, so users cannot update the environment variables. To bypass the issue, you can use setx command, which is quite often missed by IT
setx JAVA_HOME c:\yourJDKFolder 


Tuesday, December 22, 2020

Connect to Google Cloud VM with SSH on MAC

On MAC, terminal app can be used to connect to Google Cloud VM with the below steps:

1. open terminal console, and generate the key pair with your user name (for example, student), and save it to a particular folder when promoted

ssh-keygen -C student 

2. open Google Cloud console, and open VM instance, click the edit link, and Add a new SSH Key section. Open the public key file generated in last step, copy and past the content to the public key text box in Google Cloud Console. The user name should show on left side of the text box. Save the change.


3. From the mac terminal, interactively connect to Google Cloud VM instance with ssl, with the parameter of private keyfile (master) and username (student) and VM public ip address as below

ssh -i /Users/mpro2/Documents/master student@35.224.168.51

ssh mynode -t bash


to execute a single command run below

ssh -i /Users/mpro2/Documents/master student@35.224.168.51 ifconfig

ssh -i /Users/mpro2/Documents/master student@35.224.168.51 ip link



Note, by default, the ip address of VM in GCP will change each time when VM restarts.



Monday, December 14, 2020

Set environment variable for JAVA_HOME

1. .profile for mac user

with the new update for mac os, the default terminal has moved from bash to zsh, so if you previously use .profile to setup the environment and path for current user, then you will need to change the file name to .zprofile, as zsh will load .zprofile, instead of .profile when a new terminal starts.


2.Display environment variable in console or terminal window

on Windows

echo %JAVA_HOME%


on mac, 

echo $JAVA_HOME


3. set environment variable for a particular terminal or console 

on windows:

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0

Do not include quotation marks. This will not affect the JAVA_HOME environment variable setting in control panel's environment settings. 

Sunday, November 1, 2020

Understanding popState and pushState of browser history

The pushState method and popState event can be used to manage browser history when user clicks browser's back and forward button. Although the way to use those methods are not very straight forward.

When a browser visits a url, that url showing on the browser address bar is the current state. If user clicks back button, or call history.back(), the browser will load the previous url of the browser.

If application calls history.pushState(newStateObj, title, url, param), then the current state and url shown in the address bar will be pushed into the browser history, and then the url and state parameters in the pushState call becomes the current state and url, the new url will also show on the browser address bar. However, the browser will not actually load the new url set by pushState method.

When user clicks back button, the popState event is triggered, the last state and url before the current state and url (the url showing on browser before history.pushState method is called) is loaded in the browser, however, the page content will not change, and the application must update the page content by implementing the logic in popState event handler.

Wednesday, September 30, 2020

Typescript polymorphism for Angular component

Angular project may have several components that have some shared methods and properties. It is better to create a common base component and then extends it to to other sub components. The base component has an empty html template and css style, and only contain typescript code.

Both base component and sub components need to be added into module declaration section.

The injected constructor parameter needs to be passed from subclass constructor to base class constructor as constructor parameters with public access.

Similar to Java, typescript uses runtime binding when deciding which method to be invoked, it does not matter what is the current variable type, instead it will invoke the instance object's last method implemented by the inheritance tree. If the subclass does not implement the method, then it goes up to find the implementation from its parent class.

When a method in base class calls another method, it will invoke the implementation of the subclass. The only way to invoke the parent class' method is using super.certainFunc().

Sample code:


class animal {
  public name: string;
  constructor(n) {
    this.name = n;
  }
  public makeSound() {
    console.log(this.name);
  }

  public run() {
    this.makeSound();
    console.log('animal run');
  }
}

class dog extends animal {
   constructor(dn){
     super(dn);
   }
   public makeSound() {
     console.log('dog call. ' + name);
   }

   public run() {
    this.makeSound();
    super.makeSound();
    super.run();
    console.log('dog run');
  }
}

call the method from a button event handler:
  onTempTest2() {
   const d = new dog('pa');
   d.makeSound();
   d.run();
   const a: animal = d;
   a.makeSound();
   a.run();

  }

 

Monday, September 7, 2020

Azure windows server virtual machine setup notes

When setting up Azure Windows server virtual machine, there are several issues to handle in order to make it work:

1. after creating the virtual machine, you can install iis by running the below command in windows server's power shell prompt

Install-WindowsFeature -name Web-Server -IncludeManagementTools

Note, in virtual machine's azure portal's networking blade, you must add an inbound port rule for port 80 with priority of 100 in order to connect the iis using the vm's ip address.


2. Disable IE enhanced to allow IE to download other apps

By default, IE does not allow you to visit or download anything, you will need to connect to VM from remote desktop to allow IE to download. To do so, open VM's server manager app in VM, and click on Local Server and then click on "IE Enhanced Security Configuration" option, turn the option to off for both Administrator and user.

3. download and install dotnet core

By default, dotnet core is not installed on Windows VM server, you will need to download and install dotnet core in order to create and build dotnet apps.

Tuesday, September 1, 2020

Browser tab, javascripte, and sessionstoage

 Some note about browser tabs, javascript and session storage:

1. On same browser window, each tab will create and have its own session storage, the data stored in session storage are not shared between tabs. To share the data, use localstorage.

2. Data in session storage are kept as long as the tab is not closed. If user navigates away from current url to other urls, and then navigates back to the original url later, the previous session storage data are still available. Session storage will only lose if user closes the tab.

3. When the browser window is not active, or the tab is not the active, the javascript still still runs in background, although the event are raised slowly than active window or tab, (about once per second).