LoginSignup
3
2

More than 1 year has passed since last update.

curlでポートマッピング【UPnP】

Last updated at Posted at 2022-12-03

Locationを取得する

192.168.10.1はデフォルトゲートウェイ

PowerShell

$client = New-Object System.Net.Sockets.UdpClient
$client.Send([System.Text.Encoding]::UTF8.GetBytes("M-SEARCH*HTTP/1.1`r`nST:upnp:rootdevice"), 37, "192.168.10.1", 1900)
[System.Text.Encoding]::UTF8.GetString($client.Receive([ref][System.Net.IPEndPoint]::New([IPAddress]::Any, 0)))
HTTP/1.1 200 OK
Cache-Control: max-age=120
ST: upnp:rootdevice
USN: uuid:09182736-4554-6372-8191-c025a22a3c84::upnp:rootdevice
EXT:
Server: miniupnpd/1.0 UPnP/1.0
Location: http://192.168.10.1:52869/d030ac11.xml

Linux

echo -e 'M-SEARCH*HTTP/1.1\r\nST:upnp:rootdevice' | nc -u 192.168.10.1 1900
HTTP/1.1 200 OK
Cache-Control: max-age=120
ST: upnp:rootdevice
USN: uuid:09182736-4554-6372-8191-c025a22a3c84::upnp:rootdevice
EXT:
Server: miniupnpd/1.0 UPnP/1.0
Location: http://192.168.10.1:52869/d030ac11.xml

Locationのurlを開いてcontrolURLがエンドポイントになります。控えておいてください
image.png

SCPDURLを開くとリクエストのパラメータが分かります

ポートマッピング

192.168.10.100はIPv4アドレス。8000は開放したいポート

curl http://192.168.10.1:52869/upnp/control/f96e0111 \
  -H 'SOAPAction: urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping' \
  -d '<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  <NewRemoteHost></NewRemoteHost>
  <NewExternalPort>8000</NewExternalPort>
  <NewProtocol>TCP</NewProtocol>
  <NewInternalPort>8000</NewInternalPort>
  <NewInternalClient>192.168.10.100</NewInternalClient>
  <NewEnabled>1</NewEnabled>
  <NewPortMappingDescription>Hoge</NewPortMappingDescription>
  <NewLeaseDuration>0</NewLeaseDuration>
</u:AddPortMapping>
</s:Body>
</s:Envelope>'

コマンド

upnpc -u http://192.168.10.1:52869/d030ac11.xml -a 192.168.10.100 8000 8000 tcp

ポートマッピング確認

curl http://192.168.10.1:52869/upnp/control/f96e0111 \
  -H 'SOAPAction: urn:schemas-upnp-org:service:WANIPConnection:1#GetGenericPortMappingEntry' \
  -d '<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  <NewPortMappingIndex>0</NewPortMappingIndex>
</u:GetGenericPortMappingEntry>
</s:Body>
</s:Envelope>'
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <u:GetGenericPortMappingEntryResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
      <NewRemoteHost></NewRemoteHost>
      <NewExternalPort>8000</NewExternalPort>
      <NewProtocol>TCP</NewProtocol>
      <NewInternalPort>8000</NewInternalPort>
      <NewInternalClient>192.168.10.100</NewInternalClient>
      <NewEnabled>1</NewEnabled>
      <NewPortMappingDescription>miniupnpd</NewPortMappingDescription>
      <NewLeaseDuration>0</NewLeaseDuration>
    </u:GetGenericPortMappingEntryResponse>
  </s:Body>
</s:Envelope>

コマンド

upnpc -u http://192.168.10.1:52869/d030ac11.xml -l

ポートマッピング削除

curl http://192.168.10.1:52869/upnp/control/f96e0111 \
  -H 'SOAPAction: urn:schemas-upnp-org:service:WANIPConnection:1#DeletePortMapping' \
  -d '<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:DeletePortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  <NewRemoteHost></NewRemoteHost>
  <NewExternalPort>8000</NewExternalPort>
  <NewProtocol>TCP</NewProtocol>
</u:DeletePortMapping>
</s:Body>
</s:Envelope>'

コマンド

upnpc -u http://192.168.10.1:52869/d030ac11.xml -d 8000 tcp

グローバルIP確認

curl -X POST http://192.168.10.1:52869/upnp/control/f96e0111 -H "SOAPACTION: urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"

まとめ

curlでやるとグローバルIPが変わらないので便利です

3
2
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
3
2