VB如何执行Telnet命令(不出现命令提示符窗口)
不希望用生成批处理BAT后用Shell去调用,然后隐藏 提示符窗口 的做法!
Telnet进去后,还得执行3个命令,用VBS脚本我会做,但是运行了还是会出现 提示符窗口
我希望做出来得结果是:能在VB的TEXT文本框里看到执行后返回的数据,全部过程不需要调用到CMD提示符窗口
Telnet 命令如下:
-----------------------------------------------------
telnet members.3322.org 80
GET /dyndns/update?system=dyndns&hostname=myDB.3322.org&wildcard=OFF&offline=NO HTTP/1.1
Host: members.3322.org
Authorization: Basic bXlEQjoxMjM0NTY=
------------------------------------------------------
VBS脚本是这样的:
--------------------------------------------------
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "telnet members.3322.org 80"
WScript.Sleep(1200)
WshShell.SendKeys "GET /dyndns/update?system=dyndns&hostname=myDB.3322.org&wildcard=OFF&offline=NO HTTP/1.1{enter}"
WScript.Sleep(500)
WshShell.SendKeys "Host: members.3322.org{enter}"
WScript.Sleep(500)
WshShell.SendKeys "Authorization: Basic bXlEQjoxMjM0NTY={enter}"
WScript.Sleep(500)
WshShell.SendKeys "{enter}"
---------------------------------------------------
希望高手指教!!!!!
[最优解释]
一个text1控件和一个winsock1控件
大致代码就这样子
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Winsock1.Protocol = sckTCPProtocol
Winsock1.Connect "members.3322.org", 80
End Sub
Private Sub Winsock1_Connect()
Dim Str As String
Str = "GET /dyndns/update?system=dyndns&hostname=myDB.3322.org&wildcard=OFF&offline=NO HTTP/1.1 " & vbCrLf
Str = Str & "Host: members.3322.org" & vbCrLf
Str = Str & "Authorization: Basic bXlEQjoxMjM0NTY=" & vbCrLf
Winsock1.SendData Str
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
Winsock1.GetData Data
Text1.Text = Data
End Sub
[其他解释]
可以用winsock控件直接做
telnet 协议你可以用sanddata 一行一行给他发指令的,用vbcrlf加在每行结尾就行了
[其他解释]
能否用 API ,别用winsock控件呢?
带多个控件感觉麻烦,HOHO~~~~
[其他解释]
用API可以实现啊。不过只实现登录,一点用也没有,而实现一个完整的Telnet客户端,工作量又太大,所以建议楼主不妨自己GOOGLE一下,慢慢地实现一个。
[其他解释]
2楼的代码也不正确,没通过
[其他解释]
报错吗?
我这里可以运行的啊.
网上有用api实现winsock功能的类,代码很多啊,关键分清楚要做的是什么,陷在那堆API代码里是不是值得
[其他解释]
没报错,但是运行后没产生连接,TEXT里也没接收到返回的数据
[其他解释]
路过,顶一下
------其他解决方案--------------------
VBS 改为如下:
Set WshShell = Wscript.CreateObject("WScript.Shell")
a = WshShell.Run ("telnet members.3322.org 80" , 0)
WScript.Sleep(1200)
WshShell.SendKeys "GET /dyndns/update?system=dyndns&hostname=myDB.3322.org&wildcard=OFF&offline=NO HTTP/1.1{enter}"
WScript.Sleep(500)
WshShell.SendKeys "Host: members.3322.org{enter}"
WScript.Sleep(500)
WshShell.SendKeys "Authorization: Basic bXlEQjoxMjM0NTY={enter}"
WScript.Sleep(500)
WshShell.SendKeys "{enter}"
[其他解释]
用管道可以完成你的这个任务
pipe技术
等明天我找下代码来再给你
[其他解释]
一是可以用vbhide的方式启动窗口,然后用>重定向符输出到txt文件。
另外一个比较高级的技术
是使用管道技术
VB下用管道方式运行dos命令,可以把dos命令返回的结果显示在自己的窗口上。
就相当于在dos窗口和vb程序间建立了一个通道
在管道里开启一个进程,
让程序在管道里运行,
等待进程结束,
后将留在管道里的程序执行信息文件读取出来。
[其他解释]
代码是抄来的 网上自己搜索下
我没有验证
Option Explicit
Option Base 0
'Code written by JoshT. Use at your own risk
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
lpProcessAttributes As SECURITY_ATTRIBUTES, _
lpThreadAttributes As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
lpEnvironment As Any, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _
lpOverlapped As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, _
phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, _
ByVal nSize As Long) As Long
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
Private Const STARTF_USESTDHANDLES As Long = &H100&
Private Const STARTF_USESHOWWINDOW As Long = &H1&
Private Const SW_HIDE As Long = 0&
Private Const INFINITE As Long = &HFFFF&
Public Function RunCommand(CommandLine As String) As String
Dim si As STARTUPINFO 'used to send info the CreateProcess
Dim pi As PROCESS_INFORMATION 'used to receive info about the created process
Dim retval As Long 'return value
Dim hRead As Long 'the handle to the read end of the pipe
Dim hWrite As Long 'the handle to the write end of the pipe
Dim sBuffer(0 To 63) As Byte 'the buffer to store data as we read it from the pipe
Dim lgSize As Long 'returned number of bytes read by readfile
Dim sa As SECURITY_ATTRIBUTES
Dim strResult As String 'returned results of the command line
'set up security attributes structure
With sa
.nLength = Len(sa)
.bInheritHandle = 1& 'inherit, needed for this to work
.lpSecurityDescriptor = 0&
End With
'create our anonymous pipe an check for success
' note we use the default buffer size
' this could cause problems if the process tries to write more than this buffer size
retval = CreatePipe(hRead, hWrite, sa, 0&)
If retval = 0 Then
Debug.Print "CreatePipe Failed"
RunCommand = ""
Exit Function
End If
'set up startup info
With si
.cb = Len(si)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
.wShowWindow = SW_HIDE
' .hStdInput = GetStdHandle(STD_INPUT_HANDLE)
.hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
' .hStdError = GetStdHandle(STD_ERROR_HANDLE)
End With
'run the command line and check for success
retval = CreateProcess(vbNullString, _
CommandLine & vbNullChar, _
sa, _
sa, _
1&, _
NORMAL_PRIORITY_CLASS, _
ByVal 0&, _
vbNullString, _
si, _
pi)
If retval Then
'wait until the command line finishes
' trouble if the app doesn't end, or waits for user input, etc
WaitForSingleObject pi.hProcess, INFINITE
'read from the pipe until there's no more (bytes actually read is less than what we told it to)
Do While ReadFile(hRead, sBuffer(0), 64, lgSize, ByVal 0&)
'convert byte array to string and append to our result
strResult = strResult & StrConv(sBuffer(), vbUnicode)
'TODO = what's in the tail end of the byte array when lgSize is less than 64???
Erase sBuffer()
If lgSize <> 64 Then Exit Do
Loop
'close the handles of the process
CloseHandle pi.hProcess
CloseHandle pi.hThread
Else
Debug.Print "CreateProcess Failed" & vbCrLf
End If
'close pipe handles
CloseHandle hRead
CloseHandle hWrite
'return the command line output
RunCommand = Replace(strResult, vbNullChar, "")
End Function
Private Sub about_Click()
frmAbout.Show
End Sub
Private Sub begin_Click()
Dim i As Integer
Dim p As String
Dim q As String
Dim com As String
For i = 1 To 400
p = "c:\test\(" + Str(i) + ").html"
q = Str(i) + ".html"
com = "rename " + p + " " + q
Text2.Text = com
Text1.Text = Text1.Text + RunCommand(com)
'Text1.Text = RunCommand(Text2.Text)
Next i
End Sub
[其他解释]
我倒了,以 g 或者 p 打头的数据包winsock控件居然拒发~ 程序没有错,控件拒绝发送
[其他解释]
自己找到解决方法了,全部API实现
[其他解释]
楼上的能给我发个吗?我也想实现TELNET登陆然后在简单的发送命令,谢谢了
发到xlm1115@163.com
不胜感激!!
[其他解释]
楼上的能给我发个吗?我也想实现TELNET登陆然后在简单的发送命令,谢谢了
发到boying315@hotmail.com
不胜感激!!
[其他解释]
楼主能给我发份么 dwk126@126.com
谢谢
[其他解释]
楼上的能给我发个吗?我想使用API实现TELNET登陆然后在简单的发送命令,现在我不得不调用SecureCRT完成
邮箱 wanlun0711@163.com
不胜感激!!