Home > Net >  How to get the value of an item from the string?
How to get the value of an item from the string?

Time:11-20

I am trying to find the value of the Signal level only because I would like to measure the wifi signal by dBm.

I am using this command to see the Signal level (I'd like to get it using python):

$ iw dev wlx88366cfd0226 link

Connected to c4:41:1e:e5:86:f6 (on wlx88366cfd0226)
    SSID: Hlab_linksys
    freq: 5785
    signal: -22 dBm
    tx bitrate: 867.0 MBit/s

The full code is:

import subprocess
import time
import argparse

parser = argparse.ArgumentParser(description='Display WLAN signal strength.')
parser.add_argument(dest='interface', nargs='?', default='wlx88366cfd0226',
                    help='wlan interface (default: wlx88366cfd0226)')
args = parser.parse_args()

print '\n---Press CTRL Z or CTRL C to stop.---\n'

while True:
    cmd = subprocess.Popen('iwconfig %s' % args.interface, shell=True,
                           stdout=subprocess.PIPE)
    # print "cmd.stdout",cmd.stdout
    for line in cmd.stdout:
        if 'Link Quality' in line:
            # print"s"
            print (line.lstrip(' ')),
        elif 'Not-Associated' in line:
            print 'No signal'
    time.sleep(1)

The type of the output is string data stream as:

Link Quality=85/100  Signal level=78/100  Noise level=0/100

I'd like to get 78/100 only in order to convert to dBm.

The original code is from here

In my case, when I use this command see here: iwconfig

wlx88366cfd0226  IEEE 802.11AC  ESSID:"Hlab_linksys"  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency:5.785 GHz  Access Point: C4:41:1E:E5:86:F6   
          Bit Rate:867 Mb/s   Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality=84/100  Signal level=78/100  Noise level=0/100
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

enp3s0    no wireless extensions.

lo        no wireless extensions.

So, I used wlx88366cfd0226 in the code.

UPDATEED

How to get the value of the signal only:

This is my code:

import subprocess
import time
import argparse

parser = argparse.ArgumentParser(description='Display WLAN signal strength.')
parser.add_argument(dest='interface', nargs='?', default='wlx88366cfd0226',
                    help='wlan interface (default: wlx88366cfd0226)')
args = parser.parse_args()

print '\n---Press CTRL Z or CTRL C to stop.---\n'

while True:
    cmd = subprocess.Popen('iw dev %s link' % args.interface, shell=True,
                           stdout=subprocess.PIPE)
    # print "cmd.stdout",cmd.stdout
    for line in cmd.stdout:
        if 'signal' in line:
            # print"s"
            print (line.lstrip(' ')),
        elif 'Not-Associated' in line:
            print 'No signal'
    time.sleep(1)

The output here is:

    signal: -23 dBm
    signal: -23 dBm
    signal: -23 dBm
    signal: -23 dBm
    signal: -23 dBm
    signal: -23 dBm
    signal: -23 dBm
    signal: -23 dBm

I need it as a number to use in the next equations.

CodePudding user response:

the split way:

line.split("  ")[1].split('=')[1]

reading it: we take the line. split it by 2 spaces. that gives us the 3 sections ("Link quality", "signal level" and "Noise level") we then access the second element with the first [1] which gives us "Signal level=78/100". We then take that and spit it by = which gives us ['Signal level', '78/100'] and we finally access the second element with the second [1] and we get what we want.

Or if string manipulation is more your flavour:

level = line[line.find("Signal level=") 13:line.find(" ", line.find("Signal level=") 13)]

reading it: we take the line we know has the value we want and we find Signal level= in that string. find returns the index of the first character that matches in the string so we have to add 13 (the count of chars in Signal level=) to get it to then end of Signal level=. then we have to find the space after what you're looking for. we do that by looking for the first character after the Signal level= 13.

  • Related