LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grepping unknown text, is there a way? (https://www.linuxquestions.org/questions/programming-9/grepping-unknown-text-is-there-a-way-365684/)

vdemuth 09-21-2005 03:18 PM

grepping unknown text, is there a way?
 
As the title says, I need to grep some unknown text in order to pass that text as an argument to another app.
The text I need to grep will appear in a text file as a result of interrogating app1, and directing the output to a text file, where I would like it to be automatically extracted and passed to app2.
Now I know that I could then read the text file produced, and manually pass the info I need to app2, but I can imagine a case where it ought to be possible to do that automatically.
So, is there any way of doing this, or would some other way need to be found?

PTrenholme 09-21-2005 04:02 PM

Generally, pipes can be concatenated. So something like
Code:

$ app1 | grep -e '<whatever>' | app2
should work with no problem. (Make sure app1 outputs to stdio, not [or in addition to] a file.)

(Is this really what you asked? Or did I miss something? :scratch: )

JCipriani 09-21-2005 04:06 PM

By the way, that takes everything written by stdout from app1 and pipes it to grep's stdin, and then takes grep's stdout and pipes it to app2's stdin. So if app2 accepts input on stdin then that works fine. There are also other ways, for example:

Code:

app2 -someoption `app1 | grep blah`
That will take the output of app1, grep for blah, and pass the output of grep as command line arguments to app2 just after the -someoption command line param.

vdemuth 09-22-2005 01:31 AM

Maybe a better explanation as to what I am trying to achieve. It all revolves around not being able to find any sort of working utility that automatically searches for, then logs me into a wireless hot spot. I have tried the kde wireless utility, it just doesn't work automatically, Network Manager is just to unstable, and has to many dependencies, and wap_supplicant seems to be a case of using a large hammer to crack a peanut, so I am looking for something much simpler by way of a shell script.

I see the problem like this:-

1) Scan for a wireless AP using iwlist wlano scan >wirelessscanresults.txt
2) Extract the essid (if any) from the .txt file and pass it as a parameter to iwconfig, i.e. iwconfig wlan0 essid "extracted text"
3) Check in the .txt file if the AP needs a key, or not.
4) If it does, then prompt for the key, and pass to iwconfig as an argument. If not, then bring up the interface with ifconfig
5) If no scan results are found at all, then print some sort of message to screen informing the user, i.e. me, that no wireless network is available.


So you can now see the problem, to automate the process, the results of the scan, which will be unknown, and variable need to be extracted and used by iwconfig to set up the wireless network card.

Any more suggestions, as I don't think what you have currently proposed is exactly what I am looking for.

TIA.

jlliagre 09-22-2005 02:05 AM

On which one(s) of these 5 steps do you need help ?

Have you coded something yet ?

bigearsbilly 09-22-2005 04:15 AM

it's almost too easy ;)

vdemuth 09-22-2005 05:35 AM

bigearsbilly, or should that be bigheadbilly
It may be almost to simple for you, and if it is, how about some help here. I have done absolutely zero scripting with Linux to date, so this will be my first attempt at anything of this type, so a little latitude, and less attitude would be helpfull.

jlliagre, no I haven't coded anything yet. I have a fairly good idea how I want to start for myself, and then hopefully expand on it later, but for now I need to find out how to strip out the essid and key info in step 2.

Thanks for your help

bigearsbilly 09-22-2005 05:40 AM

sorry chum.
was only joking, hence the ;)

what does iwlist output look like?
don't use wireless.
post a bit.

vdemuth 09-22-2005 02:01 PM

Hi bigearsbilly,
And my apologies for jumping down your throat, just having a bad day I guess.
Anyway, below is the output from iwlist:-

wlan0 Scan completed :
Cell 01 - Address: 00:30:BD:FB:E4:70
ESSID:"adanddad"
Protocol:IEEE 802.11b
Mode:Managed
Frequency:2.462 GHz (Channel 11)
Quality:0/100 Signal level:-45 dBm Noise level:-256 dBm
Encryption key:on
Bit Rate:1 Mb/s
Bit Rate:2 Mb/s
Bit Rate:5.5 Mb/s
Bit Rate:11 Mb/s
Bit Rate:18 Mb/s
Bit Rate:24 Mb/s
Bit Rate:36 Mb/s
Bit Rate:54 Mb/s
Bit Rate:6 Mb/s
Bit Rate:9 Mb/s
Bit Rate:12 Mb/s
Bit Rate:48 Mb/s
Extra:bcn_int=100
Extra:atim=0

