Click to See Complete Forum and Search --> : Detect remote desktop session on remote computer


nomad314
January 21st, 2009, 10:41 AM
I would like to have one computer look at another computer and determine if there is a remote desktop session active.

Example: want computer B to look at computer A and determine if there is no remote desktop session or if some other computer is remoted in.

Is this possible either through WMI, port scanning, reading the remote registry, etc?

The nice thing is we use a generic administrator account for all computers, so security is not an issue.

toraj58
January 21st, 2009, 11:20 AM
i checked the status of my listening ports in my system before activating remote desktop using this command:

netstat -an |find /i "listening"

status before activating remote desktop:

C:\Documents and Settings\touraj58>netstat -an |find /i "listening
TCP 0.0.0.0:25 0.0.0.0:0 LISTENING
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:1025 0.0.0.0:0 LISTENING
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5051 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5101 0.0.0.0:0 LISTENING
TCP 127.0.0.1:1061 0.0.0.0:0 LISTENING
TCP 195.146.33.146:139 0.0.0.0:0 LISTENING


and status after activating remote desktop:

C:\Documents and Settings\touraj58>netstat -an |find /i "listening
TCP 0.0.0.0:25 0.0.0.0:0 LISTENING
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:1025 0.0.0.0:0 LISTENING
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5051 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5101 0.0.0.0:0 LISTENING
TCP 127.0.0.1:1061 0.0.0.0:0 LISTENING
TCP 195.146.33.146:139 0.0.0.0:0 LISTENING


so the guilty is port 3389...(to be sure several times i turned it on and off).
it is only enough to check that port 3389 is in listening mode in destination PC.

toraj58
January 22nd, 2009, 04:30 PM
now that we know the port is 3389
we can use a simple code like this to check if the remote desktop is ON or OFF:


IPAddress IPtoScan = IPAddress.Parse("192.168.220.55"); // example IP address

try
{
TcpClient Scanner = new TcpClient();
Scanner.Connect(IPtoScan, 3389);
MessageBox.Show("Remote desktop is ON");
}
catch
{
MessageBox.Show("Remote desktop is OFF");
}


don't forget to add these usings to your code:

using System.Net;
using System.Net.Sockets;

nabeelisnabeel
January 23rd, 2009, 06:22 AM
Thanx for the post.

toraj58
January 23rd, 2009, 09:07 AM
your welcome :wave:

kumsaatinet
March 1st, 2009, 04:12 AM
hi,
can you send all source code?
thanks.

toraj58
March 2nd, 2009, 01:47 AM
here is all source code example:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace port_scanner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
IPAddress IPtoScan = IPAddress.Parse("192.168.0.11");

try
{
TcpClient Scanner = new TcpClient();
Scanner.Connect(IPtoScan, 3389);
MessageBox.Show("Remote desktop is ON");
}
catch
{
MessageBox.Show("Remote desktop is OFF");
}

}

}
}


do not forget to add a button to the form.