0

I Have A Simple bash script

check_repo() {
    if cat /etc/apt/sources.list | grep "deb https://http.kali.org/kali kali-rolling main contrib non-free" || cat /etc/apt/sources.list | grep "deb https://http.kali.org/kali kali-rolling main non-free contrib"
    then
        return 0
    else
        return 1
    fi
}

check_repo

this script checks sources.list file if a particular repo is there or not if it is then it returns 0 & if it is not then it returns 1 but when i call the script using python3 by check = os.system("bash myscript.sh") but when i print the variable "check" it prints 256

Why so output is given & how can i fix it?

  • OS: Kali Linux
  • Kernel: x86_64 Linux 5.9.0-kali4-amd64
  • Shell: bash 5.1.0

Thanks for the answer in advance

Aditya
  • 1,132
  • 1
  • 9
  • 28
  • 2
    the return value from `os.system()` is not the exit value of the process. see https://docs.python.org/3/library/os.html#os.system – pynexj Dec 23 '20 at 07:56
  • If you are using Kali for anything other than penetration testing, you are doing something wrong. – tripleee Dec 23 '20 at 08:10
  • @tripleee hmm, i know but i think i don't need to install any other os just for programming? – Aditya Dec 23 '20 at 08:27

2 Answers2

3

This is what os.system() returns; on Unix-like systems, the high 8 bits are the result code and the lower 8 are the signal (0 if no signal).

The proper way to do this in Python would be

check = False
with open("/etc/apt/sources.list") as repo:
    for line in repo:
        if "deb https://http.kali.org/kali kali-rolling main contrib non-free" \
            in line or "deb https://http.kali.org/kali kali-rolling main non-free contrib" \
                in line:
           check = True
           break

The proper way to do this in a shell script would be

if grep -q -e "deb https://http\.kali\.org/kali kali-rolling main contrib non-free" -e "deb https://http\.kali\.org/kali kali-rolling main non-free contrib" /etc/apt/sources.list
then ...

(or refactor to use grep -E with a single regex to cover both expressions; see below for an attempt).

If you want to put this in a function, the exit status from grep will be 0 or 1 so there is no need to separately return anything other than that.

The proper way to call this from a Python script would be

import subprocess

check = subprocess.check_call(['grep', '-q', '-E',
        r'deb https://http\.kali\.org/kali kali-rolling main( contrib| non-free){2}',
        '/etc/apt/sources.list'])
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The code is still broken in that it should also examine any files in `/etc/apt/sources.d/*.list` but ignore any lines which are commented out. It should also permit variations in whitespace. Using a simple parser for this format would be better than individually attempting to address each of these bugs; ideally, find a library which correctly provides this functionality. – tripleee Dec 23 '20 at 13:17
1

On Unix, waitstatus_to_exitcode() can be used to convert the result (exit status) into an exit code. On Windows, the result is directly the exit code. Here: https://docs.python.org/3/library/os.html#os.system

s.b.
  • 96
  • 1