写了个 Vultr VPS 换服脚本
近期封锁又严了 最严的时候Github都不能幸免
- github被墙实锤了 · Issue #293 · wuhan2020/wuhan2020
这个wuhan2020
的项目属实讽刺啊 github都给墙了
在这个特殊时期 方教授还在辛勤工作 感动!
而且这次恶心人的封IP手段叒用上了 随手写了个Vultr
换服脚本 通过调用Vultr API
自动创建Snapshot并以其生成新的Server
# coding=utf-8
import requests
import os
import sys
import time
reload(sys)
sys.setdefaultencoding( "utf-8" )
API_Key = '***************************'
Response_Code = {
200: 'Function successfully executed.',
400: 'Invalid API location. Check the URL that you are using.',
403: 'Invalid or missing API key. Check that your API key is present and matches your assigned key.',
405: 'Invalid HTTP method. Check that the method(POST|GET) matches what the documentation indicates.',
412: 'Request failed. Check the response body for a more detailed description.',
500: 'Internal server error. Try again at a later time.',
503: 'Rate limit hit. API requests are limited to an average of 2/s. Try your request again later.',
}
# VPSPLANID = "201"
VPSPLANID = ''
SUBID = ''
SNAPSHOTID = ''
def sendvultr(url,method,formdata=''):
if method == 'GET':
r = requests.get(url, headers={'API-Key': API_Key})
if method == 'POST':
r = requests.post(url, headers={'API-Key': API_Key}, data=formdata)
code = r.status_code
if code != 200:
print method + ' ' + url + '\n' + str(code) + ' ' + Response_Code[code]
os.system('pause')
os._exit(0)
return r.json()
def get_available_locations():
r1 = requests.get('https://api.vultr.com/v1/plans/list')
PLAN = r1.json()[VPSPLANID]
available_locations = PLAN["available_locations"]
print PLAN["vcpu_count"] + ' vCPU,' + PLAN["name"] + '可用区:'
r2 = requests.get('https://api.vultr.com/v1/regions/list')
allregions = r2.json()
for i in available_locations:
i = str(i)
print i + ':' + allregions[i]["name"] + ' ',
def destroyServer(id):
sendvultr('https://api.vultr.com/v1/server/destroy','POST',{'SUBID':id})
print '销毁:' + id
def ping_ip(ip):
backinfo = os.system('ping %s'%ip)
if backinfo:
return False
else:
return True
def getstatus():
balance = sendvultr('https://api.vultr.com/v1/account/info','GET')['balance'][1:]
serverlist = sendvultr('https://api.vultr.com/v1/server/list','GET')
if len(serverlist) != 1:
print 'Server Not Only One!'
os.system('pause')
os._exit(0)
global SUBID
global VPSPLANID
for key,values in serverlist.items():
# print values['main_ip'] + ' ' + values['location'] + ' ' + values['power_status']
print values['main_ip'] + '\t' + values['location'] + '\t' + values['power_status'] + '\t' + str(values['current_bandwidth_gb']) + 'GB/' + str(values['allowed_bandwidth_gb']) + 'GB'
SUBID = key
VPSPLANID = values['VPSPLANID']
print '余额:' + balance
def makeSnapshot():
global SNAPSHOTID
SNAPSHOTID = sendvultr('https://api.vultr.com/v1/snapshot/create','POST',{'SUBID':SUBID})['SNAPSHOTID']
print 'Pending....',
i = 0
while(1):
time.sleep(60)
status = sendvultr('https://api.vultr.com/v1/snapshot/list','GET')[SNAPSHOTID]['status']
if status == 'complete':
print 'Snapshot complete!'
break
i=i+1
print str(i) + '..',
destroyServer(SUBID)
def CreateServer():
get_available_locations()
DCID = raw_input('DCID:')
NEWSUBID = sendvultr('https://api.vultr.com/v1/server/create','POST',{'DCID':DCID, 'VPSPLANID':VPSPLANID, 'OSID':'164', 'SNAPSHOTID':SNAPSHOTID})['SUBID']
print 'Restoring....'
i = 0
while(1):
time.sleep(60)
server_state = sendvultr('https://api.vultr.com/v1/server/list','GET')[NEWSUBID]['server_state']
if server_state == 'ok':
print 'New Server Running!'
break
i=i+1
print str(i) + '..',
newip = sendvultr('https://api.vultr.com/v1/server/list','GET')[NEWSUBID]['main_ip']
if ping_ip(newip) == True:
print '测试成功'
else:
print '测试不通 重试继续'
os.system('pause')
destroyServer(NEWSUBID)
CreateServer()
if __name__ == "__main__":
print u'1.获取Vultr状态'
getstatus()
os.system('pause')
print u'2.创建Snapshot'
makeSnapshot()
os.system('pause')
print u'3.以快照创建Server'
CreateServer()