I do not have telnet command in my system.
However my system is installed with Windows 10, so there must be a way to check whether particular port is open or not in a remote system. That particular remote system is accessible I had checked with ping command.
So here is my simple question,- how to check whether particular port is open or not using powershell.
Command netstat could brief for local system service & port and particular protocol either UDP or TCP is up & runnning. As I do not have telnet I need this to be sorted out and tackled by powershell. Any advise and suggestion are welcome.
Test-NetConnection ###.###.###.### -Port ##
You can use the following to try and open a port, if no error is returned the port is open:
$ipaddress = ""
$port = ""
$tcpClient = new-object Net.Sockets.TcpClient
$tcpClient.Connect("$ipaddress", $Port)
$tcpClient.Dispose()
Here's a more complete example, which returns true/false which is the way that Test-Path works:
function Test-Port
{
param
(
$Address,
$Port
)
$tcpClient = new-object Net.Sockets.TcpClient
try
{
$tcpClient.Connect("$Address", $Port)
$true
}
catch
{
$false
}
finally
{
$tcpClient.Dispose()
}
}
Test-Port -Address localhost -Port 80
Test-Port -Address localhost -Port 81
Depending on the version of Powershell/Windows you are using Test-NetConnection may be more appropriate.
Related
I would like to run an ftp server on a nixos host. I am using vsftpd, though could use something else if that would make a difference.
The ftp works fine on localhost, but the firewall is blocking me for remote usage. I have allowed TCP port 21, but that is not enough.
How should I configure the firewall to allow ftp connections (including writing to the ftp server)?
Here is the code that I currently have:
{
networking.firewall = { allowedTCPPorts = [ 20 21 ];
# connectionTrackingModules = [ "ftp" ];
};
services.vsftpd = {
enable = true;
# cannot chroot && write
# chrootlocalUser = true;
writeEnable = true;
localUsers = true;
userlist = [ "martyn" "cam" ];
userlistEnable = true;
};
}
With the above, any use of ftp from off-host fails:
ftp> put dead.letter
200 PORT command successful. Consider using PASV.
425 Failed to establish connection.
Use of passive mode (e.g., with ftp -p) doesn't seem to help here:
ftp> put dead.letter
227 Entering Passive Mode (192,168,0,7,219,202).
ftp: connect: Connection timed out
Testing on a throwaway host with the firewall disabled
networking.firewall.enable = false;
Allows ftp -p to work; though of course turning off the firewall is not an attractive option.
Thanks for any help and pointers,
In passive mode the client will connect to the server with a second connection, that is used to transfer "things" (directory listings, files). In your case:
227 Entering Passive Mode (192,168,0,7,219,202)
The server requested the client to connect to it on port 219 * 256 + 202 = 56266.
This port is choosen by vsftpd dynamically and is not open in your firewall. You have to fix vsftpd to a fixed port for the passive connection and open this connection in the firewall.
vsftpd has two configuration options to set this: pasv_max_port and pasv_min_port. You should be able to set them in services.vsftpd.extraConfig. You probably want to open a small range of ports and open these in the firewall.
To open ports in the firewall, use networking.firewall.allowedTCPPorts. For example:
networking.firewall.allowedTCPPorts = [ 21 ];
services.vsftpd.extraConfig = ''
pasv_enable=Yes
pasv_min_port=51000
pasv_max_port=51999
'';
networking.firewall.allowedTCPPortRanges = [ { from = 51000; to = 51999; } ];
Firewall configuration is not automatic in NixOS, because that would defeat the purpose of having control over what traffic is allowed.
Some services have an openFirewall option to make this easier, but the vsftpd module does not seem to provide this convenience.
Edit: 20 is for the client. Only 21 needs to be opened.
Edit: Plus a range for passive mode connections.
I am trying to write a script which will take in a server name and a port and checks if that port is open. Not sure how to do this is VBS. I've scoured the internet and have found nothing so far.
First Download PortQry Command Line Port Scanner Version 2.0
from Microsoft.com and extract it..
Then define some variable as below:
strPortQry = "C:\PortQryV2\PortQry.exe"
strServer = "127.0.0.1"
intPortNo = 8080
And Use the PortQry utility to find a port listen or open with this code:
PortQry.exe -n 127.0.0.1 -e 80
Something like :
objShell.Exec(objFSO.GetFile(strPortQry).ShortPath & " -n " & strServer & " -e " & intPortNo)
You can loop on port and chech any port you want. For more information please check Microsoft help for PortQry utility and check This Site may be helpfull.
i want to implement the following logic. Does it possible to have such implementation using batch or power shell ? please share with me script for that.
Let say I have a configuration file with the following "config.propertis":
BOOTPRORO=statis or dhcp
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS=8.8.8.8
I want that at startup the system will check that file and configure network accordingly:
OS: Windows
if in BOOTPROTO=dhcp, when use DHCP in network configuration and ignore all another in config file, except DNS
if in BOOTPROTO=static, then use all variables from config file to configure IP as static.
So, I have such logic under the Linus, using shell. The script in configured in rc.d and execute before network service. Does it possible to implement such over the Windows ? Guys, please share the script !
We can definitely do this.
First things first, because a lot of systems have more than one network interface, you'll need to determine what the ifIndex is of the adapter that we want to change. Do that by running Get-NetIPInterface. You should see results like this:
In my example and going forward, I'll be using this index, 41. You should change this to match what you find on your own computer.
OK, now to read from the text file. Since you've provided the data in a key=value pair format, commonly called a hashtable, we can easily grab the data from there using ConvertFrom-Stringdata. This will give us a PowerShell hashtable, and we can pull the needed line out like this.
$values = get-content T:\config.properties | ConvertFrom-StringData
$values.BootProro
>statis
We can us this to set the PC in Dynamic IP mode, or to set static addresses. Now, for you to use this in your environment, you need to find the ifIndex, as I mentioned before. replace my index of 41 with your own, and then give it a shot. I've added -WhatIf to every line, so you will see what would happen when you run it. If you're happy with the changes it woudl make, remove -Whatif to make the script actually change the settings.
$values = gc T:\config.properties | ConvertFrom-StringData
if ($values.BOOTPRORO -eq "dhcp"){
Write-Output "---DHCP mode detected in 'config.properties' file"
Write-Output "---Setting Set-NetAdapter -DHCP Enabled"
Set-NetIPInterface –InterfaceIndex 41 –Dhcp Enabled -WhatIf
}
else{
Write-outPut "---static mode detected in 'config.properties' file"
Write-Output "---Removing network configuration"
Remove-NetIPAddress -InterfaceIndex 41 -whatif
Write-Output "---Setting new network configuration equal to"
$values
New-NetIPAddress -DefaultGateway $values.GATEWAY -IPAddress $values.IPADDR -PrefixLength 24 -InterfaceIndex 41 -WhatIf
Set-DnsClientServerAddress -ServerAddresses $values.DNS -InterfaceIndex 41 -WhatIf
}
The output looks like this:
in Windows we can set ip address via batch file or powershell script but when you use dhcp address your ip is Dynamic not static I Imposition you want static ip address
BAtch-file
netsh interface ip set address name=”Local Area Connection” static 192.168.10.10 255.255.255.0 192.168.10.1
netsh interface ip set dns name=”Local Area Connection” static 8.8.8.8
if you want be dhcp You should set
netsh interface ip set address name=”Local Area Connection” source=dhcp
note I Imposition you nic name is Local Area Connection
In powershell V3.0 and Later we Used
New-NetIPAddress –InterfaceAlias “Local Area Connection ” –IPv4Address “192.168.10.10” –PrefixLength 24 -DefaultGateway 192.168.10.1
Set-DnsClientServerAddress -InterfaceAlias “Local Area Connection” -ServerAddresses 8.8.8.8
and for startup you can put script .bat and .ps1 in startup windows but attention you should Set-ExecutionPolicy bypass before U run any script of powershell
for startup any script see link
I have 3 machines(A, B & C) connected to a Router. A,B & C are in same subnet. All these three machines are interconnected using STAF. I am using machine A as an FTP server & machine B as an FTP client. Using STAF command from machine C I am starting FTP program (TCL script) on machine B.
Now the question is, How C will know whether FTP traffic is flowing between A & B?
The ftp package allows you to specify a progress monitor callback in the ftp::Open command:
package require ftp
proc progressMessage {bytesSoFar} {
puts "Transferred $bytesSoFar; looking good..."
}
set handle [ftp::Open $A $user $pass -progress progressMessage]
# Everything after this is just standard for the ftp package
if {$handle < 0} {
error "could not connect"
}
if {![ftp::Get $handle $remoteFile $localFile]} {
ftp::Close $handle
error "could not transfer"
}
ftp::Close $handle
puts "Transfer completed"
This will print a message every time a chunk is transferred (the chunk size is configurable in the options to ftp::Open via the -blocksize option; it's 4096 by default). On modern networks, this is probably going to write messages very rapidly…
package require ftp
set handle [::ftp::Open $host $user $passwd]
if {$handle < 0} {
error "Connection refused!"
return 0
}
here is the code you can find every where on net
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("Echo server\r\n");
socket.pipe(socket);
});
server.listen(1337, "127.0.0.1");
A simple tcp server will echo whatever you will send it. How to send data to it? What tools/commands I need in mac to test this server?
Use nc aka netcat. In Terminal.app, while your node app is running:
$ nc localhost 1337
Echo server
Ta-da!