Run a Windows command line from ASP Classic and get results - asp-classic

I'm trying to improve security on an old ASP Classic site. I need to generate (truly) random numbers, and ASP itself doesn't seem to have that capability directly. So it occurred to me that I might run a Windows CLI script and use the results.
I've figured out how to run a script, but not how to return the results of that script to ASP.
Dim oShell, sCommand
sCommand = "C:\scripts\myscript.bat /foo"
Set oShell = Server.CreateObject("WScript.Shell")
Set oExec = oShell.exec(sCommand)
...?
Set oShell = Nothing
Can anybody help me with that last piece of the puzzle.
(Or... if you know a better way to generate truly random numbers in ASP Classic, that would be excellent)

Make the output of command redirected to text file. For example "dir >test.txt". Then read the file.

Related

Getting the session data from ASP classic isn't working

I am currently tasked with moving one of our older bits of software to a new server. The old server is running 2008 and the new server is on 2019. The code is a mixture of ASP and ASP.NET, both using VB. Unfortunately, I'm a C# developer and my VB knowledge is slight.
The main bit of the code is the older and is all in ASP and works fine on the new server. For a particular set of customers there is an add-on that is more recent and uses ASP.NET. For the new section of code to get the details of the logged in user it uses the code given in this answer. Unfortunately it seems like it is this bit of code that is failing.
We have this bit of code in our site.master.vb
ctx1 = ctx.Request.Url.Scheme
ctx2 = ctx.Request.Url.Host
dom = ctx1 + "://" + ctx2
Dim ASPRequest As HttpWebRequest = WebRequest.Create(New Uri(dom + "/arc/asp2netbridge.asp?sessVar=" + sessionValue))
ASPRequest.ContentType = "text/html"
ASPRequest.Credentials = CredentialCache.DefaultCredentials
If (ASPRequest.CookieContainer Is Nothing) Then
ASPRequest.CookieContainer = New CookieContainer()
End If
The asp2netbridge.asp file is stored in the directory one up from the directory that contains the code and the directory structure looks the same on both servers. The contents of the as2netbridge file are the the same as in the example code linked above with the addition of some extra comments.
It then calls a Stored Procedure on our database with the customer ID from session that should return the customer details as XML, but instead we get a 'Root Element is missing' Error. If I change the Stored Procedure to hard code the customer ID in it, rather than as a parameter then it works as expected.
Is there anything that I need to install on our server to get the system working correctly? Or is there anything else I need to do to get it to work?

Is web automation with Openoffice BASIC even possible?

I'm trying to do some simple webscraping in OpenOffice (I usually work in Excel but I'm trying to port something over for a coworker that doesn't have Excel). However, when I try to run something very similar to this, it keeps giving me this BASIC runtime error 1.
Here's the relevant code, I'm not sure what I'm supposed to do to make this work.
Sub Macro1
Dim explorer As Object
Set explorer = CreateObject("InternetExplorer.Application")
explorer.Visible = True
explorer.navigate("www.yahoo.com")
Const READYSTATE_COMPLETE As Long = 4
Do While explorer.Busy Or explorer.readyState <> READYSTATE_COMPLETE
Loop
dim page as object
set page = explorer.Document
dim mailButton as object
set mailButton = page.GetElementByID("ybar-navigation-item-mail") 'this is the line the error occurs on
mailButton.Click
End Sub
Do you know that you can save script in vbs file (you have to delete types in variables declarations) and run it directly by double click without using office application? I recommend you to use this way.

Visual Basic: Uploading a file using HTTP

I'm a PHP / Javascript developer having to build a "simple" script that will run on a windows desktop that transfers a scanned image to a server. By using a .vbs script I have been able to do everything I need to do except send the file to the server. I have spent the last two days googling and trying examples, but nothing seems to work. Below is the code I'm currently fighting with.
Dim objShell,UserPath,sourcePath,strURL,stream, file, fileString, HTTPSevice, body
Set objshell = CreateObject("WScript.shell")
UserPath = objShell.ExpandEnvironmentStrings("%UserProfile%")
strURL = "www.notWebsite.com/uploader.php"
sourcePath = UserPath & "\Desktop\Scanned\scanned.jpg"
set stream = CreateObject( "Scripting.FileSystemObject" )
set file = stream.getfile(sourcePath)
set fileString = file.openAsTextstream
body = fileString.readAll
fileString.close
set xmlHttp = createobject("MSXML2.XMLHTTP.4.0")
xmlhttp.open "POST",strURL,false
xmlhttp.setRequestHeader "Content-Type", "multipart/form-data"
xmlhttp.send body
msgbox xmlhttp.responseText
The code doesn't throw an error, it just sends an empty request to the server.
Like I said before, I have no experience with developing for/on/near windows, so if a visual basic script isn't the right option, please let me know and point me in the right direction. I'm willing to learn how to make this work, but I'm just stuck on how to move forward.
Thank you in advanced for your help.
Using PowerShell is 100% the way to go for my usecase. Even though I've never heard of it before this morning, I already have the script working and ready for testing.

