LoginSignup
167
129

More than 5 years have passed since last update.

bash で TCP 通信

Last updated at Posted at 2012-02-23

使い道あるかわからないけれど、/dev/tcp/{hostname}/{port} で TCP 通信できる。(※ただし bash に限る?)

http_client.sh
#!/bin/bash
# 使い方
#  $ ./http_client.sh host path
# http://host/path に GET 投げる

exec 3<> /dev/tcp/$1/80
echo -e "GET $2 HTTP/1.0\n\n" >&3
cat <&3

実行してみる。

$ ./http_client.sh qiita.com /
HTTP/1.1 200 OK
Server: nginx/1.0.9
Date: Wed, 22 Feb 2012 01:19:59 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Tue, 01 Nov 2011 15:07:17 GMT
Connection: close
Accept-Ranges: bytes

<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
$ 

ちなみに /dev/udp/{hostname}/{port} で UDP 通信。

167
129
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
167
129