Storing received value from send, spawned telnet session


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing received value from send, spawned telnet session
# 1  
Old 02-13-2014
Storing received value from send, spawned telnet session

Hi Guys,

I'm completely new to bash and trying to write a script to spawn a telnet session to retrieve the RSSI value of my device and log the average value of the RSSI over 20 samples. I know that my command does return the RSSI value successfully but my bash scripting is letting me down.
I'm attempting to send the command, store the returned value over 20 samples and then averaging it - storing it in a log file.

Any help would be greatly appreciated.

Here's what i've got so far

Code:
#!/usr/bin/expect
set ip [lindex $argv 0]
spawn telnet $ip
exec > "$2"
sleep 2
for h in {0..20}
    do
    echo "Running test $h"
    send "iwconfig wlan0 | grep -o \"Signal level=....\" | cut -c14-16"

    set val $expect_out(buffer)

    echo "RSSI: $val"

    result +=val
done
result = result/20
echo "Average RSSI &result"


Last edited by vbe; 02-13-2014 at 10:11 AM.. Reason: code tags not icode!
# 2  
Old 02-13-2014
Try this, uncomment #log_user 0 to hide screen I/O

Code:
#!/usr/bin/expect -f
set timeout 8
#log_user 0
set ip [lindex $argv 0]
spawn -noecho telnet $ip
sleep 2
set count 0
set result 0
send "PS1='PROMPT# '\r"
expect -re "PROMPT# '(.*)PROMPT# "
while {$count < 20} {
    send "./iwconfig wanl | grep -o \"Signal level=....\" | cut -c14-16\r"
    expect -re "\r\n(.*)\r\n(.*)PROMPT# "
    set val $expect_out(1,string)
    set result [expr $result+$val ]
    set count [expr $count+1 ]
}
send "exit\r"
expect eof
set result [expr $result/20 ]
puts "Average RSSI $result"

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 02-17-2014
Thank you for getting back to me, i changed the 'send' line back to

Quote:
send "iwconfig wlan0 | grep -o \"Signal level=....\" | cut -c14-16\r"
and it now works perfectlySmilie

---------- Post updated at 07:38 AM ---------- Previous update was at 04:59 AM ----------

Additional question - I have to convert the numbers as the value of the RSSI is in dB, how do i print out the values rather than just the formula:

Code:
#!/usr/bin/expect -f

set timeout 8
log_user 0
set ip [lindex $argv 0]
spawn -noecho telnet $ip
sleep 2

set count 0
set result 0
set value 0
set resultDb 0

send "PS1='PROMPT# '\r"

expect -re "PROMPT# '(.*)PROMPT# "
while {$count < 20} {
send "iwconfig wlan0 | grep -o \"Signal level=....\" | cut -c14-16\r"
expect -re "\r\n(.*)\r\n(.*)PROMPT# "
set valdB $expect_out(1,string)
set value 10^($valdB/10)
set result [expr $result+$value ]
set count [expr $count+1 ]
}
send "exit\r"
expect eof

set result [expr $result/20 ]
set resultdB 10*log(($result/10)/log(10))

puts "Average RSSI $resultdB"

# 4  
Old 02-17-2014
Use expr, also note use .0 on end of constants to force floating point calculations.

As you can see with my test data it comes pretty close to what bc returns:

Code:
$ tail -5 iw_avg.expect 
set result [expr $result/20.0 ]
set resultStr 10*log(($result/10.0)/log(10))
set resultVal [expr 10*log(($result/10.0)/log(10))]

puts "Average RSSI $resultVal ($resultStr)"

$ ./iw_avg.expect testSrv
Average RSSI 51.740556616639076 (10*log((4067.05/10.0)/log(10)))

$ bc -l
10*l((4067.05/10)/l(10))
51.74055661663907746850


Last edited by Chubler_XL; 02-17-2014 at 05:24 PM..
# 5  
Old 02-18-2014
I'm still having a problem with this code.
After a little bit of digging to figure out which calculation was going wrong, I found it to be the line:

Code:
set val [expr 10^($val/10)]

For an RSSI of -73 the output of result says -14 when it should be

10 ^(-73/10) = 5 * 10^-08

My guess is that it's some kind of wrapping going on, how do I avoid this?

Again, thank you so much for all your time and help.

Here is my code so far, please ignore my debug puts "$ "

