Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wscript.shell.sendkeys

Status
Not open for further replies.

MrsMopey

Programmer
Jul 31, 2007
19
Hi,
I am trying to open a cmd prompt from an HTML page. I want to be able to log in to a telnet session, pass two variables from textboxes on the HTML form and execute a command. So far this is what I have:
Code:
<HTML>
<HEAD>
<TITLE>
Reset SX User
</TITLE>
<SCRIPT LANGUAGE="vbscript">
OPTION EXPLICIT

Sub cmdReset_OnClick

Dim txtUserID
Dim txtCoNo
Dim objShell

txtUserID = frmForm1.txtUserID.Value
txtCoNo= frmForm1.txtUserID.Value

Set objShell = CreateObject("wscript.Shell")

objShell.Run "%comspec% /k telnet volmrs"

objShell.SendKeys "root" [b][COLOR=red]<<<<< it stops here >>>[/color red][/b]
objShell.SendKeys "{ENTER}"
wscript.sleep(50)
objShell.SendKeys "fff"
wscript.sleep(100)
objShell.SendKeys "{ENTER}"
wscript.sleep(10)
objShell.SendKeys "Shut User " & txtCoNo & txtUserID
objShell.SendKeys "{ENTER}"


End Sub
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<FONT FACE="arial" SIZE=2><B>
<CENTER>
<H3>Reset SX User</H3>
<P>
Enter the UserID and Company number of the person you would like to reset in SX.
</P>
<FORM NAME="frmForm1">

User ID
<INPUT TYPE="text" NAME="txtUserID" SIZE=10>
<P>
<INPUT TYPE="button" NAME="cmdReset" VALUE="Reset">
<P>
Company Number
<INPUT TYPE="text" NAME="txtCoNo">
</FORM>
</BODY>
</HTML>

I can't get it to work. It stops after the first line. What am I doing wrong?

Thanks,


 
You may try this:
...
objShell.Run "%comspec% /k telnet volmrs"
WScript.Sleep 500
objShell.AppActivate "Telnet"
objShell.SendKeys "root{ENTER}"
WScript.Sleep 500
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,
I changed the code as you suggested, however it still 'hangs' on the login prompt as before.

Updated code:
Code:
Sub cmdReset_OnClick

Dim txtUserID
Dim txtCoNo
Dim objShell

txtUserID = frmForm1.txtUserID.Value
txtCoNo= frmForm1.txtUserID.Value

Set objShell = CreateObject("wscript.Shell")

objShell.Run "%comspec% /k telnet volmrs"
Wscript.sleep 500
objshell.AppActivate "Telnet" [b][COLOR=red]<<stops here, the login value doesn't get entered>>[/color][/b]
objshell.SendKeys "root{ENTER]"
wscript.sleep(500)

objShell.AppActivate "mci"
objShell.SendKeys "{ENTER}"
wscript.sleep(500)

objShell.AppActivate "Shut User " & txtCoNo & txtUserID
objShell.SendKeys "{ENTER}"
 
I don't think that telnet is sciptable.
You may try to use rsh instead.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Or even better, look at a free Telnet component like Dimac's w3sockets.

Or better yet, create an ActiveX control in VB6 for this purpose that provides an async interaction model so the page doesn't freeze when communicating with a slow server.
 
ive used tst10.exe, you pass it a text file, edit the text file on the fly before you pass it to it...

i am also using plink.exe but that is for ssh sessions
 
when i do lower myself to using sendkeys :)-)) i tend to wrap the app.activate in a loop and exit the loop when app.activate returns true
 
I have downloaded the w3sockets, but I am getting an error in my sleep command. Here is my w3socket code
Code:
Sub ResetUser
Dim oSocket, iErr, sSocketText
sSocketText = ""
Set oSocket = CreateObject("Socket.TCP")
oSocket.DoTelnetEmulation = True
oSocket.TelnetEmulation = "TTY"
oSocket.Host = "volmrs"
On Error Resume Next 
oSocket.Open
iErr = Err.Number
If iErr <> 0 Then 
 Exit Sub
