0

In python if I have interfaces = netifaces.interfaces() which is a list returning: ['lo','eth0','eth1']

how to I loop over that list passing lo and eth0, eth1 into several subprocess calls to ethtool?

subprocess.call(['ethtool %'] interfaces) --- lo
subprocess.call(['ethtool %'] interfaces) --- eth0
subprocess.call(['ethtool %'] interfaces) --- eth1

I want to run ethtool for the number of items in my interfaces list but for each pass I want to substitute the next item in the interfaces list and pass that to the subprocess.call.

3
  • Any reason why you can't just put this in a for loop, as your question title states? Commented Mar 16, 2017 at 5:16
  • yes. I want to put it in a for loop but how to I pass in each item of the interfaces list on each pass? for interface in interfaces: subprocess.call(['ethtool %'] ????) Commented Mar 16, 2017 at 5:17
  • I see. I've added an answer. The list you are passing to call is a list of arguments. So just provide interface as the second item in the list. Commented Mar 16, 2017 at 5:22

2 Answers 2

2

As far as I can tell, you just want to do a normal for loop...

interfaces = netifaces.interfaces()
for interface in interfaces:
    subprocess.call(["ethtool", interface])
Sign up to request clarification or add additional context in comments.

Comments

0
for interface in interfaces:
    print("[+] Fetching LLDP info for ", interface)
    subprocess.call(['lldptool', '-t', '-n', '-i', interface])
    print("[+] Printing ethtool info for ", interface)
    subprocess.call(['ethtool', interface])

I turns out I was passing in the interface outside of the subprocess call and once I put it inside the [] it worked as expected. I love this site! Thank you shadow

1 Comment

Welcome to Stack Overflow. The convention here is to upvote (click the up arrow) and/or accept (click the check mark so it turns green) the answer(s) which helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.