Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Telnet scripted using vbscript

  • 27-08-2013 2:00pm
    #1
    Closed Accounts Posts: 1,323 ✭✭✭


    Hi there,
    Im using vbscript to automate a telnet login, then store all the output into a log file before using vbscript regex to parse the data, but im struggling...

    Heres my code...
    Set obj = CreateObject("WScript.Shell")
    obj.run"cmd"
    WScript.Sleep 500
     
    
    cnt=0
    mrn = "508589"
    initials = "O "
    name = "xxx"
    obj.SendKeys"telnet 10.2.10.7 -f log.txt"
    obj.SendKeys("{Enter}")
    WScript.Sleep 150
    obj.SendKeys"xxx"  'telnet username
    obj.SendKeys("{Enter}")
    WScript.Sleep 50
    obj.SendKeys"xxx"  'telnet password
    obj.SendKeys("{Enter}")
    WScript.Sleep 500
    obj.SendKeys"1"
    obj.SendKeys("{Enter}")
    WScript.Sleep 300
    obj.SendKeys("STJPC2")
    WScript.Sleep 300
    obj.SendKeys("{Enter}")
    obj.SendKeys("{Enter}")
    obj.SendKeys("{Enter}")
    obj.SendKeys"1"
    obj.SendKeys("{Enter}")
    obj.SendKeys(MRN)
    obj.SendKeys("{Enter}")
    obj.SendKeys(initials)
    obj.SendKeys("{Enter}")
    obj.SendKeys("{Enter}")
    obj.SendKeys("{Enter}")
    obj.SendKeys("{Enter}")
    '**** appears in log file - parse it!
    obj.SendKeys("{PGUP}")
    obj.SendKeys("{Down}")
    obj.SendKeys("{PGUP}")
    obj.SendKeys("{Down}")
    obj.SendKeys("{PGUP}")
    obj.SendKeys("{Down}")
    obj.SendKeys("{PGUP}")
    obj.SendKeys("{Down}")
    WScript.Sleep 3000
    
    Dim objFSO, filepath, objInputFile, tmpStr, substrToFind
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    filepath = "C:\Documents and Settings\john.oconnor\log.txt"
    'substrToFind = "Specimen No :"
    Set objInputFile = objFSO.OpenTextFile(filepath)
    Do until objInputFile.AtEndOfStream
        cnt = cnt+1
        tmpStr = objInputFile.ReadLine
        'msgbox tmpstr
        If foundStrMatch(tmpStr)=true Then
           ' If foundFirstMatch = false Then
           '     index = getIndex(tmpStr)
           '     foundFirstMatch = true
           '     text = text & vbCrLf & textSubstitution(tmpStr,index,"true")
           ' End If
           ' If index = getIndex(tmpStr) Then
           '     text = text & vbCrLf & textSubstitution(tmpStr,index,"false")
           ' ElseIf index < getIndex(tmpStr) Then
           '     index = getIndex(tmpStr)
           '     text = text & vbCrLf & textSubstitution(tmpStr,index,"true")
           ' End If
        msgbox "found line: " & cnt
        'Else
        '    text = text & vbCrLf & textSubstitution(tmpStr,index,"false")
        End If
    Loop
    'Set objOutputFile = objFSO.CreateTextFile("C:\VBS\NuovaProva.txt", ForWriting, true)
    'objOutputFile.Write(text)
    'End Sub
    msgbox cnt
    
    Function textSubstitution(tmpStr,index,foundMatch)
    Dim strToAdd
    strToAdd = "<tr><td><a href=" & chr(34) & "../../Logs/CF5.0_Features/Beginning_of_CF5.0_Features_TC" & CStr(index) & ".html" & chr(34) & ">Beginning_of_CF5.0_Features_TC" & CStr(index) & "</a></td></tr>"
    If foundMatch = "false" Then
        textSubstitution = tmpStr
    ElseIf foundMatch = "true" Then
        textSubstitution = strToAdd & vbCrLf & tmpStr
    End If
    End Function
    
    Function getIndex(tmpStr)
    Dim substrToFind, charAtPos, char1, char2
    substrToFind = "<tr><td><a href=" & chr(34) & "../Test case "
    charAtPos = len(substrToFind) + 1
    char1 = Mid(tmpStr, charAtPos, 1)
    char2 = Mid(tmpStr, charAtPos+1, 1)
    If IsNumeric(char2) Then
        getIndex = CInt(char1 & char2)
    Else
        getIndex = CInt(char1)
    End If
    End Function
    Function foundStrMatch(tmpStr)
    Dim substrToFind
    substrToFind = "Serum"
    msgbox tmpstr
    Set re = New RegExp
    with re
     .IgnoreCase = True
     .Global = False
     .Pattern = "^\d{2}/\d{2}/\d{4} "
    end with
    
    if re.test(tmpStr) then
      msgbox objMatch.Value & " " &cnt
    end if
    End Function
    

    Oops sorry, meeting on... i'll post my q later...
    Thank you!


Comments

  • Closed Accounts Posts: 1,323 ✭✭✭Dr Nic


    cscript minusf.vbs


  • Closed Accounts Posts: 1,323 ✭✭✭Dr Nic


    Cripes telnet is awful clunky. I cant understand why anyone would use it. vbscript is nice however. My script is working now pretty much.
    I'll post it later...


  • Registered Users, Registered Users 2 Posts: 2,751 ✭✭✭MyPeopleDrankTheSoup


    because nobody really uses it anymore, it's all SSH now and has been for a long time. i see it's a local IP address you're connecting to so I presume the sysadmins ensure telnet is safe to use locally.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    A unix a tool called "expect" makes this kind of remote scripting really easy.


  • Closed Accounts Posts: 1,323 ✭✭✭Dr Nic


    cant use unix, have to use vbscript :-(
    Maybe can use activeX telnet component ...
    This sendkeys thing is a disaster though. its so flakey


  • Advertisement
Advertisement