Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am trying to construct the command which includes the variable containing IP addresses to the subprocess but the variable is not passed.

iplist

8.8.8.8

1.1.1.1

code

with open('iplist', 'r', encoding="utf-8") as f:

    data = f.read()

    print(data)

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

    result = subprocess.run(cmd, stdout=subprocess.PIPE)

    print(result.stdout)

output

8.8.8.8

1.1.1.1

2020/11/01 10:06:13 could not get records from db /usr/local/var/GeoIP/GeoLite2-City.mmdb: $data is not a valid IP address

b''

1 Answer

0 votes
by (36.8k points)

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)

 Want to be a master in Data Science? Enroll in this Data Science Courses

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jan 21, 2021 in Python by ashely (50.2k points)

Browse Categories

...