![]() |
|
|
| Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site | ||
|
|
IM Commands - CMD executing auto response bot download codeThe following code is an Instant messaging version of one of my other articles Mailbox Commands. The purpose of this code is to allow you to send an Instant message to the response bot and have it execute a CMD command and then return the response of that command to you as an instant message. This can allow you to be anywhere there is an IM client and do CMD line administration on a server. The downside to any such utility is security there is no real way to make this thing a secure way to do things but as a emergency type tool you might find it very useful. How it works The code works similarly to what's described in my IM bot introduction article please see this for a description of the front end of this code. The major exceptions here are the inclusion of the Wscript file system object and scripting shell. There are other methods of achieving the same thing but I'm a scripter so I like using wscript. You will need to customise these parts of the code with your own connection settings and IM user details Public WithEvents oIMServ As MSIMCliSDKLib.MSIMService The next part of the code is the one that matters which is the OnTextRecieved event, this part of the code checks to see if its a real IM message an not a typing even. To add some sort of security and trigger to this code the next part of the code looks at the IM message that is sent if the first line of the code contains IM Command Shazam then it will take the next line of the IM as the command that you wish to execute. It can handle multiple commands separated by a carriage return (which is a shift enter in the MSN messenger (using just enter send an IM)). The command is executed using the wscript shell method and the results are written to a text file. The text file is then opened, read and returned to the user as an instant message any further commands are also executed and returned. To cope with extended ASCII characters that the messenger clients can deal with but the NT command line can't interpret two mid functions are used to change "" which are in extended ASCII in emails back to the standard 127 ASCII. You may find you have problems with other extended ASCII characters and may need to implement more Mid parsing lines in this case. Private Sub oIMServ_OnTextReceived(ByVal pIMSession As Object, ByVal pContact As Object, ByVal bstrMsgHeader As String, ByVal bstrMsgText As String, pfHandled As Variant)
mhead = Len(bstrMsgHeader)
slen = 1
If fso.FileExists("c:\temp\results.txt") Then
fso.DeleteFile ("c:\temp\results.txt")
End If
If mhead = 123 Then
cword = bstrMsgText
If InStr(cword, "IM Command Shazam") Then
skip1 = 1
Do Until stratend = 1
If skip1 <> 1 Then
If InStr(slen, cword, vbCrLf) Then
elen = InStr(slen, cword, vbCrLf)
elen1 = elen - slen
comex = Mid(cword, slen, elen1)
slen = elen + 1
strMsgHeader = "Mime-Version: 1.0" & vbCrLf & "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf
msgtype = IM_MSG_TYPE_NO_RESULT
comex = Replace(comex, Chr(147), Chr(34))
comex = Replace(comex, Chr(148), Chr(34))
comex = "cmd.exe /C " & comex & " >> c:\temp\results.txt"
strrun = WshShell1.Run(comex, 1, True)
Else
elen = Len(cword) + 1
elen1 = elen - slen
comex = Mid(cword, slen, elen1)
slen = elen + 1
stratend = 1
strMsgHeader = "Mime-Version: 1.0" & vbCrLf & "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf
msgtype = IM_MSG_TYPE_NO_RESULT
comex = Replace(comex, Chr(147), Chr(34))
comex = Replace(comex, Chr(148), Chr(34))
comex = "cmd.exe /C " & comex & " >> c:\temp\results.txt"
strrun = WshShell1.Run(comex, 1, True)
Set nfile = fso.OpenTextFile("c:\temp\results.txt", ForReading, False)
Do Until nfile.AtEndOfStream = True
message = nfile.ReadLine
message1 = message2 & message & vbCrLf
If Len(message1) < 5000 Then
message2 = message1
Else
pContact.SendText strMsgHeader, message2, msgtype
message2 = message
End If
Loop
nfile.Close
pContact.SendText strMsgHeader, message2, msgtype
End If
Else
elen = InStr(slen, cword, vbCrLf)
elen1 = elen - slen
slen = elen + 1
skip1 = 2
End If
Loop
End If
End If
End Sub
That's it once this is done you can compile the code and run it. It you want to run it on a ongoing basis you might want to look at running it as a service using srvany from the NT resource kit or just use the schedule service and run it on machine start-up Using this Bot To use this bot you need to send a multi-line IM message to it, to do this with the MSN messenger client you need to hit Shift-Enter instead of pressing Enter to get a carriage return in you line (press Enter will sent the message). Below is a demo of IM Commands in action
|
Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Stephen Bryant or Pro Exchange. OutlookExchange.Com, Stephen Bryant and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
Copyright Stephen Bryant 2008