classic asp testbed for quick function/unit tests

Is there any kind of online "quick" asp/vbscript test tool? It's a pain to load up a test page, put all the stuff in, when I just want to test some ASP VBScript.
Yes, they are called .vbs files. These files are plain text files with a .vbs extenstion. You can run them from the command line. There are some minor differences, just google vbs or WSCript.
One of the most annoying issues is you cannot use response.write instead you use WScript.Echo. This displays the result in a windows style popup messagebox. So, if you use it in a loop to display values, you will have to close the box once for each value that is displayed. Can be a pain. I just contcatonate the results and display one message.
For example save the below code as myloop.vbs
Dim strMsg
Dim i
strMsg = ""
for i = 1 to 10
strMsg = strMsg & CStr(i) & vbCrLf
next
WScript.Echo strMsg
Double-click on myloop.vbs and it runs.
This is a pretty cool way to test out a concept without having to create an asp page and load it on the server.
I realize this isn't a very good/correct answer, but I typically use VB6 (Visual Studio 6) as a quick interactive debuggable VBScript-equivalent testbed. Some syntax is different (CreateObject), and you need to add certain references to your "Default" testing project, but it does allow me to answer any question about type conversions etc within a few seconds (VB6 startup time + type/paste time + F5) rather than the few minutes it will take me to set up an ASP page, and even worse start up ASP debugging, which as of VS2008 and Windows 2003 is just a huge pain.

Conditional includes in Classic ASP - where the file may not exist on the server

I am currently in a situation where I have to make some additions to an application written in classic ASP using server-side JScript on IIS.
The additions that I need to make involve adding a series of includes to the server-side code to extend the application's capabilities. However, the inc files may not exist on the server in all cases, so I need the application to fall back to the existing behavior (ignore the includes) if the files do not exist, rather than generating an error.
I know that this can't be accomplished using if statements in the JScript code because of the way that SSI works, and have not come across any ways of dynamically including the code on the server side, where the files may not exist.
Does anyone know of a way to accomplish this in classic ASP? Any help would be much appreciated.
Here's a script to dynamically include asp files:
<%
' **** Dynamic ASP include v.2
function fixInclude(content)
out=""
if instr(content,"#include ")>0 then
response.write "Error: include directive not permitted!"
response.end
end if
content=replace(content,"<"&"%=","<"&"%response.write ")
pos1=instr(content,"<%")
pos2=instr(content,"%"& ">")
if pos1>0 then
before= mid(content,1,pos1-1)
before=replace(before,"""","""""")
before=replace(before,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
before=vbcrlf & "response.write """ & before & """" &vbcrlf
middle= mid(content,pos1+2,(pos2-pos1-2))
after=mid(content,pos2+2,len(content))
out=before & middle & fixInclude(after)
else
content=replace(content,"""","""""")
content=replace(content,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
out=vbcrlf & "response.write """ & content &""""
end if
fixInclude=out
end function
Function getMappedFileAsString(byVal strFilename)
Dim fso,td
Set fso = Server.CreateObject("Scripting.FilesystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(strFilename), 1)
getMappedFileAsString = ts.ReadAll
ts.close
Set ts = nothing
Set fso = Nothing
End Function
execute (fixInclude(getMappedFileAsString("included.asp")))
%>
The last line (the one starting with "execute") is equivalent to an "include" directive, with the difference that it can be included inside an "if" statement (dynamic include).
Bye
If you are really brave, you can read the contents of the file and then Eval() it.
But you will have not real indication of line numbers if anything goes wrong in the included code.
As a potentially better alternative: Can you not create some sanity check code in global.asa to create the include files as blanks if they do not exist?
Put simply, no. Why would the files not exist? Can you not at least have empty files present?
What you could do is something like this:
Use Scripting.FileSystemObject to detect the presence of the files
Use Server.Exeecute to "include" the files, or at least execute the code.
The only problem is that the files cannot share normal program scope variables.
The solution to this turned out to be to use thomask's suggestion to include the files and to set a session variable with a reference to "me" as per http://www.aspmessageboard.com/showthread.php?t=229532 to allow me to have access to the regular program scope variables.
(I've registered because of this, but can't seem to associate my registered account with my unregistered account)

Resources