'This is an experimental application to confirm how to quickly
'get internet usage from tg582n router via its telnet interface in a simple way.
'The sent username, password and command to get usage are hardcoded for simplicity.
'The raw internet usage figures received from router are since the last router reset/restart (a rare event normally)
'zero offsets would be subtracted from usage figures for delta usages hourly/monthly/daily/per-bill
'references;
'how to telnet from vb.net https://stackoverflow.com/questions/27824264/sending-commands-to-telnet
'how to autoscroll textbox https://stackoverflow.com/questions/898307/how-do-i-automatically-scroll-to-the-bottom-of-a-multiline-text-box
'tg582n router telnet commands https://www.manualslib.com/download/914665/Technicolor-Tg582n.html
'
'it is hard keeping this really simple when so many easy ways of making this app more sophisticated.
'
Imports sns = System.Net.Sockets 'for .TcpClientclass.
Imports ste = System.Text.Encoding 'for bytes() = .ASCII.GetBytes(String) and string =.ASCII.GetString( byte() )
Public Class Form1
Dim Router_IpAddr As String = "192.168.1.254"
Dim TG582n_Username As String = "admin"
Dim TG582n_Password As String = "censored" 'this is the serial number of the router
Dim TG582n_UsageCmd As String = "ip iflist"
Dim TG582n_UsageRx As Integer = 231 'byte offset of number of received bytes & units in reply to usage command
Dim TG582n_UsageTx As Integer = 242 'byte offset 0f number of sent bytes & units in reply to usage command
Dim MyTelnetClient As New sns.TcpClient
Dim RxGrossMB, TxGrossMB, RxStartMB, TxStartMB As Single
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True 'this timer loads any received characters onto textTelnet typically every 100ms (5 to 300ms possible)
Timer2.Enabled = True 'this timer controls how often we ask the router for usage data. typically 10s (5s to 300s)
End Sub
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
Timer1.Enabled = False 'belt and braces - not essential
Timer2.Enabled = False 'belt and braces - not essential
MyTelnetClient.Close() 'belt and braces - not essential - unless open ports accumulate - vb closes files, ports etc by default on program end
End Sub
''' Call at start of application to OPEN a telnet connection to the router.
Private Sub ButtonLogin_Click(sender As Object, e As EventArgs) Handles ButtonLogin.Click
Try
MyTelnetClient.Connect(Router_IpAddr, 23) 'Connect to the router ip, 23 is telnet port
ButtonLogin.Enabled = Not MyTelnetClient.Connected 'non essential - belt and braces
ButtonUpdate.Enabled = MyTelnetClient.Connected 'non essential -if button already enabled
Catch ex As sns.SocketException 'if already logged in.... then nothing needs doing
Beep() : Debug.Print("Already logged in") 'non essential
End Try
End Sub
Sub Telnet_Send(ByVal cmd As String)
Dim bytes_to_send() As Byte = ste.ASCII.GetBytes(cmd) 'convert outgoing command string to byte(array)
Try
MyTelnetClient.Client.Send(bytes_to_send, 'send command to router
0,
bytes_to_send.Length,
sns.SocketFlags.None)
Catch ex As sns.SocketException 'if not yet logged in then cannot send the cmd, so do nothing
Beep() : Debug.Print("Not yet logged in - click the login button first.") 'non essential
End Try
End Sub
''' Sends a command and receives the reply every few seconds in order
''' to get the latest router usage figures in MB.
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If CheckAuto.Checked Then
If MyTelnetClient.Connected Then
ButtonUpdate_Click(Nothing, Nothing)
End If
End If
End Sub
''' Gets the latest router usage figures in MB.
Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click
Telnet_Send(TG582n_UsageCmd & vbCr) 'returns total router usage figures over all connected devices, e.g. ...
' {admin}=>ip iflist
' Interface Group MTU RX TX Admin Oper <--91
' 1 loop. . . . . . . . . . . . . . local 4096 419 MB 76 MB UP [UP] <-+73
'we want RX+TX & units from here 2 Internet. . . . . . . . . . . . wan 1500 3915 MB 277 MB UP UP <-+48
' 3 LocalNetwork. . . . . . . . . . lan 1500 348 MB 4302 MB UP [UP]
' 3195@+212 {admin}=>
' 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234
' 1 2 3 4 5 6 7 8
End Sub
''' Called many times a second to append any received characters from
''' telnet port to the textbox that shows sent and received characters.
''' Best called from a timer every 100ms rther than calling from a loop in another thread
''' because a timer can access form member objects like TextTelnet whereas another thread cannot do that.
Sub Telnet_Receive()
If MyTelnetClient.Client.Available > 0 Then 'Check if there is any Data to receive
Dim bytes_to_receive(MyTelnetClient.Available - 1) As Byte 'declare an array of bytes
MyTelnetClient.Client.Receive(bytes_to_receive, '
0,
bytes_to_receive.Length,
sns.SocketFlags.None) 'receive incomming telnet bytes
Dim String_From_Bytes As String = ste.ASCII.GetString(bytes_to_receive) 'convert received router bytes to string
If String_From_Bytes.Contains("Username : ") Then 'not essential. filter out ugly control chars preceding "Username: " by replacing "??????Username :" with just "Username :"
String_From_Bytes = "Username : " 'not essential. cosmetic. remove ff ff 01 ff ff 03 preamble
End If 'not essential. end.
TextTelnet.AppendText(String_From_Bytes) 'echo routers telnet (command and) reply (and prompt) AND scroll to last line
If String_From_Bytes.Contains("Username : ") Then 'If router asks for username then...
Telnet_Send(TG582n_Username & vbCr) '...send the username. it is admin for technicolor gateway tg582n
ElseIf String_From_Bytes.Contains("Password : ") Then 'If telnet asks for Password then...
Telnet_Send(TG582n_Password & vbCr) '...send password. it is router serial (11 chars) for Technicolor Gateway TG582n
ElseIf String_From_Bytes.Contains("iflist") Then 'if reply to (echoed) iflist command
If String_From_Bytes.EndsWith(">") AndAlso String_From_Bytes.Length > 270 Then 'if we have whole reply
Dim rx As String = String_From_Bytes.Substring(TG582n_UsageRx, 10) 'bytes received tg482n ONLY
Dim tx As String = String_From_Bytes.Substring(TG582n_UsageTx, 10) 'bytes sent tg582n ONLY (on a good day)
RxGrossMB = Val(rx) 'get values in KB,MB or GB
TxGrossMB = Val(tx)
If rx.ToUpper.Contains("K") Then RxGrossMB /= 1024 'ensure values always in MB (for now)
If tx.ToUpper.Contains("K") Then TxGrossMB /= 1024
If rx.ToUpper.Contains("G") Then RxGrossMB *= 1024
If tx.ToUpper.Contains("G") Then TxGrossMB *= 1024
Label_All_MB.Text = RxGrossMB + TxGrossMB 'show total rx + tx MB
End If
End If
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Telnet_Receive()
End Sub
End Class