さて、telnetとはTCP/IPネットワークにおいてネットワークにつながれた
コンピュータを遠隔操作するためものでそのために使用されるプロトコル。
telnetサービスを起動しているコンピュータ(サーバ)にログオンして目の前にいるのと
同じように操作することができるものです。
という事でTCP/IPのクライアントを書いて
ip_addr = 192.168.0.1;
ip_port = 23;
sockfd_ip = socket(AF_INET, SOCK_STREAM, 0);
address_ip.sin_family = AF_INET;
address_ip.sin_addr.s_addr = inet_addr(ip_addr);
address_ip.sin_port = htons(atoi(ip_port));
len_ip = sizeof(address_ip);
result_ip = connect(sockfd_ip, (struct sockaddr *)&address_ip, len_ip);
if(result_ip == -1) {
perror("rncannot connect");
fprintf(stderr,"r%s:%srn",ip_addr,ip_port);
end_process();
}
fp2 = sockfd_ip;
if (read(fp2, &ch, 1) > 0) {
・
・
後はファイルポインタfp2をread/writeすればいいだけのように思いますが
ここまでは、あくまでTCP/IPのクライアント部分だけです
スイッチやルータなどは簡易telnetを実装していることが多い為、TCP/IPのクライアントのみでも充分ログイン可能です
しかし、Linux、FreeBSD、WindowsServerサーバなどのtelnetクライアントはこの後、複雑なtelnetプロトコルをしゃべらす必要があります
詳しくは下記URLを参照して貰いたいが
http://support.microsoft.com/kb/231866/ja
最低限の知識として、telnetクライアントはtelnetサーバとコマンド&レスポンスのやり取りが必要と言うことです
各コマンドは最上位有効ビットを 1 に設定することによりデータと区別されます。
(データは第 8 ビットが 0 に設定された 7 ビットとして転送されます。)
コマンドは常に Interpret as command (IAC) 文字によって示されます。
例えば
| IAC | 動作のタイプ | オプション |
| ff | fd |18 |
上記の『ff,fd,18』は『Will Terminal Type』と言うコマンドでteratermのtelnetクライアントが、ESTABLISHED後、一番にサーバへ送出するコマンドです。
以下は、teratermのtelnetクライアントでログイン・プロンプトが表示されるまでLinuxサーバとのやりとりをwiresharkでキャプチャ後、テキスト形式で保存します
アプリケーションレイアのTelnet Data部分を+ボタンで展開
File→Export→File→Plain text(ファイルの種類)→ファイル名をつけて保存すると下記のようになる(途中省略)
No. Time Source Destination Protocol Info
1 0.000000 192.168.2.11 192.168.2.240 TCP 2331 > 23 [SYN] Seq=0 Win=16384 Len=0 MSS=1460
Frame 1 (62 bytes on wire, 62 bytes captured)
Ethernet II, Src: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02), Dst: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65)
Internet Protocol, Src: 192.168.2.11 (192.168.2.11), Dst: 192.168.2.240 (192.168.2.240)
Transmission Control Protocol, Src Port: 2331 (2331), Dst Port: 23 (23), Seq: 0, Len: 0
No. Time Source Destination Protocol Info
2 0.003797 192.168.2.240 192.168.2.11 TCP 23 > 2331 [SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1460
Frame 2 (63 bytes on wire, 63 bytes captured)
Ethernet II, Src: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65), Dst: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02)
Internet Protocol, Src: 192.168.2.240 (192.168.2.240), Dst: 192.168.2.11 (192.168.2.11)
Transmission Control Protocol, Src Port: 23 (23), Dst Port: 2331 (2331), Seq: 0, Ack: 1, Len: 0
No. Time Source Destination Protocol Info
3 0.003841 192.168.2.11 192.168.2.240 TCP 2331 > 23 [ACK] Seq=1 Ack=1 Win=17520 Len=0
Frame 3 (54 bytes on wire, 54 bytes captured)
Ethernet II, Src: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02), Dst: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65)
Internet Protocol, Src: 192.168.2.11 (192.168.2.11), Dst: 192.168.2.240 (192.168.2.240)
Transmission Control Protocol, Src Port: 2331 (2331), Dst Port: 23 (23), Seq: 1, Ack: 1, Len: 0
No. Time Source Destination Protocol Info
4 0.075627 192.168.2.11 192.168.2.240 TELNET Telnet Data ...
Frame 4 (69 bytes on wire, 69 bytes captured)
Ethernet II, Src: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02), Dst: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65)
Internet Protocol, Src: 192.168.2.11 (192.168.2.11), Dst: 192.168.2.240 (192.168.2.240)
Transmission Control Protocol, Src Port: 2331 (2331), Dst Port: 23 (23), Seq: 1, Ack: 1, Len: 15
Telnet
Command: Will Terminal Type
Command: Do Suppress Go Ahead
Command: Will Suppress Go Ahead
Command: Do Echo
Command: Will Negotiate About Window Size
No. Time Source Destination Protocol Info
5 0.078002 192.168.2.240 192.168.2.11 TCP 23 > 2331 [ACK] Seq=1 Ack=16 Win=5840 Len=0
Frame 5 (60 bytes on wire, 60 bytes captured)
Ethernet II, Src: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65), Dst: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02)
Internet Protocol, Src: 192.168.2.240 (192.168.2.240), Dst: 192.168.2.11 (192.168.2.11)
Transmission Control Protocol, Src Port: 23 (23), Dst Port: 2331 (2331), Seq: 1, Ack: 16, Len: 0
No. Time Source Destination Protocol Info
6 1.042733 192.168.2.240 192.168.2.11 TELNET Telnet Data ...
Frame 6 (72 bytes on wire, 72 bytes captured)
Ethernet II, Src: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65), Dst: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02)
Internet Protocol, Src: 192.168.2.240 (192.168.2.240), Dst: 192.168.2.11 (192.168.2.11)
Transmission Control Protocol, Src Port: 23 (23), Dst Port: 2331 (2331), Seq: 1, Ack: 16, Len: 18
Telnet
Command: Do Terminal Type
Command: Will Suppress Go Ahead
Command: Do Suppress Go Ahead
Command: Will Echo
Command: Do Negotiate About Window Size
Command: Do Authentication Option
No. Time Source Destination Protocol Info
7 1.043224 192.168.2.11 192.168.2.240 TELNET Telnet Data ...
Frame 7 (63 bytes on wire, 63 bytes captured)
Ethernet II, Src: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02), Dst: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65)
Internet Protocol, Src: 192.168.2.11 (192.168.2.11), Dst: 192.168.2.240 (192.168.2.240)
Transmission Control Protocol, Src Port: 2331 (2331), Dst Port: 23 (23), Seq: 16, Ack: 19, Len: 9
Telnet
Suboption Begin: Negotiate About Window Size
Command: Suboption End
No. Time Source Destination Protocol Info
8 1.044762 192.168.2.240 192.168.2.11 TCP 23 > 2331 [ACK] Seq=19 Ack=25 Win=5840 Len=0
Frame 8 (60 bytes on wire, 60 bytes captured)
Ethernet II, Src: 00:0a:79:b1:58:65 (00:0a:79:b1:58:65), Dst: 00:16:6f:5f:7a:02 (00:16:6f:5f:7a:02)
Internet Protocol, Src: 192.168.2.240 (192.168.2.240), Dst: 192.168.2.11 (192.168.2.11)
Transmission Control Protocol, Src Port: 23 (23), Dst Port: 2331 (2331), Seq: 19, Ack: 25, Len: 0
以下省略
pepoと