Cell 02 - Address: 03:00:B2:FF:24:BB
ESSID:"TimsOffice"
Protocol:IEEE 802.11b
Mode:Managed
Frequency:2.462 GHz (Channel 6)
Quality:0/100 Signal level:-65 dBm Noise level:-184 dBm
Encryption key:on
Bit Rate:1 Mb/s
Bit Rate:2 Mb/s
Bit Rate:5.5 Mb/s
Bit Rate:11 Mb/s
Bit Rate:18 Mb/s
Bit Rate:24 Mb/s
Bit Rate:36 Mb/s
Bit Rate:54 Mb/s
Bit Rate:6 Mb/s
Bit Rate:9 Mb/s
Bit Rate:12 Mb/s
Bit Rate:48 Mb/s
Extra:bcn_int=100
Extra:atim=0

Cell 01 - Address: D3:0D:11:30:01:6F
ESSID:"YouNeedSecurity"
Protocol:IEEE 802.11b
Mode:Managed
Frequency:2.462 GHz (Channel 9)
Quality:0/100 Signal level:-72 dBm Noise level:-142 dBm
Encryption key:off
Bit Rate:1 Mb/s
Bit Rate:2 Mb/s
Bit Rate:5.5 Mb/s
Bit Rate:11 Mb/s
Bit Rate:18 Mb/s
Bit Rate:24 Mb/s
Bit Rate:36 Mb/s
Bit Rate:54 Mb/s
Bit Rate:6 Mb/s
Bit Rate:9 Mb/s
Bit Rate:12 Mb/s
Bit Rate:48 Mb/s
Extra:bcn_int=100
Extra:atim=0

As you can see, I am scanning three wireless APs, two are encrypted, as shown by the Encryption key:on line, whilst the third is not. Now I can of course send this output to a text file, then read that file to find out the names of the scannes APs, and whether they are encrypted or not, then use that as a modifier to the iwconfig command. What I would lke to do of course, is to automate that process. The problem is, as I move around the country a lot, the ESSID will be different whenever I am lucky enough to find wireless points, and manually setting up my wireless card gets a bit tiring. In fact, it sometimes tempts me to use Micro$haft XP as it can do what I want. So I reckon it must be possible under my favourite OS as well, I just don't know how to go about extracting the info I need, when I don't know the info in the first place!
So, any suggestions?

TIA

jlliagre 09-22-2005 03:08 PM

This looks like something that can easily be solved with awk.

The iwlist output is made of clearly identifiable records with a colon delimiter, just extract the ESSID, encryption key status from each one, and you are almost done.

bigearsbilly 09-23-2005 04:09 AM

I'd hate to see a man forced to use xp.
( except to swap characters in vi )

here's a very messy perl script as a start.
it produces the following so far:

Code:

$ wl.pl < 1

network "TimsOffice" has encryption on
network "adanddad" has encryption on
network "YouNeedSecurity" has encryption off
$


Code:

#!/usr/bin/perl -w

use strict;

my %sid_hash = ();

my (        $ESSID,
        $dummy,
        $key,
        $on_or_off,
  );


while (<STDIN>) {

        chomp;
        next if /^Bit Rate/;
        if (/^ESSID/) {

          ($dummy, $ESSID) = split /:/;
          $sid_hash{$ESSID} = $ESSID;
        }
        if ( /^Encryption/ ) {
          defined ($sid_hash{$ESSID}) or die "Hmm! dodgy file format?";
          $sid_hash{$ESSID} .= ":$_";
        }
}


foreach $key ( keys %sid_hash ) {
    ($ESSID, $dummy, $on_or_off) = split /:/,$sid_hash{$key};
    print "\nnetwork $ESSID has encryption $on_or_off";
}
print "\n";


bigearsbilly 09-23-2005 04:31 PM

Here's a messy graphical start:
relies on you doing
Quote:

1) Scan for a wireless AP using iwlist wlano scan >wirelessscanresults.txt
i've hard-coded the data file for the moment.
assuming you have tcl/tk using the data in earlier post

Code:


#!/bin/sh
#\
exec wish "$0" "$@"

set fp [ open "| /usr/bin/cat ./wirelessscanresults.txt" ]
# set fp [ open "| /sbin/iwlist wlano scan" ]  ###  try uncommenting this if it works
gets $fp ; # skip first line

set f1  [frame .f1 -borderwidth 3]
set f2  [frame .f2 -borderwidth 3]
set f2  [frame .f3 -borderwidth 3]

set scroll .f1.scroll
set cells .f1.cells

