RDP connectivity/responsiveness test - remote-desktop

I want to write an app to test whether a Windows machine is responding correctly to RDP (Remote Desktop) - i.e. to check if the machine not only allows the connection, but is also responding normally, and is not hung or otherwise responding abnormally.
Is there a library or utility that I can use to do this? My searches turned up full RDC clients, but I'm hoping there's something out there at least offers an API for testing. I would most like to use Java or a scripting language to do this, but I'm open to suggestions.

You can find some good answers in this question: Programmatically create and launch and RDP session (without gui)
Because RDP is a constantly evolving proprietary protocol, i'm guessing there isn't some simple open-source code you can take and use. This leaves us with two possible paths to follow:
Use Microsoft RDP ActiveX control (on Windows)
Launch mstsc.exe and send keyboard events to it (also on Windows, using your favorite language)
For the second option, I suggest AutoHotkey. It is perfect for automating windows programs and comes with a powerful library. It also has a strong comunity behind it so you can find lots of useful scripts on the internet. I use it to control winamp (like 'i hate this song! delete it and move to next')(well, technically 'move to the next song and delete the previous' because you can't delete the file while in use but you get the idea). If you choose this path, i can help you with the script.

Found this on Experts Exchange:
use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10, Prompt => '', Port >= 3389);
if($t->open("computer.name.or.ip")) {
print "Connect successful\n";
}
else {
print "Could not connect\n";
}
The idea was to attempt a connection and if it can't connect within 'x' amount of seconds, assume it isn't going to work. Gets a bit more complicated if you're trying to see if a login for a specific user works or not, but this should at least get you started.
NOTE: As pointed out in the comments, the original solution left out the RDP port, so I included that in this...

Related

Way to Refresh Network Drives due to Disconnection from Pending VPN Connection without Workflow Interruption

