Skip to main content

SSH Tunnel

Local Port Forwarding​

ssh -L [client_port]:[host_to_forward]:[host_to_forward_port]

Suppose there is a client A, an SSH server B, and a host C, and you want to forward C:8080 to port 80 on A.

ssh -L80:C:8080 B

With this forwarding, accessing http://localhost from A will display the same content as http://C:8080 on B.

In summary, even if C is not visible from A, you can access the home network via SSH if port 22 is open to the outside. If B is visible from A, you can forward C's port to A via B.

Using -g allows access to C:8080 from computers on A's network using A's hostname.

Remote Port Forwarding​

ssh -R [client_port]:[host_to_forward]:[host_to_forward_port]

Unlike local forwarding, it forwards ports visible from the client instead of ports visible from the destination.

Suppose there is a client B, an SSH server A, and a host C, and you want to forward C:8080 to port 80 on A.

ssh -R80:C:8080 A

With this forwarding, accessing http://localhost from A will display the same content as http://C:8080 on B.

Even if B is not accessible from the outside, if a connection between B and A is established, you can forward the content of C to A. It is often used when you cannot directly operate B. When a connection between B and A is broken, tools like auto-ssh are often used to automatically reconnect.

How to Install Powerline

Go Installation​

Install Go to install powerline-go.

brew install go

Powerline-go Installation​

go get -u github.com/justjanne/powerline-go

Configuration

Bashrc​

Open ~/ .bashrc with Vim or nano and add the following.

GOPATH=$HOME/go
function _update_ps1() {
PS1="$( $GOPATH/bin/powerline-go -newline -error $? )"
}
if [ "$TERM" != "linux" ] && [ -f "$GOPATH/bin/powerline-go" ]; then
PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
fi

Run source ~/.bashrc to apply the changes.

Font

This should finish it, but there might be character encoding issues. You need to set a font that corresponds to Powerline. On Windows, you can use Cascadia Code PL or Cascadia Mono PL.

We recommend PlemolJP from https://github.com/yuru7/PlemolJP/. Download it from https://github.com/yuru7/PlemolJP/releases, click on PlemolJP_NF_vX.X.X.zip, download, extract, and install the font. Set the font to PlemolJP35 Console NF.

Avoid using delay() in Arduino

Using delay() in Arduino prevents any other actions from being performed during the waiting time. I created a program that blinks an LED with a 1-second cycle using millis().

  1. Get the time using millis() and divide it by the interval, assigning the result to t.
  2. Compare the previous t and the new t and execute a function if they are different.

Example

unsigned long t = 0, ot;

void sетуp(){
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
ot = t;
t = millis() / 500;
if(ot != t){
if(t % 2){
digitalWrite(LED_BUILTIN, LOW);
}else{
digitalWrite(LED_BUILTIN, HIGH);
}
}
}

Setting up a Raspberry Pi without a monitor

This guide explains how to set up a Raspberry Pi without needing a monitor.

  • Requires a Raspberry Pi device that can connect an Ethernet cable for a wired connection.
  • A computer is needed to operate the Raspberry Pi.

Install Raspberry Pi Imager, select the OS. Choose the micro SD card to write to, and click WRITE.

Raspberry Pi Imager

Configuration

Enable SSH​

Enabling SSH allows you to operate the Raspberry Pi remotely.

Reinsert the micro SD card and create a text file named ssh directly in the root directory. No extension is needed.

Raspberry Pi enable SSH

Enable VNC​

Open config.txt and uncomment and save the following lines:

framebuffer_width=1280
framebuffer_height=720

Raspberry Pi enable VNC

Boot Up​

Insert the micro SD card into the Raspberry Pi, connect the power, and boot it up.

Connect via SSH​

Connect using hostname raspberrypi, username pi, and password raspberry.

Raspberry Pi connect SSH

Update Packages​

sudo apt update
sudo apt upgrade -y

Enable VNC​

sudo raspi-config

Raspberry Pi config

Select 3 Interface Options, then P3 VNC, and select YES. Close with Finish.

Connect via VNC​

Raspberry Pi VNC viewer

Use the same username and password as for SSH.

Raspberry Pi VNC connected

Enable Wi-Fi​

Enabling Wi-Fi via VNC eliminates the need for a wired connection, allowing you to operate the Raspberry Pi with only power.

Troubleshooting CGI not working with Ruby installed via rbenv on Webrick

When executing CGI in the browser, an error occurs stating: /usr/bin/env: 'ruby': No such file or directory

#!/usr/bin/env ruby

# ...

Cause

The $PATH is not set.

#!/usr/bin/env bash

echo -ne "Content-type: text/html\n\n"
echo $PATH

Running this will display:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

This shows that the directory containing Ruby is not present.

Solution

Set the Ruby path using :CGIPathEnv.

srv = WEBrick::HTTPServer.new({
:DocumentRoot => "./site/",
:Port => 8080,
:CGIPathEnv => ENV["PATH"]
})

How to create a gem

Creating a Template​

bundle gem <GEM Name> -t
cd <GEM Name>

Editing the Gemspec​

  1. Open <GEM Name>.gemspec.
  2. Edit spec.summary, spec.description, spec.homepage,
  3. Write the homepage URL to spec.metadata["allowed_push_host"]
  4. Write the Gem's page to spec.homepage
  5. Write the repository URL to spec.metadata["source_code_uri"]
  6. Write the URL of changelog.md to spec.metadata["changelog_uri"]

Set at least this much.

Push to GitHub and Install​

git init
git add .
git commit -m First Commit
git remote add origin [email protected]:<username>/<GEM Name>.git
git push -u origin master

Install​

gem install specific_instal
gem specific_install -l "git://github.com/<username>/<GEM Name>.git"

Gemfile​

gem "<GEM Name>", github: "<username>/<GEM Name>.git", branch: :main

Displaying Images in the Terminal

Introducing the tiv command for displaying images in the terminal.

tiv

It's convenient when you don't want to open a file explorer.

git clone https://github.com/stefanhaustein/TerminalImageViewer.git
cd TerminalImageViewer/src/main/cpp
make -j
sudo make install

Euclidean Algorithm in Bash

  1. Let C be the remainder when A is divided by B.

  2. Let D be the remainder when B is divided by C.

  3. Let E be the remainder when C is divided by D.

    ...

  4. Let Y be the remainder when X is divided by 0.

    Then Y is the greatest common divisor.

Bash Script

#!/usr/bin/env bash

function gcd(){
test $2 -eq 0 && echo $1 || gcd $2 $(( $1 % $2 ))
}

gcd 1071 1029
# 21

Installing ImageMagick (Ubuntu)

wget https://download.imagemagick.org/ImageMagick/download/ImageMagick-7.0.11-14.tar.xz

Extract

tar xf ImageMagick-7.0.11-14.tar.xz

Make

sudo apt update
cd ImageMagick-7.0.11-14.tar.xz
./configure
make -j
sudo make install
sudo ldconfig /usr/local/lib