set quit  [button .f2.quit -command exit -text "Quit"]
set cells[listbox .f1.cells -yscrollcommand "$scroll set"]
set scroll  [scrollbar $scroll -command ".f1.cells yview" ]
$cells configure -yscrollcommand "$scroll set"
bind $cells <ButtonRelease-1> { list_network }

pack  .f1 .f3 .f2 -side top
pack  $cells $scroll -side left -fill y
pack  $quit -side bottom

set count -1

proc list_network {} {
    upvar networks networks
        set index [selection get]
set count 0
    if {[info exists networks($index)] < 0} {return}
          destroy .f3.sub
          pack [frame .f3.sub]

  #pack [ label .f3.win ] -side left
        foreach thing $networks($index) {
          puts $thing
          pack [ label .f3.sub.$count -text $thing] -side top
                incr count
        }
}

while { [gets $fp line] >= 0 } {

        if { [string match *Cell* $line ] } {
                set count $line
                set L [split $line ]
                $cells insert end $line
                set networks($count)[list $line]

                puts -nonewline "\n$count)";
        } else {
                lappend networks($count) $line
        }

}

$cells selection set 0 0
foreach nm [array names networks] {
        puts "$nm = $networks($nm)"
}
list_network

./wirelessscanresults.txt
Code:

wlan0 Scan completed :
Cell 01 - Address: 00:30:BD:FB:E4:70
ESSID:"adanddad"
Protocol:IEEE 802.11b
Mode:Managed
Frequency:2.462 GHz (Channel 11)
Quality:0/100 Signal level:-45 dBm Noise level:-256 dBm
Encryption key:on
Bit Rate:1 Mb/s
Bit Rate:2 Mb/s
Bit Rate:5.5 Mb/s
Bit Rate:11 Mb/s
Bit Rate:18 Mb/s
Bit Rate:24 Mb/s
Bit Rate:36 Mb/s
Bit Rate:54 Mb/s
Bit Rate:6 Mb/s
Bit Rate:9 Mb/s
Bit Rate:12 Mb/s
Bit Rate:48 Mb/s
Extra:bcn_int=100
Extra:atim=0

Cell 02 - Address: 03:00:B2:FF:24:BB
ESSID:"TimsOffice"
Protocol:IEEE 802.11b
Mode:Managed
Frequency:2.462 GHz (Channel 6)
Quality:0/100 Signal level:-65 dBm Noise level:-184 dBm
Encryption key:on
Bit Rate:1 Mb/s
Bit Rate:2 Mb/s
Bit Rate:5.5 Mb/s
Bit Rate:11 Mb/s
Bit Rate:18 Mb/s
Bit Rate:24 Mb/s
Bit Rate:36 Mb/s
Bit Rate:54 Mb/s
Bit Rate:6 Mb/s
Bit Rate:9 Mb/s
Bit Rate:12 Mb/s
Bit Rate:48 Mb/s
Extra:bcn_int=100
Extra:atim=0

Cell 01 - Address: D3:0D:11:30:01:6F
ESSID:"YouNeedSecurity"
Protocol:IEEE 802.11b
Mode:Managed
Frequency:2.462 GHz (Channel 9)
Quality:0/100 Signal level:-72 dBm Noise level:-142 dBm
Encryption key:off
Bit Rate:1 Mb/s
Bit Rate:2 Mb/s
Bit Rate:5.5 Mb/s
Bit Rate:11 Mb/s
Bit Rate:18 Mb/s
Bit Rate:24 Mb/s
Bit Rate:36 Mb/s
Bit Rate:54 Mb/s
Bit Rate:6 Mb/s
Bit Rate:9 Mb/s
Bit Rate:12 Mb/s
Bit Rate:48 Mb/s
Extra:bcn_int=100
Extra:atim=0


jlliagre 09-24-2005 02:54 AM

I got the same output as bigearsbilly with this awk script:
Code:

iwlist | awk -F: '
/ESSID/ {
        essid=$2
}
/Encryption key/ {
        printf("network %s has encryption %s\n", essid, $2);
}
'

---
Perl isn't your friend ;)

bigearsbilly 09-24-2005 07:58 PM

i said it was messy ;)

It's not the end product....
but it's engineered ready for enhancement!

vdemuth 09-25-2005 02:16 PM

Well it looks like I'm gonna have to go and learn AWK and/or Perl, (or maybe both), so I'll be back here when I've done that. Don't know how long thats gonna take, and at my age it could be a while. Thanks for all your input so far both, but it looks a bit out of my knowledge stream at the moment. One day maybe!


All times are GMT -5. The time now is 04:52 AM.