Try   HackMD

IOT - Interface with the Raspberry Pi

Module 1

secure shell

headless

disconnect keyboard, mouse and screen

Secure SHell deamon (SSHD)

Deamon is a generic term on Unix Linux machines that is just a process that is forever running in the background waiting for something happen.

How to get your Raspberry Pi IP address ?

$ ifconfig

A protocal

翻成中文是協定,比較好聯想的是,當有兩台機器要溝通時,需透過某個介質來溝通,但是(半雙工通訊)不能兩個裝置同時在傳遞訊息,這種溝通模式其實就類似對講機,只能透過同一個頻道溝通;但在網路通訊中也有「全雙工」(Full-Duplex),像是電話或 TCP 連線,雙方可以同時發送與接收訊息,這點跟對講機不同。因此,就需要定義一個「協定」,來讓兩台機器建立溝通的默契,就比如說像對講機,當某一個人 小明 講完後加一個 over,讓對方 小王 知道說 小明 講完了,小王 可以開始講了。所以「協定」的用途就是在建立溝通的默契。

A protocal is basically a set of rules that define a lot if things. They define the communication.
So, they define how data should be transferred between two different nodes.
They define what data is contained in each packet.
They define what's in the header, they define a lot.
But the protocol defines how you should communicate.

在網路 (Internet) 上看到的 Packet(封包) 一般來說,不論是什麼 Protocal 都可以拆分成 Header 跟 Payload,其中 Head 包含協議相關的訊息e.g. Destination Address, information about the text, the size of the message;而 Payload 則是您要發送的實際數據(Data)。

Internet Protocal Family

  • IP (Internet Protocal) 網際網路協定

    IP protocal deals with host-to-host connections, so a machine to another machine, two hosts that are on the network. The communication between those two machines is dealt with by the IP protocol. It defines Host naming scheme (IP address), and it is an unreliable protocal, if you just use IP protocol alone you would not be guaranteed that it would arrive. It might fail for many number of reasons. Maybe it reaches a host, so when these messages travel, they travel from one machine to the next, to the next, until they get to their destination. Maybe it reaches a machine and that machine drops it for some reason, that machine has a bug. Maybe the machine crashes and it disappears, right? Things like this can happen. So for one reason or another the message might not get to the destination.

  • UDP (Unreliable Datagram(資料封包) Protocol)

    UDP deals with process-to-process communications. But remember every particular machine can have many processes running on it. Maybe I want a machine running a web browser, also running an online game. So those are two processes both communicating on the network. And my online game to online game server communication is different than my web browser to web server communication. So, that's going to allow the web browser to communicate with web server on the two different machines. And that's going to allow online game client to communicate with online game server on the two different machines. So in order to do that, these processes have to have names associated with them. These network processes, they have to have unique names as well. These are called Ports .

  • TCP (Transmission Control Protocol)

    TCP is also process-to-process communications, just like UDP. And it also uses the same type of process naming, but it's reliable communication. So what that means is if you send a message from one process to another on a different machine, that message is guaranteed to arrive at the destination. Now how does it guarantee this? Basically by retry, so it'll send a message and it'll wait for a response. If it doesn't get the response, it assumes the message didn't arrive so it'll try again and try again and so on. So that's built in to the TCP Protocol. So TCP is a more complex protocol than UDP, but it gives you better guarantees. So TCP is generally what we'll be using.

