Skip to content Skip to sidebar Skip to footer

Pass Variable To Subprocess

I am trying to construct a command which includes a a variable containing IP addresses to a subprocess but the variable is not passed. iplist 8.8.8.8 1.1.1.1 code with open('iplis

Solution 1:

Something like

with open('iplist', 'r', encoding="utf-8") as ip_file:
    for ip in ip_file:
      cmd = ['./mmdbinspect', '--db', '/usr/local/var/GeoIP/GeoLite2-City.mmdb', ip.strip()]
      result = subprocess.run(cmd, stdout=subprocess.PIPE)
      print(result.stdout)

Solution 2:

Use: cmd = ['./mmdbinspect', '--db', '/usr/local/var/GeoIP/GeoLite2-City.mmdb', data]

data is a variable name.

$data is used on other languages (like PHP).

Solution 3:

CYREX pointed me in right direction. Thank you.

Final code below

withopen('iplist', 'r', encoding="utf-8") as f:
    data = f.readlines()
    print(data)
    for line in data:
        ip = line.strip() # strip \n lookup will fail
        cmd = ['./mmdbinspect', '--db', '/usr/local/var/GeoIP/GeoLite2-City.mmdb', ip]
        print(cmd)
        result = subprocess.run(cmd, stdout=subprocess.PIPE)
        print(result.stdout)

Post a Comment for "Pass Variable To Subprocess"