Tuesday, November 17, 2020

Check laptop battery percentage using terminal

There are 2 methods to do this.

First method

The first method is to use cat to display the content of /sys/class/power_supply/BAT*/capacity 
$ cat /sys/class/power_supply/BAT*/capacity

To see the updated value, refreshed every second. -n1 is for refresh every second, and -d is to highlight any changes.
$ watch -n1 -d 'cat /sys/class/power_supply/BAT*/capacity'


Second method

The second method, is to use upower command. To use this command, you need to know the object paths available for your device
$ upower -e
/org/freedesktop/UPower/devices/line_power_AC
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/battery_BAT1
/org/freedesktop/UPower/devices/DisplayDevice

So, to check the my first battery information, use below command
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage

Same goes with the second battery
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage

Tuesday, November 10, 2020

Using putty via cmd

Putty is an ssh client that many windows users used to connect to ssh server. Due to the complexity of ssh protocol, the settings in putty is also quite complex. 

Here I want to show some examples where you can use putty via the cmd. You can simply copy below commands and paste it inside your cmd, do some changes like IP, username and port, and you are good to go without having to go through many steps to complete some simple task such as setting up a dynamic tunnel.

The first thing that you have to do before using putty via cmd is, to install putty. Just download the installer from here, and install it using the usual double click on installer method.

Once installed, fire up your cmd. 

Below are some basic scenario to use putty (if you are familiar with the linux openssh client, the options are almost identical)


Scenario 1: Connecting to a remote server (with ip 10.10.10.10):

cmd> putty user@10.10.10.10


Scenario 2: Forward a remote port to you local machine, in this case, forwarding port 22 on 10.10.10.10 to port 9999 on your local machine

cmd> putty user@10.10.10.10 -L 9999:localhost:22


Scenario 3: Forward a local port to a remote machine, in this case, forwarding port 22 in localhost to 9999 in remote machine

cmd > putty -R 9999:localhost:22 root@10.0.0.16


Scenario 4: Create a socks5 proxy (dynamic tunnel) on port 8888 in localhost via the remote machine. This is useful if we want to browse addresses that only can be accessed by the remote machine, but the remote machine does not have an internet browser.

cmd> putty user@10.10.10.10 -D 8888


Scenario 5: Connect to a remote machine, via port other than 22. In this case, sshd is running on port 9999

cmd> putty -P 9999 user@10.10.10.10


There you go, 5 scenarios where you can use putty via command line. Hope this will make your life easier when using putty. Ciao!

Sunday, November 8, 2020

Checking your public IP from terminal

Sometimes, when you are setting up a linux machine, there is a need to know what IP our machine is being connected to the public internet. There are many websites that provide this kind of service, but below are some of my favorites that work well with commands. 


To be able to use this, we need a text based web browser. We will use curl in this case. Install curl if your machine does not have one.

# yum install curl -y


To get our ip address, run curl like below. Choose whichever you like :).

$ curl ipecho.net/plain

210.210.210.210

$ curl icanazip.com

210.210.210.210


The best, in my opinion is ipinfo.io, because it will give you a nicely formatted information regarding your public IP address, and if you want a specific info, just put it as a slash being ipinfo.io. For example, if you want to know what is your city, just curl ipinfo.io/city

$ curl ipinfo.io

{

  "ip": "210.210.210.210",

  "city": "Kuala Lumpur",

  "region": "Kuala Lumpur",

  "country": "MY",

  "loc": "3.1390,101.6869",

  "org": "AS4818 Digi Telecommunications Sdn. Bhd.",

  "postal": "50505",

  "timezone": "Asia/Kuala_Lumpur",

  "readme": "https://ipinfo.io/missingauth"

}

$ curl ipinfo.io/ip

210.210.210.210

$ curl ipinfo.io/city

Kuala_Lumpur

Simple http/https proxy using squid

This is a basic proxy setup, whereby you practically does not have to do any settings.

First, install squid in our machine with address 10.0.0.10

# yum install squid -y


Then, start squid service

# systemctl start squid


By default squid runs on port, 3128, so we will allow 3128/tcp on our firewall

# firewall-cmd --add-port 3128/tcp

# firewall-cmd --add-port 3128/tcp --permanent


Now the time to test out our proxy, in a different machine. This test machine does not have any internet connection to the outside world, except network connection to our proxy server.


First we use curl to try to reach ipecho.net/plain without proxy

$ curl ipecho.net/plain

curl: (6) Could not resolve host: ipecho.net


Next we will use curl with our brand new proxy. We can see that our machine is able to reach ipecho.net/plain, with the help of the proxy.

$ curl --proxy http://10.0.0.10:3128 ipecho.net/plain

210.210.210.210


Nice, happy proxying.

Saturday, November 7, 2020

Set static IP address in ubuntu 18.04

Starting from ubuntu 18.04, network configuration is now handled by netplan, which is based on YAML.


To set a static ip in netplan, you need to know what is the interface name that you want to configure. To do that, just run "ip address" command
$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:18:46:ea brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.154/24 brd 192.168.0.255 scope global dynamic enp0s3
       valid_lft 604595sec preferred_lft 604595sec
    inet6 2001:e68:542c:e410:a00:27ff:fe18:46ea/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 259188sec preferred_lft 172788sec
    inet6 fe80::a00:27ff:fe18:46ea/64 scope link 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 08:00:27:7d:fa:68 brd ff:ff:ff:ff:ff:ff

You can see that we have 2 interfaces, enp0s3 and enp0s8. Now we have to locate which file need to be edited to give enp0s8 a static IP address. We can do that by:
$ ls /etc/netplan/
01-netcfg.yaml

To add an ip address  of 10.10.10.10/24 to enp0s8, with gateway 10.10.10.1 and nameserver 8.8.8.8 and 8.8.4.4, simply edit /etc/netplan/01-netcfg.yaml as per below
$ sudo vi /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes
    enp0s8
      dhcp4: no
      addresses: [10.10.10.10/24]
      gateway4: 10.10.10.1
      nameservers:
        addresses: [8.8.8.8,8.8.4.4]

Save the file. Since this is a yaml based configuration, make sure that the indentation is uniform across the file. To test your configuration for any syntax error, and press ENTER if you agree to use the configuration:
$ sudo netplan try
Do you want to keep these settings?


Press ENTER before the timeout to accept the new configuration


Changes will revert in 119 seconds
Configuration accepted.  

Once accepted, verify the ip has been set using "ip address" and "ip route"
$ ip address show dev enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:7d:fa:68 brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.10/24 brd 10.10.10.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe7d:fa68/64 scope link 
       valid_lft forever preferred_lft forever

$ ip route show dev enp0s8
default via 10.10.10.1 proto static 
10.10.10.0/24 proto kernel scope link src 10.10.10.10