PDA

View Full Version : A tenet connection from a C program..



ahmd2080
07-03-2009, 04:14 PM
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,

dwks
07-03-2009, 04:39 PM
You mean like this? (From the shell, not from C.)

$ telnet whereever < input-file

BTW, I would advise you not to put your password in a plain-text file like that . . . .

ahmd2080
07-04-2009, 03:42 AM
You mean like this? (From the shell, not from C.)

$ telnet whereever < input-fileBTW, 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?