0

I'm trying to call a function get_ethname into another function get_ethSpeed, but i'm unable to understand how to get it called.

Many thanks for your inputs in advanced.

The output of the first function returns the name of the NIC interface on the system as below..

[root@tss/]# cat intDetail1.py
#!/usr/bin/python
import ethtool

def get_ethname():
    inames = ethtool.get_devices()
    inameCurr = inames[1]
    print inameCurr
    return inameCurr

def main():
    get_ethname()

main()
[root@tss /]# ./intDetail1.py
eth0

Below is the main code where i'm trying to call it.

 #!/usr/bin/python
    import ethtool
    import subprocess

    def get_ethname():
        inames = ethtool.get_devices()
        inameCurr = inames[1]
        print inameCurr
        return inameCurr

    def get_ethSpeed():
        spd = subprocess.popen("['ethtool',  'get_ethname']", stdout=subprocess.PIPE).communicate()[0]
        print spd
        return spd

    def main():
        get_ethname()
        get_ethSpeed()

    main()

When i run the above code it gives the below error .

  File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

My aim is get to main running interface name on the systems and and then get the speed determine of the NIC by using linux system utility ethtool which tells the speed of the Interface:

[root@tss /]# /sbin/ethtool eth0| grep Speed
              Speed: 1000Mb/s

Output look of ethtool eth0 is Below:

[root@tss /]# ethtool eth0
Settings for eth0:
        Supported ports: [ TP ]
        Supported link modes:   10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
                                1000baseT/Full
        Supported pause frame use: No
        Supports auto-negotiation: Yes
        Advertised link modes:  10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
                                1000baseT/Full
        Advertised pause frame use: No
        Advertised auto-negotiation: Yes
        Speed: 1000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 1
        Transceiver: internal
        Auto-negotiation: on
        MDI-X: Unknown
        Supports Wake-on: g
        Wake-on: g
        Link detected: yes
krock1516
  • 441
  • 10
  • 30
  • What are the file names and their tree structure/relation? –  Apr 23 '17 at 11:46
  • 1
    `popen` requires a list of args. You have given it a string instead. Remove the double quotes and it should be fine. Furthermore, `python2.6` is no longer supported. You should move over to `python2.7` or preferably `python3` sooner rather than later. – Robert Seaman Apr 23 '17 at 11:51
  • @DaniSpringer, Sorry i could not get your question exactly, though. I am Just trying to get the `eth0` to be received with `ethtool` command which is linux system command so, saying that the second function needs to fetch the output of `/sbin/ethtool eth0` output. There is not files called in the code. – krock1516 Apr 23 '17 at 11:55
  • @RobertSeaman, I Checked that removing double quotes but it returns error `Settings for get_ethname: ` and `No data available`. – krock1516 Apr 23 '17 at 12:00
  • 1
    Also, `get_ethname` needs to call the function you defined so: `spd = subprocess.popen(['/sbin/ethtool', get_ethname()], stdout=subprocess.PIPE).communicate()[0]` – Robert Seaman Apr 23 '17 at 12:04
  • @RobertSeaman, Still same error. – krock1516 Apr 23 '17 at 12:07
  • Does it show the exact same error? If it says `Settings for get_ethname` then you're not calling the function correctly. Please paste the error. – Robert Seaman Apr 23 '17 at 12:12
  • Error: `Cannot get device settings: No such device Cannot get wake-on-lan settings: No such device Cannot get message level: No such device Cannot get link status: No such device Settings for get_ethname(): No data available` – krock1516 Apr 23 '17 at 12:16
  • If you are calling functions, they are somewhere, no? Anyhow, `no such file or directory` means you are trying to access a file in te wrong place –  Apr 23 '17 at 12:18
  • @DaniSpringer, I have Just updated the post with `ethtool eth0` output, I am calling the function from the correct place, because i'm root admin on the system and i'm able to execute the First function alone while running from the same place but while clubbing it with another function then it gets error, though the system command works fine from the same place. I'm doing something wrong definitely. – krock1516 Apr 23 '17 at 12:23
  • Good luck. I don't know enough to help –  Apr 23 '17 at 12:28
  • @Karn, try copying and pasting what I wrote. The chances are you have quotes around `get_ethname()` therefore it is being passed in as a string, rather than the result of the function. – Robert Seaman Apr 23 '17 at 12:38
  • 1
    @DaniSpringer, nice to have your inputs. – krock1516 Apr 23 '17 at 12:43
  • RobertSeaman, You are absolutely right , that was the case indeed here. Which i later realized when i saw an answer from @cricket_007. appreciate your help though. – krock1516 Apr 23 '17 at 12:46

1 Answers1

1

No such device Settings for get_ethname(): No data available

This is still the same problem with the original question. You're passing a literal string and expecting the shell to invoke the Python function?

There's no quotes here except around the actual shell command

spd = subprocess.Popen(['/sbin/ethtool', get_ethname()], stdout=subprocess.PIPE).communicate()[0]

Or, make another variable

iface = get_ethname()
 # Or 
 iface = ethtool.get_devices()[1]

spd = subprocess.Popen(['/sbin/ethtool', iface], stdout=subprocess.PIPE).communicate()
return spd[0]

Note that you'll still need to grep (or scan the output with python) for "Speed"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • cricket_007..... You correctly caught it, though i removed those literal string already but i missed to remove the quotes around the function which i realized after your answer. Though the real sauce still need to be extarcted :) that `"Speed"` thing. Thanks for your inputs.. Would be great any hist on that. – krock1516 Apr 23 '17 at 12:37
  • You could pipe the subprocess to another subprocess that uses grep, or use regex, or loop over the lines of that output – OneCricketeer Apr 23 '17 at 12:41
  • cricket_007 .. thanks for the hint, also please edit the `popen` into `Popen` which is a typo error in the code you provided, in case someone copy paste that. – krock1516 Apr 23 '17 at 12:49
  • Okay. By the way. http://stackoverflow.com/questions/32712129/run-linux-grep-command-from-python-subprocess – OneCricketeer Apr 23 '17 at 12:57