5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Oracle Cloud(OCI)の自インスタンスの情報を取得する方法

Last updated at Posted at 2019-01-27

Oracle Cloud Infrastructure(OCI)上に作成したインスタンスについて、自インスタンスの基本的な情報を確認する方法。(検証してる時とか便利。unameとかと同じノリで使う)

コマンド

$ curl -L http://169.254.169.254/opc/v1/instance/
# IPアドレスは変えなくてOK

出力例(computeインスタンスで実施)

[opc@testinstance ~]$ curl -L http://169.254.169.254/opc/v1/instance/
{
  "availabilityDomain" : "gype:PHX-AD-1",
  "faultDomain" : "FAULT-DOMAIN-1",
  "compartmentId" : "ocid1.compartment.oc1..XXXXXXXXXXX",
  "displayName" : "testinstance",
  "id" : "ocid1.instance.oc1.phx.XXXXXXXXXXX",
  "image" : "ocid1.image.oc1.phx.XXXXXXXXXXX",
  "metadata" : {
    "ssh_authorized_keys" : "ssh-rsa XXXXXXX== XXXXXXX@XXXXXXX\n"
  },
  "region" : "phx",
  "canonicalRegionName" : "us-phoenix-1",
  "shape" : "VM.Standard2.1",
  "state" : "Running",
  "timeCreated" : 1534770022275
}

説明

  • 「169.254.0.0/16」はOracle Cloudが内部で使うために予約してるIP
  • 「169.254.169.254」はインスタンスのメタデータ取得用のIPアドレス
  • Windowsの場合はブラウザで確認
  • curlの「-L」オプションは、リダイレクトされても対応するよ、というやつ(つけなくても通る)
       -L, --location
              (HTTP/HTTPS)  If  the  server  reports  that the requested page has moved to a different location (indicated with a Location: header and a 3XX
              response code), this option will make curl redo the request on the new place. If used together with -i, --include or -I, --head, headers  from
              all  requested pages will be shown. When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl
              to a different host, it won't be able to intercept the user+password. See also --location-trusted on how to change this.  You  can  limit  the
              amount of redirects to follow by using the --max-redirs option.

              When  curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the
              HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmod‐
              ified method.

おまけ

  • URLの後ろに項目指定するとそれだけ取れる
[opc@testinstance ~]$ curl  http://169.254.169.254/opc/v1/instance/region
phx
  • 'jq'コマンド(json出力をいい感じにいじるコマンド)を使っても同じことができる
[opc@testinstance ~]$ curl  http://169.254.169.254/opc/v1/instance/ -s | jq -r '.region'
phx

※jqの"-r"オプションは、引用符("")を除いてrawで出力させるオプション
※curlの"-s"オプションは、進捗とかエラーメッセージとかを出力させないサイレントオプション
これをつけないと余計な出力が出ちゃう

[opc@testinstance ~]$ curl  http://169.254.169.254/opc/v1/instance/ |  jq -r '.region'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1027  100  1027    0     0   582k      0 --:--:-- --:--:-- --:--:-- 1002k
phx

参考

OCIマニュアル「Getting Instance Metadata」
https://docs.cloud.oracle.com/iaas/Content/Compute/Tasks/gettingmetadata.htm

以上

5
0
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
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?