0

I am using this website (http://npr.me.uk/scripting.html) to connect to telnet and run command. It returns me some information. I need to get this info every 4 seconds. How do I do that? Now it runs but reconnects everytime, so I have to wait while it opens a connection and it takes much more than 4s. Bat file:

echo off
cls
if exist r1.txt del r1.txt
if exist r2.txt del r2.txt
tst10.exe /r:stats.txt /o:r1.txt /m
for /f "skip=30 tokens=*" %%A in (r1.txt) do echo %%A >> r2.txt
del r1.txt
start r2.txt

And stats file:

192.168.xxx.xxx
WAIT "login:"
SEND "myuser\m"
WAIT "Password:"
SEND "mypass\m"
WAIT ">"
SEND "mycommand\m"
WAIT ">"
babboon
  • 683
  • 3
  • 20
  • 45
  • 1
    What does this have to do with VB.NET? – J... Dec 02 '13 at 11:16
  • Because later I will have to work with this info from r2.txt using vb.net – babboon Dec 02 '13 at 11:18
  • 1
    So why bother with a telnet program? .NET has sockets support - you can make a telnet connection in VB.NET instead. http://stackoverflow.com/questions/390188/c-sharp-telnet-library (search...there are more here on SO - this is for C# but it is easily translated to VB.NET) – J... Dec 02 '13 at 11:24
  • 1
    if you're bent on using this scritpting approach, however, what you are looking for is `Process.Start` http://msdn.microsoft.com/en-us/library/53ezey2s.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 -- this will allow you to run the batch file (as a separate process) programmatically from within your application. – J... Dec 02 '13 at 11:31
  • I will check on telnet connection in VB.NET. It would awesome if I retrieve info inside vb.net application. Thanks. But if I use Process.Start with this bat it will reconnect anyway and it will take more than 4s. – babboon Dec 02 '13 at 11:38
  • Worked great! Thanks J... – babboon Dec 02 '13 at 14:27

1 Answers1

0

Use Powershell to program using a csv file with the connections, I am using it for re-programming mfd's

I have a file mfd.txt and a script that reads it in.

I have a telnet script template to change the settings on the mfd and the powershell script creates custom scripts for each mfd and sets dns and hostname parameters. When run, a logfile is piped into a directory for checking later

Script is as follows:

#Process for updating devices quickly using telnet

#Check file exists
c:
cd 'C:\Resources\Telnet'
cls
$fileisthere = $false
$fileisthere = test-path 'C:\Resources\Telnet\mfds.csv'

if ($fileisthere -ne $true) 
       { 
        ""
        Write-Host ("There is no MFD import list C:\Resources\telnet\mfds.csv")  | out-file -filepath $logfile -force
        ""
        exit 
        }

        Write-Host ("MFD import List is present")


# for each device in devices:
$mfds = import-csv 'C:\Resources\Telnet\mfds.csv'

foreach ($mfd in $mfds) 
{             
#   ping device and check for response
$mfdname = $mfd.name
$mfdip = $mfd.ipaddress
$mfddns1 = $mfd.dns1
$mfddns2 = $mfd.dns2
$mfdhostname = $mfd.serial

""
Write-Host ("Updating device $($mfdname) on IP address $($Mfdip) ")
"" 
("Updating device $($mfdname) on IP address $($Mfdip) ")      |  out-file -filepath $logfile -Append -force

if(!(Test-Connection -Cn $mfdip -BufferSize 16 -Count 1 -ea 0 -quiet))
        {         
        Write-Host ""
        Write-Host ("MFD $($mfdname) is offline or not at this address")
        Write-Host ""                                                         
        ""                                                    |  out-file     $logfile -Append -force
        ("MFD $($mfdname) is offline or not at this address") |  out-file $logfile -Append -force
        ""                                                    |  out-file $logfile -Append -force

        }

else
        {

       #find replace script

# Device is present and add to script header
        $tststring = "$($mfdip) 23"
        $tstfile = "$($mfdname)-$($mfdip).txt"
        $tstlogfile = "$($mfdname)-$($mfdip).log"
        $tststring | out-file $tstfile -force
        type dns.txt >> $tstfile

        $location1 =  "C:\Resources\telnet\$($tstfile)"
        $change1 = get-content $location1
        $change1 | ForEach-Object { $_ -replace "dns 1 server", "dns 1 server $($mfddns1)"} | Set-Content $location

        $location2 =  "C:\Resources\telnet\$($tstfile)"
        $change2 = get-content $location2
        $change2 | ForEach-Object { $_ -replace "dns 2 server", "dns 2 server $($mfddns2)"} | Set-Content $location


        $location3 =  "C:\Resources\telnet\$($tstfile)"
        $change3 = get-content $location3
        $change3 | ForEach-Object { $_ -replace "hostname ether name", "hostname ether name $($mfdhostname)"} | Set-Content $location

        $location4 =  "C:\Resources\telnet\$($tstfile)"
        $change4 = get-content $location4
        $change4 | ForEach-Object { $_ -replace "devicename name", "devicename name $($mfdhostname)"} | Set-Content $location


        # Create variables for update
        Write-Host ("Updating $($Mfdname) on IP Address $($mfdIP) ")

        $parameter1 = "/r:$($tstfile)"
        $parameter2 = "/o:$($tstlogfile)"

        #& cmd tst10 $parameter1 $paremeter2
        write-host ("$($tstfile)  $($tstlogfile)")


        new-item $tstfolder -Type directory
        move-item $tstfile $tstfolder
        move-item $tstlogfile $tstfolder -ErrorAction SilentlyContinue

        }


}