egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

!bw Bandwidth command

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
Zecca93
Voice


Joined: 26 Apr 2012
Posts: 13

PostPosted: Mon Oct 14, 2013 11:04 pm    Post subject: !bw Bandwidth command Reply with quote

Hey is there script or can anybody make script for Bandwidth like this

<me> !bw
<bot> Upstream: 4.25kB/s (Uploaded: 3185.23MB) Downstream: 4.07kB/s (Downloaded: 592.80MB)

Thanks.
Back to top
View user's profile Send private message
spithash
Master


Joined: 12 Jul 2007
Posts: 248
Location: Libera

PostPosted: Fri Oct 18, 2013 4:24 am    Post subject: Reply with quote

Code:

namespace eval bandwidth {
  variable api "/proc/net/dev"
  variable interface "eth0:LAN" ;# for windows win:<something> without the others..
  # %IFN% : example eth0 # %IFA% : example WAN # %INC% : Incomming traffic # %OUT% : Outgoing traffic
  variable template "\[\002%IFA%\002\]: Incoming: \(\002%INC%\002\) kb/s Outgoing: \(\002%OUT%\002\) kb/s."
  # 0: privmsg to channel # 1: privmsg to nick # 2: notice to nick
  variable msgtype "0"
  variable pubflag "-"
  variable bandwidth "!bw"
  variable version 0.2
  switch -regexp [string tolower $::tcl_platform(platform)] {
    "win" {
      variable switch 1
    }
    "unix" {
      variable switch 2
    }
    ".*" {
      variable switch 0
    }
  }
  switch  -exact $msgtype {
    "0" { variable msg "PRIVMSG %CHAN%" }
    "1" { variable msg "PRIVMSG %NICK%" }
    "2" { variable msg "NOTICE %NICK%" }
  }
 proc f {value} {
    set test $value ; set unit 0
    while {[set test [expr {$test / 1024}]] > 0} {
       incr unit
    }
    return [format "%.2f %s" [expr {$value / pow(1024,$unit)}] [lindex [list B KB MB GB TB PB EB ZB YB] $unit]]
 }

  proc totals {n u h c t} {
variable interface
   set iface [lindex [split $interface ":"] 0]
   set network [lindex [split $interface ":"] 1]

#catch {exec cut \-d. \-f1 /proc/uptime} reply; set secs [expr $reply % 60]; set mins [expr $reply / 60 % 60]; set hours [expr $reply / 3600 % 24]; set days [expr $reply / 86400]

catch {set useless [exec uptime]} uptime
#set uptime "${days}d ${hours}h ${mins}m ${secs}s"
   if {![catch {set data [exec /sbin/ifconfig $iface]} error]} {
      foreach line [split $data "\n"] {
         if {[string match "*RX bytes*" $line]} {
         set rxb [lindex [split [lindex $line 1] ":"] 1]
         set txb [lindex [split [lindex $line 5] ":"] 1]
         putserv "PRIVMSG $c :\002\[\002$network\002\]\002: total of [f $rxb] incoming, total of [f $txb] outgoing in $uptime."
         }
      }
   }
}
  proc bandwidth_pub {nick host hand chan arg} {
   totals $nick $host $hand $chan $arg
    variable switch; variable template; variable interface; variable msg; variable api
    if {[string equal 0 $switch]} {
      putlog "Error unknown operating system."
    } elseif {[string equal "1" $switch]} {
      if {[regexp -nocase {^(win):([a-zA-Z0-9\-]{1,100})$} $interface -> ifn ifa]} {
        set s_inc 0; set s_out 0; set e_inc 0; set e_out 0
        foreach {x} [split [exec netstat -e] \n] {
          if {[string equal -nocase -length 4 byte $x]} {
            set s_inc [lindex $x 1]
            set s_out [lindex $x 2]
          }
        }
        after 500
        foreach {x} [split [exec netstat -e] \n] {
          if {[string equal -nocase -length 4 byte $x]} {
            set e_inc [lindex $x 1]
            set e_out [lindex $x 2]
          }
        }
        putserv "[string map [list %NICK% $nick %CHAN% $chan] $msg] :[string map [list %IFN% $ifn %IFA% $ifa %INC% [format %.2f [expr ($e_inc - $s_inc) / 512.0]] %OUT% [format %.2f [expr ($e_out - $s_out) / 512.0]]] $template]"
      } else {
         putlog "Script misconfiguration, check your settings."
      }
    } elseif {[string equal "2" $switch]} {
      if {[file exists $api]} {
        if {[file readable $api]} {
          if {[regexp {win:} $interface]} {
            putlog "Script misconfiguration, check your settings."
          } else {
            if {[catch {open $api} rf]} {
              putlog "Error couldn't open $api for an unknown reason."
            } else {
               while {![eof $rf]} {
                 gets $rf x
                foreach {ifn ifa} [split $interface \x3a\x2c] {
                  regexp "$ifn:\[\x20\t\]{0,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}" $x -> s_inc($ifn) s_out($ifn)
                }
              }
              close $rf
            }
            after 500
            if {[catch {open $api} rf]} {
              putlog "Error couldn't open $api for an unknown reason."
            } else {
               while {![eof $rf]} {
                 gets $rf x
                foreach {ifn ifa} [split $interface \x3a\x2c] {
                  if {[regexp "$ifn:\[\x20\t\]{0,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}" $x -> e_inc($ifn) e_out($ifn)]} {
                    putserv "[string map [list %NICK% $nick %CHAN% $chan] $msg] :[string map [list %IFN% $ifn %IFA% $ifa %INC% [format %.2f [expr ($e_inc($ifn) - $s_inc($ifn)) / 512.0]] %OUT% [format %.2f [expr ($e_out($ifn) - $s_out($ifn)) / 512.0]]] $template]"
                  }
                }
              }
              close $rf
            }
          }
        } else { putlog "Error $api is not readable."  }
      } else { putlog "Error $api does not exist." }
    } else { putlog "Error unknown operating system." }
  }
  bind pub $pubflag $bandwidth [namespace current]::bandwidth_pub
}

_________________
Libera ##rtlsdr & ##re - Nick: spithash
Click here for troll.tcl
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber