Is there any alternative for netstat command in C#? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am writing a C# program which tells the Active Connections (Protocal, Local Address , Foreign Address , State , Process Id ) . To do this I am running cmd.exe as a process and passing netstat -ano as an argument.
System.Diagnostics.Process.Start("cmd.exe","netstat -ano");
This returns Active connections .
But I don't want to use this netstat command .
Is there any alternative for netstat.exe in C# ?Any library or something else so that I can get the same output ?

You do not need to run nmap or netstat, you can get all the info much easier when you take a look at the System.Net.NetworkInformation namespace. There you can find charming things like GetActiveTcpListeners() or GetActiveTcpConnections() and much more.
public static void ShowActiveTcpConnections()
{
Console.WriteLine("Active TCP Connections");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation c in connections)
{
Console.WriteLine("{0} <==> {1}",
c.LocalEndPoint.ToString(),
c.RemoteEndPoint.ToString());
}
}
Details are here in the docs:
https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipglobalproperties.getactivetcpconnections?view=net-5.0

I was looking for an netstat alternative as you and I found nmap.
To scan I usually run:
nmap -sP 192.168.1.*

Related

Remove execute permission from uploaded folders and files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am working on file and folder upload system and i want to add some security to it.
i have followed this Article , The security point number 6 on it says:
6. Keep tight control of permissions
Any uploaded file will be owned by the web server. But it only needs
read/write permission, not execute permissions. After the file is
downloaded, you could apply additional restrictions if this is
appropriate. Sometimes it can be helpful to remove the execute
permission from directories to prevent the server from enumerating
files.
How to apply that using C#
If I'm understanding you correctly, you want to upload a file to a remote server and then change the file to read only. Here is one option. Start by getting a File Object. After that you can set the access control then supply the access you want to provide.
It might be something like this:
using System.IO;
using System.Security.AccessControl;
private void SetFileAccess(string path)
{
var fileSecurity = new FileSecurity();
var readRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.ReadData, AccessControlType.Allow);
var writeRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.WriteData, AccessControlType.Allow);
var noExecRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.ExecuteFile, AccessControlType.Deny);
fileSecurity.AddAccessRule(readRule);
fileSecurity.AddAccessRule(writeRule);
fileSecurity.AddAccessRule(noExecRule);
File.SetAccessControl(path, fileSecurity);
}
MSDN Link to File
MSDN Link to SetAccessControl Method
MSDN Link to File System Rights

Calling python script from C# (object-oriented approach) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I want to run python script from C#(Visual Studio). I don't want to use C# process to do it, because I must have access to all of the python's modules, classes, and methods (I'd like to treat it as python object). I'm looking for sth like this:
"Python code"
"PyModule"
class PyClass:
def method:
print("Hello world!")
C# code
using PyModule.PyClass
PyClass.method()
I found python.net http://pythonnet.github.io/ , but they say that their solution is unverifiable.
I have to write it in Python 3, so IronPython isn't solution for me.
Do you know any solution that is similar to this?
Well, do have a look into IronPython as suggested in the comments. You can write your python script, save it to, say, greet.py
def greet(name):
return 'Hello ' + name + '!'
Now from C# you can do:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
static void RunPythonScript()
{
var engine = Python.CreateEngine();
dynamic scope = engine.CreateScope();
engine.ExecuteFile("greeting.py", scope);
var greeting = scope.greet("John");
Console.WriteLine(greeting);
}
Result as you might expect is Hello John!

how to automap a network drive and check if drive letter is in use [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm fairly new to WebDAV , how do I go about coding in vb or C# to automap a network drive and check if letter G:\ is available and if it is , assign that letter, if not go to H , I, J ,K drives ?
thank you in advance
You can start a process to add the network drive. Then you can check to see when and how p finishes.
Dim p As Process
p = System.Diagnostics.Process.Start("net.exe", "use r: \\reo\c-drive")
Here is one way to check the availability of a drive letter:
If System.IO.Directory.Exists("c:\") Then ...
You can also use Windows API calls, which are messier to write but don't require a separate process.
It's usually not necessary to map a network drive, however, because you can generally use the network path instead of the mapped drive letter.

Sip parser in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I am looking for a library or class in c# that can parse sip packets.
I need functions that will help me get the
Call-ID field from the packet, types of requests, and basically breakdown the sip packet to its fields.
Does anybody know something that can help me?
Thanks, ofek
This class from my sipsorcery project can do it for you.
Update: If you have a string that contains a full SIP packet you can parse the full thing by using:
var req = SIPSorcery.SIP.SIPRequest.ParseSIPRequest(reqStr);
var headers = req.Header;
var resp = SIPSorcery.SIP.SIPResponse.ParseSIPResponse(respStr);
var headers = resp.Header;
If you don't know whether the SIP packet is a request or a response you can use the SIPMessage class:
var mess = SIPSorcery.SIP.SIPMessage.ParseSIPMessage(messStr, null, null);
var headers = SIPSorcery.SIP.SIPHeader.ParseSIPHeaders(mess.SIPHeaders);
Update 2:
Given you're using pcap.net to capture the SIP packets you are probably ending up with a block of bytes rather than a string. You can use the SIPMessage class to parse the SIP packet from a UDP payload:
var mess = SIPSorcery.SIP.SIPMessage.ParseSIPMessage(packet.Ethernet.IPv4datagram.Udp.Payload, null, null);
var headers = SIPSorcery.SIP.SIPHeader.ParseSIPHeaders(mess.SIPHeaders);

List All Partitions On Disk [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I'm making a utility in C# for a filesystem that isn't supported by windows, which means that I can't just access the drive. I need a way to list all partitions on the hard disk and writing/formatting them.
To list disk partitions you can use WMI.
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskPartition");
foreach (var queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_DiskPartition instance");
Console.WriteLine("Name:{0}", (string)queryObj["Name"]);
Console.WriteLine("Index:{0}", (uint)queryObj["Index"]);
Console.WriteLine("DiskIndex:{0}", (uint)queryObj["DiskIndex"]);
Console.WriteLine("BootPartition:{0}", (bool)queryObj["BootPartition"]);
}
You can use the following approach to get the Volume or DriveLetter on which the partition of disk is mounted.
Win32_LogicalDiskToPartition
Win32_DiskDrive
From the Win32_DiskDrive class you can get the DriveNumber by querying property Index or extracting the DriveNumber from Name attribute. Then query Antecedent and Dependent from Win32_LogicalDiskToPartition. In the Antecedent value you will get the Disk Number and the partition it is trying to map the Volume, after that extract the DriveLetter such as "C:", "D:" etc from the Dependent property. So by using this logic you can get the LogicalDrives mounted on particular HardDisk.
I'm using this logic in my component to get the LogicalDrive names ("C:", "D:" etc) for particular hard drive on my system.

Categories