Most of my company is currently working from home and is having to tunnel into our office network through a VPN. As such, all our network drives are, technically, disconnected until we login to the VPN. Once connected, we could simply open a file explorer window and open each network drive to reestablish/refresh the connection in Windows' eyes, but more than 2 or 3 drives is a hassle, and some of our members don't even think to do this every day even for our main project drive (only universal drive letter we use in our office) before opening some of our programs that need a network drive mapped, and thus have the potential to mess up our files or link therein.
Desired Solution:
I'm looking for a batch or cmd file that can run to refresh or reconnect the drives, without explicitly using NET USE if possible (more on why further down) or opening a file explorer by using a batch file to open a shortcut to a network folder. If this just isn't possible, I'll stick with the solution I have that uses NET USE, but would appreciate help smoothing out the issues outlined below.
I have a couple solutions that are more than a little intrusive in that they require some form of interaction to dismiss them once launched, rather than running, reaching eof, then closing using TIMEOUT /T 5. I was hoping there might be something a little more streamlined, or at least less intrusive, that I could use to avoid these unnecessary interactions that I'm currently dealing with:
Shortcut to a folder on each drive to "ping" said drive for refresh, but this opens a file explorer window, potentially interrupting workflow. I would like to suppress this window opening if possible, or at least immediately close it (not exactly how file explorer works, I know).
Batch file that tests all mapped drives for disconnected ones, then attempts to reconnect, sometimes causing a user input in the cmd window to interrupt workflow (We have two security levels each with their own login credentials, and several of us have mapped network drives using each of the two creds, so some disconnected drives will require manual entry of username and password when the /USER:%username% %password% parameters don't work, which I just can't seem to avoid. Neither server nor network location for NET USE are viable tests for which security level, as their labeled after the project itself rather than the security level needed, and so my only remedy here would be to suppress the asking for user input).
Multiple batch files, one for each drive, requiring users to manually copy and alter batch files for their own needs (only one drive is universally used for each project we're on, and even then some people are one different projects), then updating the task scheduler to run each of their created files (not at all desirable, and as far as I'm concerned, unacceptable). If nothing else can be modified or work without interference, I can just using this to run on our company's default project drive letter, and let people worry about reconnecting the others as needed.
I have a task scheduled for any time our VPN client reports a connection, which due to our home networks and/or the connection between us and the VPN servers, could occur multiple times a day (I've had up to 4 or 5 "connection" events trigger my task after the initial login, though I only have to explicitly login the one time), which can cause further interruptions in workflow beyond just the initial VPN login run. I'm willing to accept any solution that uses NET USE as long as I can suppress the request for user input if the Windows login password doesn't work, or a shortcut if I can prevent the opening of a file explorer window. Alternative solutions are preferred, but I understand that may not be possible.
I have tested using DIR //server/foo, but it timed out for the same reasons that file explorer doesn't display the network drive as "connected" after VPN login is successful.
I've also had mixed results with setting the task scheduler to run my program off of a network connection as opposed to the "connect" event from our VPN client, mostly in that it didn't run reliably, if at all, as I needed to wait until I was connected through the VPN, not just standard internet connection through an ethernet cable. For this, I tried using both methods here, but none of the networks that are available after VPN login seemed to trigger the task properly, even though manually triggering the even through Task Scheduler showed the action would run fine. As such, I'm considering this as a failure, but can revisit if no more streamlined solutions are available or I can get a guarantee this method won't trigger as often (preferably only once per login) as watching the VPN client for its connection event.
At this point, I'm stuck with two solutions that have a high potential to interrupt workflow and annoy the user too greatly for me to want to send out as a final solution (a for loop on NET USE where drive is disconnected and a batch file to open a shortcut that opens a window front and center), and another solution that I consider to limited in scope and as a last resort. I'll make due if there's not a better way to handle either of the first two, but in the mean time, I would greatly appreciate any help/advice!
Edit:
On a side note, I do expect to have the cmd window pop-up briefly, then close automatically once the operations are done (like it would with the shortcut solution), so some amount of workflow interruption will likely be unavoidable. What I'm considering unacceptable is that I can't minimize it and prevent the user form having to interact with it directly or indirectly to get the cmd or resulting window to close after it takes the active window status from whatever program user was previously in.
Edit 2:
I've posted this query elsewhere with this additional recap to help narrow my request:
As a recap, I'm looking for a solution for refreshing network drive connections that do no rely on Credential Manager, will not require user input if network drive credentials differ from windows login credentials, and will not pop-up a window that requires user to close manually. Brief workflow interruptions resulting from cmd or child-process windows popping up before closing automatically are acceptable, with a preference towards one of the two solution avenues I already have running. Thanks again for any input you may have!
I had the same problem. I have 4 mapped drives to reconnect to when I open the VPN and it was a real pain. I have looked for batch file solutions in vain.
My solution is to use a free program called FBackup. I use it anyway for the intended purpose of backing things up, but I noticed that when it opens, it reconnects all of my drives (including the mapped drives using the VPN to the office)
So after I connect, I open FBACKUP. It makes all of the connections for me, then I just shut it down. Job done.
1 this probably belongs on the site that this site was made to head up against, which I am unsure if I am allowed to reference by name on this site.
That said if it was at this site you probably want topost on Superuser.
That said..
For users with more than one drive mapping you want to store thier credentials used in windows credential manager so that the drives can automatically be mapped without specifying a username and password each time.
That will allow your batch script to run in the background and re-map the drives
In lack of a better answer that is viable for our setup (will definitely remember Ben's answer for later, in case I'm able to use it for the lower security profiles), I've decided to go with a more limited scope for my batch file, since I have yet to find any info that will allow me to suppress a file explorer window or request for user input from the cmd window (for password) when attempting to refresh connection to all saved network drives.
Since the one drive that absolutely must be reconnected is, by our guidelines, universally assigned to the same letter (mapping changes depending on the primary project we're on), I've opted for just refreshing this drive as the credentials for this drive will always match the Windows login credentials, and thus won't request user for input if a password is wrong.
I will keep tabs on this question, so that if someone else comes along and has a better solution, I will definitely swap the selected answer to them.

Remote Desktop Application - Start multiple sessions on my client PC

I have a windows 2016 server running RDS. It is configured to run a single application when a user accesses it from a .rdp shortcut. The user just sees the application as though it is running on their local machine, not a whole remote desktop.
If from a client PC, if I launch the application and log in then I can see a new connection in the RD Gateway manager.
However if I launch the application from the same client PC multiple times I am only asked to login the first time, and whilst I see many instances of the application I only see one connection in the RD gateway manager.
I would like a new connection each time I launch the application, so that I can simulate multiple users running the app concurrently in their own sessions.
I have tried adding "prompt for credentials on client:i:1" to the .rdp shortcut and this has no effect.
Thanks you your help.
I don't think it's possible. Let me try to explain:
Whenever you launch the RDP file, you connect to a session on the RDSH. Now, if you want separate connections, that means that there should be multiple sessions (probably for the same user since the credentials don't change) which is not allowed by the RDSH by default.
Assuming you get passed multiple sessions for same user, MSTSC actually recognizes that for the given RDP file, there is already an open connection, so it will try to reuse that.
Last but not least, there is the problem of licensing - you are "working around" the system: both per-device and per-user licensing would report 1, therefore it would probably be a violation of ToS.
It would be much more useful if you could tell us what you are trying to achieve and then maybe we can help you more.
P.S.: This question has nothing to do with programming, so it probably belongs to https://superuser.com
Hi I figured out a solution to this, so I will share in case it is useful for anyone else.
Modify the hosts file to include different names for the target machine ip address:
eg:
51.xxx.159.233 test1.xxx.uk-user1
51.xxx.159.233 test1.xxx.uk-user2
51.xxx.159.233 test1.xxx.uk-user3
51.xxx.159.233 test1.xxx.uk-user4
Then in each rdp launch shortcut, reference a different alias.
eg:
Shortcut1:
full address:s:test1.xxx.uk-user4
Shortcut2:
full address:s:test1.xxx.uk-user3
Then when the RDP is launched it will ask you to log in even if you already have an active connection and server will have a new user logged into it.
Hope this helps somone in the future.

Avoiding all system messages and messages from other software

Here is the situation. The company I work for builds this piece of software in c that can make a Windows computer act a bit like a TV. Essentially, our piece of software is meant to be played full screen and content is displayed from the internet without the user having to ever touch the computer again.
The problem is that once in a while, the system brings up pop-ups like "Your Windows system is ready for an upgrade." or "Please renew your Norton subscription" etc. which the user has to periodically and manually remove.
Is there a way to display content full screen without being bothered by those warnings?
Yah, whether or not the development community agrees, Microsoft has several standards for when and why it might be acceptable to have exclusive use of the monitor.
The most official strategy is to use DirectX in exclusive mode. This is what games do, what windows media player does in full screen video with hardware acceleration enabled, etc... If your application is multimedia intensive (as suggested by TV like functionality), you should probably be using DirectX too. Besides giving you the exclusive display access it will also increase your applications performance while lowering the CPU load (as it will overload graphics work to the video card when possible).
If DirectX is not an option, there are a great number of hacks available that seem to all behave differently between various generations of windows operating systems. So you might have to be prepared to implement several techniques to cover each OS you plan to support.
One technique is to set your application as the currently running screensaver. A screensaver if really just an EXE renamed to SCR with certain command line switches it should support. But you can write your own application to be such a screensaver and a little launcher stub that sets it as the screensaver and launches it. Upon exit the application should return the original screensaver settings (perhaps the launcher waits for the process to exit so that it returns the settings in both graceful exits and any unplanned process terminations ie: app crash). I'm not sure if this behavior is consistent across platforms though, you'll have to test it.
Preventing other applications from creating window handles is truly a hack in my opinion and pretty bad one that I wouldn't appreciate as a customer of such software.
A constant BringWindowToTop() call to keep you in front is better (it doesn't break other software) but still a little hack-ish.
Catch window creation messages with a global hook. This way you can close or hide unwanted windows before they become visible.
EDIT: If you definitely want to avoid hooks, then you can call a function periodically, which puts your window to the top of the z-stack.
You could disable system updates http://support.microsoft.com/kb/901037 and remove the norton malware.
You could also connect a second screen so that the bubbles appear in the the first monitor.
Or you rewrite it for linux or windows ce.
One final option is to install software that reconfigures your os into a kiosk http://shop.inteset.com/Products/9-securelockdown.aspx
If you don't need keyboard or mouse input, how about running your application as a screensaver?
A lot of thoses messages are trigged/managed by Windows Explorer.
Just replace it with your dummy c#/winform.
By changing the registry value
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"Shell"="Explorer.exe"
You can specify virtually any exe as an alternative to explorer.exe
That's the way all windows based (embedded) system (ATM & co) do.
There's still few adjustment (disable services you dont need / dr watson & others), and of course, you'll want to keep a "restart explorer.exe" backdoor.
But that's a good start

Remote Desktop Project in C

I want to make project for my final year in college.
So someone suggested me to make Remote Desktop in C.
Now I know basic socket functions for windows in C i.e. I know how to make
echo server in C.
But I don't know what to do next. I searched on internet but couldn't find
something informative.
Could someone suggest me how to approach from this point..any tutorial...or any source ?
I think this is do-able. For a college project, you don't need to have something as complex and as full-featured as VNC. Even demonstrating simple keyboard and mouse control and screen feedback would be enough, in my opinion, and that's well within reach.
If you're doing everything from scratch and using Win32, you can get the remote screen using the regular "printscreen" example all around the internet.
http://www.codeproject.com/KB/cpp/Screen_Capture__Win32_.aspx has it, for one. You can then compress the image with a third-party library, or just send it raw; this wouldn't be very efficient but it would still be a viable demonstration.
Apart from capturing the screen data remotely and showing it in the local window, you'll need to listen for local window messages for mouse and keyboard events, send them to the remote host, and then play them back. http://msdn.microsoft.com/en-us/library/ms646310%28VS.85%29.aspx will probably do that for you.
Check tightvnc TightVNC is a free remote control software package. The source code is also available.
For sending the image of the screen I would probably use rtp. The JRTPLIB is really handy for that.
And yes, as KevinDTimm says, an echo server is the very easiest part.
KevinDTimm may well be right, writing an RDP client would a fairly significant undertaking. To give you some idea, the current spec, available at the top of this page, is 419 pages long and includes references to several additional documents for specific aspects of RDP like Audio Redirection and Clipboards.

WinAPI C - RunAsUser from REDMON_USER and REDMON_SESSIONID

I installed a PostScript printer driver and have setup REDMON (redmonnt.dll) for redirecting postscript output to my program. In my rather simple c program I capture the data from STDIN and I am able to successfully save it into a .ps file. The file looks OK.
However, I want to start gsview.exe for viewing the file. If I call ShellExecute it fails in Windows 7 because of permission issues. It seems that my program is called under a different user account (LOCAL SERVICE). So I am looking for a way to run gsview.exe under a specific username (the user who initiated the print job) which is available to the program in a variable called REDMON_USER along with the SESSIONID as well.
Q: What are the minimum WinAPI calls required to start a program given a username and a sessionid?
Any code examples in C/C++, .NET would be very helpful.
EDIT: What I am trying accomplish is something very similar to redrunee (from redmonee). I don't want to use redrunee because it opens about a console window for a brief moment.
Note:
1) The program is called by the printer service as [LOCAL SERVICE] account.
2) The first parameter Username (REDMON_USER), in effect, points to the user currently looking at the screen
Look at CreateProcessAsUser.
Also look at CreateProcessWithLogonW and CreateProcess.
They are linked from the CreateProcessAsUser
EDIT In reply to comments by OP.
Follow advice from this thread.
I am copying this here verbatim, in case the original link stops working:
The same code works for us on Vista as
on XP, etc. The service is running as
the Local System.
use WTSGetActiveConsoleSessionId to get the ID of the current active
Windows session at the console (i.e.
the machine keyboard and display, as
opposed to WTS sessions).
use WTSQueryUserToken to get the token for that session.
use DuplicateTokenEx(hToken,MAXIMUM_ALLOWED,NULL,SecurityIdentification,TokenPrimary,
&hTokenDup) to duplicate that token.
use CreateEnvironmentBlock to create an environment that you will be
passing to the process.
use CreateProcessAsUser with the duplicated token and the created
environment. Actually, we use
CreateProcessAsUserW, since the A
version had some sort of bug on some
older systems.
Don't forget to CloseHandle on the various tokens, etc, and to
DestroyEnvironmentBlock the
environment.
Thank you efratian.
PS. Oh joy of Windows programming, did not do it for quite a while. Now I remember why. The only thing that is close or even worse documented is OpenSSH programming.
The documentation describes the "Run as User" feature, which seems to be exactly what you want:
Run as User is intended for launching a GUI program such as GSview locally via RedRun.

Resources