LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2005, 03:18 PM   #1
vdemuth
Member
 
Registered: Oct 2003
Location: West Midlands, UK
Distribution: Slackware 14 (Server),OpenSuse 13.2 (Laptop & Desktop),, OpenSuse 13.2 on the wifes lappy
Posts: 781

Rep: Reputation: 98
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?
 
Old 09-21-2005, 04:02 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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? )

Last edited by PTrenholme; 09-21-2005 at 04:05 PM.
 
Old 09-21-2005, 04:06 PM   #3
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Rep: Reputation: 15
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.
 
Old 09-22-2005, 01:31 AM   #4
vdemuth
Member
 
Registered: Oct 2003
Location: West Midlands, UK
Distribution: Slackware 14 (Server),OpenSuse 13.2 (Laptop & Desktop),, OpenSuse 13.2 on the wifes lappy
Posts: 781

Original Poster
Rep: Reputation: 98
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.
 
Old 09-22-2005, 02:05 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
On which one(s) of these 5 steps do you need help ?

Have you coded something yet ?
 
Old 09-22-2005, 04:15 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it's almost too easy
 
Old 09-22-2005, 05:35 AM   #7
vdemuth
Member
 
Registered: Oct 2003
Location: West Midlands, UK
Distribution: Slackware 14 (Server),OpenSuse 13.2 (Laptop & Desktop),, OpenSuse 13.2 on the wifes lappy
Posts: 781

Original Poster
Rep: Reputation: 98
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
 
Old 09-22-2005, 05:40 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
sorry chum.
was only joking, hence the

what does iwlist output look like?
don't use wireless.
post a bit.
 
Old 09-22-2005, 02:01 PM   #9
vdemuth
Member
 
Registered: Oct 2003
Location: West Midlands, UK
Distribution: Slackware 14 (Server),OpenSuse 13.2 (Laptop & Desktop),, OpenSuse 13.2 on the wifes lappy
Posts: 781

Original Poster
Rep: Reputation: 98
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 keyn
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 keyn
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 keyff
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 keyn 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
 
Old 09-22-2005, 03:08 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 09-23-2005, 04:09 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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";
 
Old 09-23-2005, 04:31 PM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 09-24-2005, 02:54 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 09-24-2005, 07:58 PM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
i said it was messy

It's not the end product....
but it's engineered ready for enhancement!
 
Old 09-25-2005, 02:16 PM   #15
vdemuth
Member
 
Registered: Oct 2003
Location: West Midlands, UK
Distribution: Slackware 14 (Server),OpenSuse 13.2 (Laptop & Desktop),, OpenSuse 13.2 on the wifes lappy
Posts: 781

Original Poster
Rep: Reputation: 98
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 09:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 03:22 PM
question about grepping something Grafbak Programming 6 03-01-2005 01:28 PM
grepping last word of output rajatgarg Programming 3 11-25-2003 10:41 AM
Not grepping emails right mikeyt_333 Linux - General 11 04-13-2003 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration