Powershell を上手に使いこなす

Powershell を上手に使いこなす

Telnet がインストールされていない場合、ポートが開いていて到達可能であることを確認するために何を使用しますか? たとえば、私は の手法を使用していましたがtelnet <destination>、Telnet が反対側のシステムと対話できなかったとしても、ポートが存在することはわかっていました。

Windows 2008 では telnet がインストールされていないため、少し困惑しています。代わりに何を使えばよいのでしょうか。また、Linux や Solaris にない場合は何か教えてください。

私はさまざまなサイトで仕事をしているコンサルタントです。さまざまな理由 (アクセス権、変更管理時間、インストールすると翌年誰かがそれを使用するという責任が生じるなど) により、他の人のサーバーにインストールすることはできません。しかし、USB またはその他の自己完結型の非インストール ツールがあればすばらしいでしょう...

答え1

Powershell を上手に使いこなす


基本コード

$ipaddress = "4.2.2.1"
$port = 53
$connection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port)

if ($connection.Connected) {
    Write-Host "Success"
}
else {
    Write-Host "Failed"
}

一発ギャグ

PS C:\> test-netconnection -ComputerName 4.2.2.1 -Port 53

コマンドレットに変換する

[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$ip,
    
   [Parameter(Mandatory=$True,Position=2)]
   [int]$port
)

$connection = New-Object System.Net.Sockets.TcpClient($ip, $port)
if ($connection.Connected) {
    Return "Connection Success"
}
else {
    Return "Connection Failed"
}

スクリプトとして保存していつでも使用可能

次に、PowerShell または cmd プロンプトで次のようにコマンドを使用します。

PS C:\> telnet.ps1 -ip 8.8.8.8 -port 53

または

PS C:\> telnet.ps1 8.8.8.8 53

答え2

ここでは、Telnet を使用せずに TCP ポートをテストするさまざまな方法を紹介します。

バッシュマニュアルページ

# cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_5.3
^C

# cat < /dev/tcp/127.0.0.1/23
bash: connect: Connection refused
bash: /dev/tcp/127.0.0.1/23: Connection refused


カール

# curl -v telnet://127.0.0.1:22
* About to connect() to 127.0.0.1 port 22 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_5.3
^C

# curl -v telnet://127.0.0.1:23
* About to connect() to 127.0.0.1 port 23 (#0)
*   Trying 127.0.0.1... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host


パイソン

# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 22))
>>> clientsocket.send('\n')
1
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 23))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused


パール

# perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
  PeerHost => '127.0.0.1',
  PeerPort => '22',
  Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
^D
connected to the server

関連情報