Code:
#!/usr/bin/expect -f

set timeout 8
log_user 0
set ip [lindex $argv 0]
spawn -noecho telnet $ip
sleep 2

set count 0
set result 0.0
set val 0.0

send "PS1='PROMPT# '\r"

expect -re "PROMPT# '(.*)PROMPT# "
while {$count < 20} {
    send "iwconfig wlan0 | grep -o \"Signal level=....\" | cut -c14-16\r"
    expect -re "\r\n(.*)\r\n(.*)PROMPT# "
    set val $expect_out(1,string)
    puts "$val"
    #convert
    set val [expr 10^($val/10)]
    puts "$val"
    set result [expr $result+$val ]
    puts "$result"
    set count [expr $count+1 ]
}
send "exit\r"
expect eof
puts "$result"
#average
set result [expr $result/20.0 ]
puts "$result"
#convert back to dB
puts "$result"
set result [expr 10*log(($result/10.0)/log(10))]

puts "Average RSSI $result"

# 6  
Old 02-18-2014
As you can probably guess tcl is not a very powerful mathematical language. The ^ operator is bitwise exclusive OR not a power function. What you are after is pow():

Code:
set val [expr pow(10,$val/10.0)]

Checkout this tcl expr for more info on the expr command.

Last edited by Chubler_XL; 02-18-2014 at 12:21 PM.. Reason: Added helpfull link
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Suppressing output of a spawned telnet process

Hi, I'm trying to spawn a telnet process and trying to do some actions in the remote host using expect script. I would like to know how to suppress all the output in order the user using the script should not be able to see any actions done on the remote host. I tried using the "log_user 0"... (8 Replies)
Discussion started by: arun_maffy
8 Replies

2. HP-UX

ssh session getting hung (smilar to hpux telnet session is getting hung after about 15 minutes)

Our network administrators implemented some sort of check to kill idle sessions and now burden is on us to run some sort of keep alive. Client based keep alive doesn't do a very good job. I have same issue with ssh. Does solution 2 provided above apply for ssh sessions also? (1 Reply)
Discussion started by: yoda9691
1 Replies

3. UNIX for Dummies Questions & Answers

Disconnecting a telnet session

How can I disconnect an existing telnet session? The host is a serial port server with multiple ports. The users login using the host's name and a port, i.e. telnet host01 1235. Thanks. (14 Replies)
Discussion started by: cooldude
14 Replies

4. UNIX for Dummies Questions & Answers

Telnet Session to AIX

Hello, I have AIX 5.3 at home connected to netgear router. Port Forwarding has been enabled on the router. Problem is that if I want to telnet, I have to try 2 or 3 times before I can get a logon prompt. It times out for first or second time (Connection to session <IP_Address> failed: Connection... (1 Reply)
Discussion started by: bluebee
1 Replies

5. UNIX for Dummies Questions & Answers

Unix Telnet session

Hi Is there any way whilst in a telnet session you can view your client machine name that you are using to connect to the Unix box ? :eek: (2 Replies)
Discussion started by: mlucas
2 Replies

6. Shell Programming and Scripting

Telnet Session

{ sleep 2 echo "$user" sleep 2 echo "$password" sleep 2 echo " ls" sleep 10 echo "exit" }| telnet $server I have a machine x and i have executed the above script on machine 'x'. i entered the... (6 Replies)
Discussion started by: pathanjalireddy
6 Replies

7. Shell Programming and Scripting

Telnet session does not expire

Dear friends.. Our project has a module that runs on handheld devices. Through the handheld we telnet to solaris where the application actually runs. I noticed that after starting a session through the handheld, if i go out of range or if i remove and replace the battery in the handheld, the... (1 Reply)
Discussion started by: deepsteptom
1 Replies

8. UNIX for Dummies Questions & Answers

telnet session timeout

hi, we can set something such that if the user has been idle for a while, it will auto disconnect. where to do so? thanks (6 Replies)
Discussion started by: yls177
6 Replies

9. Programming

Find the IP address that a telnet session uses

Hello Experts, Happy New Year to all of us, In AIX 4.3.3 I am trying to figure out how is possible to find out the IP address that a telnet session - user uses to login in to host machine. My objective is by finding the login IP address to allow the user to login or not. All users uses the... (6 Replies)
Discussion started by: sszago
6 Replies
Login or Register to Ask a Question