OSI Reference Model

OSI Reference Model 7 Layer Network Protocol TCP IP Physical Data Link Network Transport Session Presentation Application ISO Troubleshoot

Layerชื่อหน้าที่Protocolอุปกรณ์
7ApplicationInterface ผู้ใช้HTTP FTP DNS SMTP-
6Presentationแปลง เข้ารหัสSSL/TLS JPEG MPEG-
5Sessionจัดการ SessionNetBIOS RPC-
4Transportส่งข้อมูลถูกต้องTCP UDP-
3Networkหาเส้นทางIP ICMP OSPF BGPRouter
2Data Linkส่งใน LANEthernet Wi-Fi ARPSwitch
1Physicalสัญญาณไฟฟ้าRJ45 Fiber OpticHub Cable

แต่ละ Layer

# === OSI Model Deep Dive ===

# Layer 1 — Physical
# สายเคเบิล UTP Cat5e Cat6 Fiber Optic
# สัญญาณไฟฟ้า แสง คลื่นวิทยุ
# อุปกรณ์: Hub, Repeater, Modem
# Bandwidth: 100Mbps, 1Gbps, 10Gbps

# Layer 2 — Data Link
# MAC Address: 00:1A:2B:3C:4D:5E
# Frame: Header + Data + Trailer (FCS)
# Switch ส่งข้อมูลตาม MAC Address Table
# ARP: IP -> MAC Address Resolution
# VLAN: แบ่ง Broadcast Domain
# STP: Spanning Tree Protocol ป้องกัน Loop

# Layer 3 — Network
# IP Address: 192.168.1.1 (IPv4), 2001:db8::1 (IPv6)
# Subnet Mask: 255.255.255.0 (/24)
# Router หาเส้นทางด้วย Routing Table
# OSPF BGP RIP — Routing Protocol
# ICMP: ping traceroute
# NAT: แปลง Private IP -> Public IP

# Layer 4 — Transport
# TCP: Connection-oriented, Reliable, 3-way Handshake
# UDP: Connectionless, Fast, No Guarantee
# Port Number: HTTP=80, HTTPS=443, SSH=22, DNS=53
# Flow Control, Error Recovery, Segmentation

# Layer 5-7 — Upper Layers
# Session: จัดการ Connection Login/Logout
# Presentation: Encryption (SSL/TLS), Compression, Format
# Application: HTTP, FTP, DNS, SMTP, SNMP

from dataclasses import dataclass

@dataclass
class OSILayer:
    number: int
    name: str
    pdu: str
    protocols: str
    devices: str
    troubleshoot: str

layers = [
    OSILayer(7, "Application", "Data", "HTTP FTP DNS SMTP SSH", "Firewall (L7)", "App ทำงานไหม DNS Resolve?"),
    OSILayer(6, "Presentation", "Data", "SSL/TLS JPEG MPEG ASCII", "-", "Certificate Valid? Encoding ถูก?"),
    OSILayer(5, "Session", "Data", "NetBIOS RPC PPTP", "-", "Session Timeout? Connection Reset?"),
    OSILayer(4, "Transport", "Segment", "TCP UDP", "Firewall (L4)", "Port เปิด? TCP Handshake?"),
    OSILayer(3, "Network", "Packet", "IP ICMP OSPF BGP ARP", "Router", "IP ถูก? ping ได้? Route?"),
    OSILayer(2, "Data Link", "Frame", "Ethernet 802.11 ARP STP", "Switch", "MAC ถูก? VLAN? Duplex?"),
    OSILayer(1, "Physical", "Bits", "RJ45 Fiber 802.3", "Hub Cable", "สายหลุด? ไฟติด? Link Up?"),
]

print("=== OSI 7 Layers ===")
for l in layers:
    print(f"  Layer {l.number}: {l.name} (PDU: {l.pdu})")
    print(f"    Protocols: {l.protocols}")
    print(f"    Devices: {l.devices} | Troubleshoot: {l.troubleshoot}")

Troubleshooting

# === Network Troubleshooting ===

# Bottom-up Approach (Layer 1 -> 7)

# Layer 1: Physical
# ip link show                    # Linux: interface status
# ethtool eth0                    # Link speed/duplex
# # Windows: ipconfig /all

# Layer 2: Data Link
# arp -a                          # ARP table
# ip neighbor show                # Linux ARP
# # Switch: show mac address-table

# Layer 3: Network
# ping 8.8.8.8                    # ICMP connectivity
# traceroute 8.8.8.8              # Route path
# ip route show                   # Routing table
# # Windows: tracert 8.8.8.8

# Layer 4: Transport
# ss -tlnp                        # Linux: listening ports
# netstat -an                     # All connections
# nmap -sT target 80              # TCP port scan
# telnet target 443               # Test port

# Layer 7: Application
# curl -v https://example.com     # HTTP test
# dig example.com                 # DNS lookup
# nslookup example.com            # DNS (Windows)
# openssl s_client -connect host:443  # SSL test

@dataclass
class TroubleshootStep:
    layer: int
    check: str
    command: str
    expected: str
    common_issue: str

steps = [
    TroubleshootStep(1, "Link Status", "ip link show eth0", "state UP", "สายหลุด Fiber ขาด"),
    TroubleshootStep(2, "ARP Resolution", "arp -a", "MAC entries exist", "VLAN ผิด STP Block"),
    TroubleshootStep(3, "IP Connectivity", "ping 8.8.8.8", "Reply received", "IP ผิด Route หาย"),
    TroubleshootStep(3, "DNS Resolution", "dig example.com", "A record found", "DNS Server ผิด"),
    TroubleshootStep(4, "Port Open", "ss -tlnp | grep 443", "LISTEN", "Firewall Block Service ไม่รัน"),
    TroubleshootStep(7, "HTTP Response", "curl -I https://example.com", "200 OK", "App Crash Config ผิด"),
    TroubleshootStep(7, "SSL Certificate", "openssl s_client -connect host:443", "Verify OK", "Cert Expired"),
]

print("\n=== Troubleshooting Steps ===")
for s in steps:
    print(f"  [L{s.layer}] {s.check}")
    print(f"    Command: {s.command}")
    print(f"    Expected: {s.expected} | Issue: {s.common_issue}")

OSI vs TCP/IP

# === OSI vs TCP/IP ===

@dataclass
class ModelComparison:
    osi_layers: str
    osi_name: str
    tcpip_layer: str
    tcpip_name: str
    protocols: str

comparison = [
    ModelComparison("7,6,5", "Application+Presentation+Session", "4", "Application", "HTTP DNS FTP SSH SMTP"),
    ModelComparison("4", "Transport", "3", "Transport", "TCP UDP"),
    ModelComparison("3", "Network", "2", "Internet", "IP ICMP ARP OSPF BGP"),
    ModelComparison("2,1", "Data Link+Physical", "1", "Network Access", "Ethernet Wi-Fi PPP"),
]

print("\n=== OSI vs TCP/IP ===")
print(f"  {'OSI Layers':<25} {'TCP/IP Layer':<20} {'Protocols'}")
for c in comparison:
    print(f"  L{c.osi_layers} {c.osi_name:<20} L{c.tcpip_layer} {c.tcpip_name:<15} {c.protocols}")

# Common Protocols by Port
ports = {
    20: ("FTP Data", "TCP"),
    21: ("FTP Control", "TCP"),
    22: ("SSH", "TCP"),
    23: ("Telnet", "TCP"),
    25: ("SMTP", "TCP"),
    53: ("DNS", "TCP/UDP"),
    67: ("DHCP Server", "UDP"),
    80: ("HTTP", "TCP"),
    110: ("POP3", "TCP"),
    143: ("IMAP", "TCP"),
    443: ("HTTPS", "TCP"),
    3306: ("MySQL", "TCP"),
    5432: ("PostgreSQL", "TCP"),
    6379: ("Redis", "TCP"),
    8080: ("HTTP Proxy", "TCP"),
}

print(f"\n\nCommon Ports:")
for port, (service, proto) in ports.items():
    print(f"  Port {port:>5}: {service:<15} ({proto})")

เคล็ดลับ

  • Bottom-up: แก้ปัญหาจาก Layer 1 ขึ้นไป สายก่อน
  • ping: ใช้ ping ทดสอบ Layer 3 เป็นอันดับแรก
  • Port: จำ Port สำคัญ 22 80 443 53 3306
  • Wireshark: ใช้ Wireshark ดู Packet ทุก Layer
  • TCP/IP: เข้าใจ OSI แล้วเข้าใจ TCP/IP ง่ายขึ้น

OSI Model คืออะไร

แบบจำลองเครือข่าย 7 ชั้น ISO 1984 อธิบายการส่งข้อมูล Layer หน้าที่เฉพาะ Troubleshoot แก้ปัญหา ภาษากลาง IT