Powershell을 상사처럼 사용하세요

Powershell을 상사처럼 사용하세요

포트가 열려 있고 연결 가능한지 확인하기 위해 Telnet이 설치되지 않은 경우 사람들은 무엇을 사용합니까? 예를 들어 나는 telnet <destination>텔넷이 상대방의 시스템과 상호 작용할 수 없더라도 의 기술을 사용했고 그것이 거기에 있다는 것을 알고 있었습니다.

Windows 2008에서는 텔넷이 설치되어 있지 않아 약간 당황했습니다. 그럼 대신 무엇을 사용할 수 있나요? 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

cmdlet으로 변환

[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

다음은 텔넷 없이 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

관련 정보