concept and initial code

This commit is contained in:
Radek
2025-03-06 12:53:48 +00:00
parent 83fd56c750
commit c06e385bc9
3 changed files with 102 additions and 2 deletions

23
Dockerfile Normal file
View File

@@ -0,0 +1,23 @@
# Use a minimal base image
FROM alpine:latest
# Install necessary packages
RUN apk add --no-cache snmpd python3 py3-pip
# Install any additional Python packages if needed
RUN pip3 install --no-cache-dir random
# Copy the custom script into the container
COPY custom_snmp_script.py /usr/local/bin/custom_snmp_script.py
# Make the script executable
RUN chmod +x /usr/local/bin/custom_snmp_script.py
# Configure snmpd to use the custom script
RUN echo "extend .1.3.6.1.4.1.38446.1.2.4.1.9.1 /usr/local/bin/custom_snmp_script.py" >> /etc/snmp/snmpd.conf
# Expose the SNMP port
EXPOSE 161/udp
# Start snmpd
CMD ["snmpd", "-f"]

View File

@@ -1,3 +1,64 @@
# OIDsimulator # SNMPD Custom OID Docker Container
This Docker container runs `snmpd` with a custom script that provides a specific OID (`.1.3.6.1.4.1.38446.1.2.4.1.9.1`). The value of this OID increases randomly by a small amount every 4 minutes.
## Features
- Lightweight Docker container based on Alpine Linux.
- Custom Python script to generate and update the OID value.
- Configurable SNMP daemon (`snmpd`) to use the custom script.
## Requirements
- Docker installed on your system.
- Basic knowledge of Docker commands.
## Dependencies
- `snmpd`: The SNMP daemon.
- Python 3: Used to write the custom script.
- `random`: Python library for generating random numbers.
## How to Use
### Step 1: Build the Docker Image
Navigate to the directory containing the `Dockerfile` and `custom_snmp_script.py`, then run:
```bash
docker build -t snmpd-custom .
```
### Step 2: Run the Docker Container
Start the Docker container with the following command:
```bash
docker run -d -p 161:161/udp --name snmpd-custom-container snmpd-custom
```
### Step 3: Verify the Setup
You can verify that the container is running and the SNMP daemon is providing the custom OID by using an SNMP client to query the OID:
```bash
snmpwalk -v 2c -c public localhost .1.3.6.1.4.1.38446.1.2.4.1.9.1
```
You should see the value incrementing randomly every 4 minutes.
## Customization
- **Initial Value**: You can change the initial value in the `custom_snmp_script.py` file.
- **Increment Range**: Modify the range in the script to change how much the value increments.
- **Update Interval**: Adjust the sleep interval in the script to change the frequency of updates.
## Notes
- Ensure that the SNMP community string (`public` in the example) matches your configuration.
- The container exposes port 161 for SNMP communication.
## License
This project is licensed under the MIT License.
Simulate SNMP response on a specific oid using a script in a Docker container.

16
custom_snmp_script.py Normal file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env python3
import random
import time
# Initial value
value = 1385062
while True:
# Increment the value by a random amount between 0 and 6
value += random.randint(0, 6)
# Output the value in the required format
print(f"INTEGER: {value}")
# Wait for 4 minutes
time.sleep(240)