Thread: A tenet connection from a C program..

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    A tenet connection from a C program..

    Hello,

    Can I establish a Telnet connection using a C function ?

    and then make the function read a text file line by line. That file contains the username, password and other commands to execute on the remote computer once the connection has been established....

    I know some visual basic functions that can do that such as:
    crt.Screen.Synchronous
    crt.Session.Connect
    crt.Screen.Send
    crt.Screen.WaitForString


    but I don't know their equvalent in C programming language..

    Thanks,

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean like this? (From the shell, not from C.)
    Code:
    $ telnet whereever < input-file
    BTW, I would advise you not to put your password in a plain-text file like that . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Quote Originally Posted by dwks View Post
    You mean like this? (From the shell, not from C.)
    Code:
    $ telnet whereever < input-file
    BTW, I would advise you not to put your password in a plain-text file like that . . . .
    I want to read the input file line by line.
    This is what I mean:
    Sub Main

    Const username = "" ' Username to use for login

    Const password = "" ' Password for corresponding user

    Const loginPass = "" ' Password to use for password only login (aka no aaa new-model)



    Const oldEnablePass = "" ' The current enable Password

    Const newLoginPass = "" ' New password to set for telnet and console lines

    Const newEnablePass = "" ' New Enable password to set



    Const DEVICE_FILE_PATH = "c:\scripts\devices.txt"



    Dim fso

    Set fso = CreateObject("Scripting.FileSystemObject")



    Dim fil

    Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)



    Dim ip

    Dim name

    Dim protocol

    Dim line
    Dim cnxnString
    While Not fil.AtEndOfStream
    line = fil.ReadLine

    name = Split(line, ";")(0)
    ip = Split(line, ";")(1)
    protocol = Split(line, ";")(2)

    Select Case protocol
    Case "Telnet"
    cnxnString = "/TELNET " & ip & " 23"
    Case "SSH2"
    cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip
    Case "SSH1"
    cnxnString = "/SSH1 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip
    End Select


    ' Connect

    crt.Screen.Synchronous = True

    crt.Session.Connect cnxnString

    If protocol = "Telnet" Then

    Dim index

    index = crt.Screen.WaitForStrings("Username:", "Password:")



    If index = 1 Then

    crt.Screen.Send username & vbCr

    crt.Screen.WaitForString "Password:"

    crt.Screen.Send password & vbCr

    Else If index = 2

    crt.Screen.Send loginPass & vbCr

    End If

    End If



    ' Enable

    crt.Screen.WaitForString ">"

    crt.Screen.Send "en" & vbCr

    crt.Screen.WaitForString "Password:"

    crt.Screen.Send oldEnablePass & vbCr



    crt.Screen.WaitForString "#"

    crt.Screen.Send "conf t" & vbCr

    crt.Screen.WaitForString "(config)#"



    ' change enable password

    crt.Screen.Send "enable secret " & newEnablePass & vbCr

    crt.Screen.WaitForString "(config)#"



    ' Change telnet password

    crt.Screen.Send "line vty 0 15" & vbCr

    crt.Screen.WaitForString "(config-line)#"

    crt.Screen.Send "password " & newLoginPass & vbCr

    crt.Screen.WaitForString "(config-line)#"

    crt.Screen.Send "exit" & vbCr

    crt.Screen.WaitForString "(config)#"



    ' Change console password

    crt.Screen.Send "line con 0" & vbCr

    crt.Screen.WaitForString "(config-line)#"

    crt.Screen.Send "password " & newLoginPass & vbCr

    crt.Screen.WaitForString "(config-line)#"

    crt.Screen.Send "exit" & vbCr

    crt.Screen.WaitForString "(config)#"



    ' Save

    crt.Screen.Send "exit" & vbCr

    crt.Screen.WaitForString "#"

    crt.Screen.Send "copy run start" & vbCr

    crt.Screen.WaitForString "startup-config"

    crt.Screen.Send vbCr

    crt.Screen.WaitForString "#"



    crt.Screen.Synchronous = False

    crt.Session.Disconnect

    Wend



    fil.Close

    End Sub

    Which is a vbs script that I found on some site. It's pretty easyto understand it's code, but I need to write a similar one in C.
    Is that possible?
    Last edited by ahmd2080; 07-04-2009 at 03:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Replies: 3
    Last Post: 01-14-2003, 10:34 PM