You can use UDP with IP or TCP with IP and either way (that's Internet). But you notice that you have to use IP with either TCP or UDP. IP has to be used because the hosts have to have their own unique naming (IP addresses). And then TCP or UDP had to be used, and each process was also be assigned a different name(Port).

TCP vs UDP

  • TCP and UDP are the transport layer protocols. They deal with process to process communication is basically what it means.

  • TCP is connection-oriented, so it's reliable. When you make a connection between two different processes that connection exists over a period of time. It's not just a one-off (不只是一次性的). It assumes that it exists for a while. Packet(資料封包) sequencing is supported. So it assumes you're sending many messages from one process to another. So it sequences the package(對資料封包進行排序), basically numbers of them. So at the receiving end(接收端), you know what order the packets were sent in, and that's important for a lot of applications, the order in which the packets are received may matter. TCP also handles various essential tasks such as flow control, error detection, and correction. For instance, if a bit is flipped due to electromagnetic interference (電磁干擾), the error is detected at the receiving end using a checksum or similar mechanism. Additionally, TCP manages flow control by adjusting data transmission rates and rerouting packets to avoid network congestion(網路擁堵), ensuring smooth and reliable communication.

  • UDP is connectionless. UDP, it assumes it's a one off, this one package that you're sending, it's just one and you don't intend to send any other or not anytime soon anyway. So, there's not some long lasting connection that exists when you're doing the UDP. It's unreliable, it does not guarantee packet arrival. It doesn't do packet sequencing. So packets arrive and the order that you sent them in might be different than the order they were received in. UDP doesn't attempt to label them so you can figure out the order. So at the receiving end, you have no idea what order the packets were sent in, you just know I got a bunch of packets. Which is fine in a lot of cases, as long as you don't care about the order. UDP is simpler and faster than TCP because it doesn't have to handle retries, and flow control and error correction detection and all that. So, that's a comparison between the two. And we're generally going to stick with TCP, TCP together with IP.

IP Address

  • An IP Address is a unique address for every machine on the internet.

  • TCP/IP uses IP address as this unique address. And these two protocols IP is essential. TCP or UDP are another pair protocals, but thede protocols are the protocols that make up the internet. So if you speak TCP and then you speak IP, then you are using the Internet. And if you don't, then you're not using Internet, that's the idea.

  • IPv4
    仍是使用最多的版本,它使用的是 32-bits 地址,So 32-bit means, 4 bytes. And each byte, normally the way you see it commonly are four numbers written in decimal separated by period. So in this case, 192.0.0.0. Four numbers, each one is between 0 and 255, because that's the byte, a byte it represents a number between 0 and 255.

  • IPv6
    由於 IPv4 只支援 232 台裝置,現在網際網路上的裝置數量早已超過 232 台了,IPv6 還整合了安全性,它使用 IPSec 來加密傳輸中的數據;反觀,IPv4 本身並沒有任何加密。而目前 IPv6 尚未被廣泛使用。

  • Ports
    A port number is a unique identifier for an application on a machine. IP addresses, they uniquely identify the machine, but on a particular machine, there may be many applications running on that machine. And each application maybe want to talk on the network, right, to other machines on the network. So then each application on the machine has a different port number. So the IP protocol it uses IP addresses to refer to a machine. But to refer to particular applications on a particular machine, you would use either the TCP or the UDP protocol. But TCP, UDP and their addressing, they use port numbers for the addressing. So, these ports to address a specific application on a machine. As an example, you can have a machine that wants to talk on, maybe you want to use a browser, right? So web browsers use port 80, web servers rather, they use port 80, right? So web browser sends a page request on port 80 to the web server, and the server is always listening to port 80 for request, and that's just one application. A firewall could just block off traffic in a port, so these ports have to be open. They can't be blocked by a firewall. Ports number is 16-bits value in TCP and UDP headers. 所以一個 TCP/IP or UDP/IP 可以有 65536 個 Ports. The low port numbers are the ones dedicated to particular applications, like when I say low 1023 and below. Above that they're just open ports, you can use them for whatever you want.

補充

Domain Name System (DNS)

截圖 2025-03-29 下午5.52.19截圖 2025-03-29 下午5.09.00

把 DNS 想像成一張將 網域名稱(Domain Name) 映射到 IP 位址 的大型對照表。由於 DNS 需要記錄全球的網路資訊,因此它採用了 分散式架構,並以 階層式(Hierarchical)表格 的方式存放在世界各地的不同伺服器上,而不是單一的表格。

DNS 查詢流程

當你在自己的電腦上想要造訪某個網站(例如 cnn.com)時,這個網域名稱必須先轉換為對應的 IP 位址,這個過程稱為 DNS 查詢(DNS lookup)

具體來說,當你輸入網址時,實際執行查詢動作的是你的 網頁瀏覽器(Web Browser)。查詢的流程如下:

  1. 檢查本機快取(Local Cache)

    你的電腦首先會檢查本地 DNS 快取(如瀏覽器的緩存),如果之前查詢過這個網站,且快取尚未過期,則直接使用快取的 IP 位址,無需進一步查詢。

  2. 查詢本地 DNS 伺服器(Local DNS Server)

    若本機沒有快取,查詢會發送至 本地 DNS 伺服器(通常是 ISP 提供的 DNS 伺服器,或者你手動設定的 DNS,如 Google DNS: 8.8.8.8)。若本地 DNS 伺服器快取以存在目標 IP Address,則直接回應查詢結果。

  3. 本地 DNS 伺服器向上層請求(Recursive Query)

    若本地 DNS 伺服器沒有這個網域的對應 IP 位址,它會向 更高層的 DNS 伺服器 查詢。

    查詢過程是一層一層進行,直到找到負責該網域的 權威 DNS 伺服器(Authoritative DNS Server)

  4. 頂級 DNS 伺服器(TLD Name Server)指引查詢

    若本地 DNS 伺服器無法解決查詢,它可能會向更上層的 根 DNS 伺服器(Root DNS Server) 請求。

    根 DNS 伺服器不會直接提供 IP 位址,而是指引到對應的 頂級網域(TLD)DNS 伺服器(例如 .com、.org 的管理伺服器)。

    TLD DNS 伺服器則會進一步指引至 負責 cnn.com 的權威 DNS 伺服器

  5. 權威 DNS 伺服器(Authoritative DNS Server)回應 IP 位址

    權威 DNS 伺服器 是最終存有該網站 IP 位址 的伺服器,例如 cnn.com 的官方 DNS 伺服器(192.168.0.1#53)會返回 151.101.79.5。
    截圖 2025-03-29 下午6.23.07

  6. 回傳結果與快取

    權威 DNS 伺服器將 IP 位址回傳至本地 DNS 伺服器,本地 DNS 伺服器會短暫快取這個結果,以加速後續相同請求。最終,IP 位址會回傳到你的瀏覽器,瀏覽器便能透過這個 IP 連接至網站伺服器,開啟網頁。

出於效能考量,不希望每次訪問網站時都進行 DNS 查詢。如果你已經查詢過一次 cnn.com,那麼與其每次輸入 cnn.com 時都重新查詢,系統會將結果存入快取(本地記憶體)。這樣,下次再訪問 cnn.com 時,就可以直接從快取獲取對應的 IP 位址,而不需要再次查詢 DNS,從而提升存取速度並改善效能。

DNS 查找工具 nslookup

截圖 2025-03-29 下午5.16.32

Server: 192.168.0.1 -> DNS 伺服器的預設(Default)位址
Address: 192.168.0.1#53 -> DNS 伺服器的位址,埠號(Port)53

所以,當我想從我的電腦上進行 DNS 查找,我的電腦必須先連到 192.168.1.1#53,而那裡就會是一個 DNS 伺服器在等待接收我的查找請求。

Non-authoritative answer:
Name: www.google.com
Address: 142.250.204.36 -> Google 的外部伺服器位址

Client-Server Model

需要 Client-Server Model 的基礎才可以開始學習透過程式設計連接到網際網路,而不單只是做個網際網路使用者。
截圖 2025-03-29 晚上8.24.28

client-server model describes most of the interactions over the network on the Internet, between machines. But this is not completely all inclusive. Sometimes you're talking peer-to-peer but client-server's a very common model for communication on the Internet.

Internet Connections

The client and the server communicate over a connection on the Internet.
截圖 2025-03-29 晚上9.02.34

Socket

A socket is the endpoint of a connection.
當客戶端與伺服器建立連線時,會有兩個 sockets,一個位於客戶端,另一個位於伺服器端。這些 sockets 負責雙方的通訊。socket 這個術語在網路程式設計中至關重要,因為在寫網路程式時,會頻繁地建立、開啟和關閉 socket。而 socket 本質上是一種實現網路通訊的程式設計概念。

socketIP 位址(IP address)和 埠號(port)有密切關聯,socket 是由 IP 位址(指的是運行該程序的主機)和 埠號(識別特定的應用程式)組成的。因此一個 Socket 總是與一個 IP 位址和一個埠號綁定在一起。這樣當你有一個 IP 位址和對應的埠號時,就能確定一個唯一的網路連線端點(socket)。

截圖 2025-03-29 晚上9.33.44

Socket Interface

截圖 2025-03-31 晚上8.15.41
截圖 2025-03-31 晚上8.16.04

import socket
import sys

try:
    mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print("Failed to create socket")
    sys.exit()

try:
    host = socket.gethostbyname("www.google.com")
except socket.gaierror:
    print("Failed to get host")
    sys.exit()

mysock.connect((host, 80))
message = b"GET / HTTP/1.1\r\n\r\n"

try:
    mysock.sendall(message)
except socket.error:
    print("Failed to send")
    sys.exit()

data = mysock.recv(1000)
print(data)
mysock.close()
import socket
import time

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    host = "140.116.245.212"
    port = 1234
    mysock.connect((host, port))
    print("Connected to server!")
except socket.error as e:
    print(f"Connection error: {e}")
    exit()

try:
    message = b"P76131385"
    mysock.sendall(message)
    time.sleep(1)
    print("Message sent!")
    data = mysock.recv(1024)
    print(f"Server response: {data.decode('utf-8')}")
        
except socket.error as e:
    print(f"Error sending/receiving data: {e}")
    mysock.close()

截圖 2025-03-31 晚上9.30.13

import socket
from colorama import Fore, Style
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

try:
    mysock.bind(("", 1234))
    mysock.listen(5)
    print("Server is listening on port 1234...")
except socket.error as e:
    print(f"Socket error: {e}")
    exit()

try:
    while True:
        conn, addr = mysock.accept()
        while True:
            data = conn.recv(1024)  # 連續接收資料
            if not data:
                break  # 連線關閉時結束這個連線的迴圈
            
            print(f"Received From {Fore.GREEN}{addr}:{Style.RESET_ALL}{data}")
            response = f"{Fore.GREEN}Message received!{Style.RESET_ALL}"
            conn.sendall(response.encode('utf-8'))
        conn.close()

except KeyboardInterrupt:
    print("\nServer Stopped...")
    mysock.close()