End If 
oSocket.SendText "Root"
osocket.waitfor  ("root's Password")
oSocket.SendText "MCI"
oSocket.waitfor ("(volmrs)#")
oSocket.SentText ("Shut User " & txtCoNo & txtUserID")

End Sub

I must have something incorrect.
 
I see one error:

[tt]oSocket.SentText ("Shut User " & txtCoNo & txtUserID")[/tt]

Should be [tt]SendText[/tt]. Also the parens aren't necessary (or really correct) though this shouldn't cause a failure here:

[tt]oSocket.SentText ("Shut User " & txtCoNo & txtUserID")[/tt]

You have these on your [tt]WaitFor[/tt] calls too.


Also consider that [tt]SendText[/tt] does not send a newline at the end, for that you'd want [tt]SendLine[/tt] instead. Maybe this is the source of the problem?
 
I changed as you suggested:
Code:
<SCRIPT LANGUAGE="vbscript">
OPTION EXPLICIT

Sub cmdReset_OnClick

Dim txtUserID
Dim txtCoNo
Dim oSocket, iErr, sSocketText

sSocketText = ""
Set oSocket = CreateObject("Scripting.FileSystemObject")
oSocket.Host = "volmrs"
On Error Resume Next
oSocket.Open
iErr = Err.Number
If iErr <> 0 Then
 Exit Sub
End If

oSocket.SendLine "Root"
osocket.waitfor "root's Password"
oSocket.SendLine "MCI"
oSocket.waitfor "(volmrs)#"
oSocket.SendLine "Shut User " & txtCoNo & txtUserID

I get an error message saying :
Object doesn't support this property or method " oSocket.Host
 
Why create a FSO ?????

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I dunno, I was following the readme.txt that downloaded with the w3socket app. This is the first time I have had to use this, so I don't quite know what I am doing. If I have my syntax wrong, can you point me in the right direction.
 
your first post had the write syntax NOT
Set oSocket = CreateObject("Scripting.FileSystemObject")
BUTT
Set oSocket = CreateObject("Socket.TCP")
which will explain the runtime about object not supporting method

its the weekend
 
Yep, I fixed that back to Socket.TCP, and now I don't get the error, but I don't see a cmd window open so how do I know if the command executed correctly?
 
Don't forget to add back:

[tt]oSocket.DoTelnetEmulation = True
oSocket.TelnetEmulation = "TTY"[/tt]

Otherwise you get a TCP socket instead of a Telnet socket.
 
Why would you expect a command window?

The results should come back to the w3sockets object. Use the [tt]GetText[/tt] method to retrieve the results.
 
I threw a MsgBox command in to see if I had an error and I get an error number : 2147418113 whatever the heck that is.
 
You can also use a [tt]WaitFor[/tt] and then check [tt]Buffer[/tt] for results.

This WSH script example assumes a Windows Telnet server:
Code:
Option Explicit
Dim w3sock

Set w3sock = CreateObject("socket.tcp")
With w3sock
    .DoTelnetEmulation = True
    .TelnetEmulation = "TTY"
    .Host = "localhost:23"
    .Open
    .WaitFor "login:"
    .SendLine "George"
    .WaitFor "password:"
    .SendLine "Curious"
    .WaitFor ">"
    .SendLine "prompt $LREADY$G"
    .WaitFor "<READY>"
    .SendLine "dir c:\windows\repair"
    .WaitFor "<READY>"
    'Parse echoed DIR line and trailing <READY> off results.
    WScript.Echo Split(Split(.Buffer, vbNewLine, 2)(1), "<READY>")(0)
    .Close
End With
Set w3sock = Nothing
 
I changed the code again:
Code:
<HTML>
<HEAD>
<TITLE>
Reset SX User
</TITLE>
<SCRIPT LANGUAGE="vbscript">
Option Explicit
Dim w3sock

Set w3sock = CreateObject("socket.tcp")
With w3sock
    .DoTelnetEmulation = True
    .TelnetEmulation = "TTY"
    .Host = "volmrs"
    .Open
    .WaitFor "login:"
    .SendLine "root"
    .WaitFor "password:"
    .SendLine "mci"
    .WaitFor ">"
    .SendLine "SHUT USER" & txtUserID.Value & txtCoNo.Value
    .WaitFor "<READY>"
    .SendLine "dir c:\windows\repair"
    .WaitFor "<READY>"
    'Parse echoed DIR line and trailing <READY> off results.
    WScript.Echo Split(Split(.Buffer, vbNewLine, 2)(1), "<READY>")(0)
    .Close
End With
Set w3sock = Nothing
</SCRIPT>
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<FONT FACE="arial" SIZE=2><B>
<CENTER>
<H3>Reset SX User</H3>
<P>
Enter the UserID and Company number of the person you would like to reset in SX.
</P>
<FORM NAME="frmForm1">

User ID
<INPUT TYPE="text" NAME="txtUserID" SIZE=10>
<P>
<INPUT TYPE="button" NAME="cmdReset" VALUE="Reset">
<P>
Company Number
<INPUT TYPE="text" NAME="txtCoNo">
</FORM>
</BODY>
</HTML>

Now I get error saying :ERROR WHILE PROCESSING SOCKET OPERATION. CAN'T ASSIGN REQUESTED ADDRESS
 
I don't know why you're getting that error. Perhaps the server is not available?


Also, you have copied things from my sample that are for a Windows Telnet server, like waiting for "<READY>" which will only work if the prompt has been changed to <READY>.

You also need to wait for the login and password prompts that your server uses. My examples were for a Windows Telnet server, maybe your server is something else? Hard to imagine anyone making a user ID of "root" on a Windows box.

You also can't use WScript.Echo in an HTML page. The example was a WSH script. You'll either need a MsgBox call or maybe insert the text into a <DIV> element or something's innerText (or innerHTML after converting vbNewLines into <BR>s).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top