This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Is there a place where i can post snippe scripts..?

Old posts that have not been replied to for several years.
Locked
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Is there a place where i can post snippe scripts..?

Post by Ofloo »

Sorry not sur where to post snippes or whatever there called sample scripts ..

this script shows u how u can read the incomming and out going speed on ur linux box maybe usefull to show in channel when u ask stats like this u can make an average speed or something .. or mrtg .. change ppp0 to eth0 or eth1 .. what ever you prefer ..

Constant readout

Code: Select all

#!/usr/bin/tclsh

proc speedcheck {} {
  global gouttraf ginctraf
  set cache [open "/proc/net/dev" "r"]
  set rfile [read $cache]
  close $cache
  foreach line [split $rfile "\n"] {
    if {[string match -nocase "ppp0" [lindex [split [lindex $line 0] \x3A] 0]]} {
      set inctraf [lindex [split [lindex $line 0] \x3A] 1]
      set outtraf [lindex $line 8]
    }
  }
  if {![info exists ginctraf]} {
    set ginctraf "$inctraf"
  }
  if {![info exists gouttraf]} {
    set gouttraf "$outtraf"
  }
  if {[info exists gouttraf] && [info exists ginctraf]} {
    set sinctraf [expr [expr $inctraf-$ginctraf]/1000]
    set souttraf [expr [expr $outtraf-$gouttraf]/1000]
    set gouttraf "$outtraf"
    set ginctraf "$inctraf"
    puts "Incoming $sinctraf kb/s"
    puts "Outgoing $souttraf kb/s"
  }
  after 1000
  speedcheck
}

speedcheck
Read only once

Code: Select all

#!/usr/bin/tclsh

proc speedcheck {} {
  foreach line [split [exec "/bin/cat" "/proc/net/dev"] "\n"] {
    if {[string match -nocase "ppp0" [lindex [split [lindex $line 0] \x3A] 0]]} {
      set ginctraf [lindex [split [lindex $line 0] \x3A] 1]
      set gouttraf [lindex $line 8]
    }
  }
  after 500
  foreach line [split [exec "/bin/cat" "/proc/net/dev"] "\n"] {
    if {[string match -nocase "ppp0" [lindex [split [lindex $line 0] \x3A] 0]]} {
      set inctraf [lindex [split [lindex $line 0] \x3A] 1]
      set outtraf [lindex $line 8]
    }
  }
  set sinctraf [expr [expr $inctraf-$ginctraf]/500.0]
  set souttraf [expr [expr $outtraf-$gouttraf]/500.0]
  puts "Incoming: $sinctraf kb/s"
  puts "Outgoing: $souttraf kb/s"
}

speedcheck
XplaiN but think of me as stupid
s
spock
Master
Posts: 319
Joined: Thu Dec 12, 2002 8:40 pm

Post by spock »

1 kB is 2^10 bytes (1024)
photon?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: Is there a place where i can post snippe scripts..?

Post by user »

Ofloo wrote:

Code: Select all

proc speedcheck {} {
  ...
  after 1000
  speedcheck
}
That's NOT the way to create an endless loop in tcl. You're doing recursive calls and there's a limit to the number of nested calls, so your code will eventually generate an error.
Use something like this instead:

Code: Select all

proc continous_speedcheck {} {
	while 1 {
		speedcheck
		after 1000
	}
}
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

1 kB is 2^10 bytes (1024)
right wasn't sur it was applyed for bandwith as well but now u mention it makes sence..
That's NOT the way to create an endless loop in tcl. You're doing recursive calls and there's a limit to the number of nested calls, so your code will eventually generate an error.
Use something like this instead:
tnx for the tip
XplaiN but think of me as stupid
Locked