LoginSignup
3
3

More than 5 years have passed since last update.

さくらVPSのサーバー情報(IPとGatewayとDNS)を取得するスクリプト

Posted at

メモです。

Script

#!/bin/bash

ip_addr=""
passwd=""

curl=$(which curl)
lcurl=""

cookie_file="cookie"

vps_init() {

  if [ -z $ip_addr ]; then
    echo -n "IP Addr: "
    read ip_addr
  fi

  if [ -z $passwd ]; then
    echo -n "Password "
    stty -echo
    read passwd
    stty echo
  fi

  echo -e "\n"
}

login() {
  vps_init
  url="https://secure.sakura.ad.jp/vpscontrol/"
  token=$($curl -L $url | grep Token | sed -e 's/.* value="\(.*\)".*/\1/g')

  $curl -L -c $cookie_file \
      -F "Submit=index" \
      -F "Token=$token" \
      -F "ipaddress=$ip_addr" \
      -F "password=$passwd" \
      $url > /dev/null

  if [ ! -e $cookie_file ]; then
    echo "Login failure."
    finish -1
  fi

  name=$(cat $cookie_file | grep secure.sakura.ad.jp  |awk '{print $6}')
  val=$(cat $cookie_file | grep secure.sakura.ad.jp  |awk '{print $7}')



  lcurl="eval $curl -L -b \"$name=$val\""

}

generate_config() {
  url="https://secure.sakura.ad.jp/vpscontrol/main/"

  tmp=$($lcurl \
      -F "Token=$token" \
      $url | egrep "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | sed -e "s/[^0-9\.,]//g" | tr ',' ' ')

  ip=$(echo $tmp | awk '{print $1}')
  gateway=$(echo $tmp | awk '{print $3}')
  dns1=$(echo $tmp | awk '{print $4}')
  dns2=$(echo $tmp | awk '{print $5}')
  cat <<EOF > ./vps-config
#VPS Config
VPS_IP="$ip/23"
VPS_GATEWAY="$gateway"
VPS_DNS1="$dns1"
VPS_DNS2="$dns2"
EOF


}

finish() {

  rm -f $cookie_file

  exit $1
}

main() {
  login
  generate_config
  finish 0
}

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