CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2002
    Location
    England
    Posts
    530

    Telnet onto server and execute a batch file in VB

    Hi,

    I need to telnet onto a server and execute a batch file. Whilst no problem doing this manually, I need to do this through code. I understand I need to use winsock, but how???

    Many thanks.

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Telnet onto server and execute a batch file in VB

    Originally posted by jvbd02
    Hi,

    I need to telnet onto a server and execute a batch file. Whilst no problem doing this manually, I need to do this through code. I understand I need to use winsock, but how???

    Many thanks.
    start with a telnet control?

    http://www.dart.com/powertcp/telnet_included.asp
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Sep 2002
    Location
    England
    Posts
    530
    Looks a nice control - unfortunately purchasing it is out of the question, and it'll need to be in use for longer than the 30 days trial period on offer.

    I'm using winsock (through api calls and not a control). I have managed to connect and disconnect to the server, but how do I now execute the batch file.

    Thanks.

  4. #4
    Join Date
    Sep 2002
    Location
    England
    Posts
    530

    I'm being a bit thick

    After placing this on the backburner for some months I find myself addressing this problem again...

    Right, I've been using winsock api's and not a winsock control cos my work PC doesn't have licences for it (sore point, let's not go there). I'm not at my work PC right now so cant post the code, but I don't think that'll be problematic - the code is naff, it doesn't work, I want to scrap it.

    All I want to do, through VB (Access VBA), is connect to a UNIX box on the LAN, supply a username, then password, then, navigate to the correct directory on that box, and run a .bat file. It shouldn't be hard, but like the subject of this thread says, I am being a little thick.

    Please, please help!

    Thanks.

  5. #5
    Join Date
    Apr 2004
    Location
    Austria
    Posts
    43
    Use the Microsoft Winsock Control to create a TCP connection to the server and read the RFC for Telnet communications. that should enable you to send data to the telnet server.

    indeed the telnet sever is only listeing for ascii characters etc.. so you just have to use the winsock.send("login ...") method.

    greetings UNI

  6. #6
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362
    I wonder if it would be possible to use the windows telnet client, GetWindowText, and SendMessage APIs?

  7. #7
    Join Date
    Sep 2002
    Location
    England
    Posts
    530
    Monkey - will give it a go, thanks.

    Ulluvatan - can't use the winsock control, only api - I've adapted code from allapi.net but it doesn't work - I am clueless as to how to code it. Here is the code, don't laugh too hard! Also this code is executed in VB5 where it doesn't crash - hooking an Access form causes mayhem on my PC!

    Code:
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@Allapi.net
        Dim sSave As String
        Me.AutoRedraw = True
        Set Obj = Me.Text1
        'Start subclassing
        HookForm Me
        'create a new winsock session
        StartWinsock sSave
        'show the winsock version on this form
        If InStr(1, sSave, Chr$(0)) > 0 Then sSave = Left$(sSave, InStr(1, sSave, Chr$(0)) - 1)
        Me.Print sSave
        'connect to Microsoft.com
        lSocket = ConnectSock("dev01", 23, 0, Me.hwnd, False)
        
        SendData lSocket, "user" & Chr(0)
        SendData lSocket, "pass" & Chr(0)
        SendData lSocket, "cd /myfolder" & Chr(0)
        SendData lSocket, "my.bat" & Chr(0)
        
    End Sub

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    it would be easier to:

    1) write a cronjob for the utility

    or

    2) write a simple program to sit on the unix server and listen for a connection, and whenever it gets one, and maybe some authentication, run the bat file..

    if you wrote a program that listene don the unix server, and scanned the first line of every communication, for the word "hello" before it ran tha bat.. then you could run the bat with a web browser:

    http://UNIX_SERVER_NAME:PORT/hello


    here is an example program of such, in java:
    Code:
    import java.io.*;
    
    public class RunProg{
      public static void main(String[] argv)throws Exception{
        for(;;)
          if(new BufferedReader(new InputStreamReader(new ServerSocket(80).accept().getInputStream)).readLine().indexOf("hello") > -1 )
            Runtime.getRuntime().exec("/usr/me/myprog");
        }
      }
    }
    i could run it as shown above, with the web browser.
    Or with vb:

    Inet1.OpenURL("http://unixsrv/hello")
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    heck, if the unix box runs a webserver you could probably do it with cgi..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured