Setting Permissions and Testing Scripts
With both scripts created, the next step is to make them executable and test them. In Linux, a script needs execute permissions to be run directly from the terminal.
First, check the current permissions of the files.
ls -l /root
The output shows the permissions in the first column. The -rw-r--r-- indicates that the files are readable and writable but not executable.
-rw-r--r-- 1 root root 150 Oct 20 10:22 log_parser.py
-rw-r--r-- 1 root root 85 Oct 20 10:15 nmap_scan.sh
-rw-r--r-- 1 root root 112 Oct 20 10:20 sample.log
Use the chmod command with the +x flag to add execute permissions to both scripts.
chmod +x /root/nmap_scan.sh
chmod +x /root/log_parser.py
Now, check the permissions again.
ls -l /root
The x in the permissions string (-rwxr-xr-x) confirms that the files are now executable.
-rwxr-xr-x 1 root root 150 Oct 20 10:22 log_parser.py
-rwxr-xr-x 1 root root 85 Oct 20 10:15 nmap_scan.sh
-rw-r--r-- 1 root root 112 Oct 20 10:20 sample.log
With permissions set, test the Bash script by running it.
/root/nmap_scan.sh
The script will execute the Nmap scan and print the results, which should look similar to this:
Starting Nmap scan on localhost...
Starting Nmap 7.94 ( https://nmap.org ) at 2023-10-20 10:30 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up, received user-set (0.00010s latency).
Other addresses for localhost (not scanned): ::1
rDNS record for 127.0.0.1: localhost
Not shown: 999 closed tcp ports (conn-refused)
PORT STATE SERVICE
...
Nmap done: 1 IP address (1 host up) scanned in 0.15 seconds
Scan completed.
Next, test the Python script.
/root/log_parser.py
The script will parse the log file and print only the line containing "ERROR".
Starting log parsing...
2023-10-20 10:01:00 ERROR Connection failed
Log parsing completed.
Both scripts are now working correctly. The final step is to schedule them to run automatically.