Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Python application test with unittest

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with unittest
run: |
python -m unittest discover
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Environments
.env
.venv
env/
venv/
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Network Scanner

This script, `testping1.py`, is a simple network scanner that checks for reachable devices within a specified IP address range. It uses the `ping` command to determine if a device is online.

## Features

- Pings a range of IP addresses.
- Displays reachable IP addresses.
- Shows a progress bar during the scan.

## Requirements

- Python 3
- `tqdm` library

## Installation

1. Clone the repository:
```bash
git clone <repository-url>
cd <repository-directory>
```

2. Install the required Python library:
```bash
pip install -r requirements.txt
```
*(Note: A `requirements.txt` file will be added to the project)*

## Usage

Run the script from your terminal:

```bash
python testping1.py
```

The script is pre-configured to scan the `192.168.43.1` to `192.168.43.254` IP range. You can modify the `start_ip` and `end_ip` variables in `testping1.py` to scan a different range.

**Important:** Ensure you have permission to scan the network before running this script. Unauthorized network scanning can be disruptive and may be against the terms of service for the network you are on.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tqdm
43 changes: 43 additions & 0 deletions test_testping1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest
import subprocess
from unittest.mock import patch, MagicMock
from testping1 import is_reachable

class TestIsReachable(unittest.TestCase):

@patch('testping1.subprocess.Popen')
def test_is_reachable_success(self, mock_popen):
"""Test is_reachable returns True for a successful ping."""
mock_process = MagicMock()
# Simulate a successful ping response containing "bytes from"
mock_process.communicate.return_value = (b'64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.063 ms', b'')
mock_popen.return_value = mock_process

self.assertTrue(is_reachable('127.0.0.1'))

@patch('testping1.subprocess.Popen')
def test_is_reachable_failure(self, mock_popen):
"""Test is_reachable returns False for a failed ping."""
mock_process = MagicMock()
# Simulate a failed ping response
mock_process.communicate.return_value = (b'Request timed out.', b'')
mock_popen.return_value = mock_process

self.assertFalse(is_reachable('10.0.0.1'))

@patch('testping1.subprocess.Popen')
def test_is_reachable_calls_ping_correctly(self, mock_popen):
"""Test is_reachable calls the ping command with correct arguments."""
mock_process = MagicMock()
mock_process.communicate.return_value = (b'', b'')
mock_popen.return_value = mock_process

is_reachable('192.168.1.1', timeout=5)
# Verify that subprocess.Popen was called with the correct arguments, including the timeout
mock_popen.assert_called_once_with(
['ping', '-c', '1', '-W', '5', '192.168.1.1'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)

if __name__ == '__main__':
unittest.main()