Using Python subprocess.call to call bash script and agruments unsucessfully
Question: I'm working on a project for managing blocks of IP addresses in a lab
environment where we have a limited number of address available. For example
users are allowed to create virtual machines in the lab as needed but should
assign an IP of say an available range of 50.

The program will empower users to choose available IP's by pinging the range
sequentially and giving output of what IP address respond.

I'm stuck trying to find a way for ping to respond with a 1 or 0. I might be
going about it all wrong. I've written a bash shell script which run fine on
its own to return 1 or 0. I'm now attempting to use call from the subprocess
module in python to call the script but i never get to the second argument. In
addition the second arg when converted to a string give me syntax errors.

    
    
    ###Shell script###
    
    #!/bin/bash
    
    if ping -c 1 $1 >/dev/null; then
    echo "1"
    else
    echo "0"
    fi
    
    ###Python code calling ./ping.sh###
    import subprocess
    host = str(input('Host Please:   '))
    output = subprocess.call(['./ping.sh', host], shell=True)
    
    ###Output of python code###
    
    jon@WartHead ~/Desktop/Python/Python-3.3.0 $ python ip.py 
    Host Please:   127.0.0.1
    Traceback (most recent call last):
      File "ip.py", line 2, in <module>
        host = str(input('Host Please:   '))
      File "<string>", line 1
        127.0.0.1
              ^
    SyntaxError: invalid syntax
    

This is one of my first python scripts written, any help is much welcomed.

Jon


Answer: I would do something like this;

    
    
    import re,commands
    
    
    class CheckAlive:
        def __init__(self,firstIP,lastIP):
            for checkIP in range(firstIP,lastIP):
                myCommand = commands.getstatusoutput('ping -c ' + '127.0.0.' + str(checkIP))
                searchString = r'ping: unknown host'
    
                match = re.search(searchString,str(myCommand))
    
                if match:
                    # these hosts are not alive
                    print '127.0.0.' + str(checkIP) + " host is not up."
                else:
                    # these hosts are alive
                    print '127.0.0.' + str(checkIP) + " host is up."
    
    CheckAlive(0,254) # checks all hosts from 127.0.0.0 to 127.0.0.254
    

