Change IP settings to ACL of conoha with python

Basically, only adding and updating security group rules, so it is a condition that a security group has already been set for the target.

A little explanation

code

set_acl.py


#!/usr/bin/env python
import requests
import json
import os

class set_rule():

        def __init__(self): #Initialization
                my_ip = ""
                my_token = ""
                my_rule =""

        def set_data(self,rule): #Set of security group ID, token, current IP
                self.my_rule = rule
                with open('my_ip.txt','r') as f:
                        self.my_ip = f.read()

                with open('my_token.txt','r') as f:
                        self.my_token = f.read()

        def remove(self): #Delete old registration information
                rule_data = ''
                rule_flug = os.path.exists("./my_rule.txt")

                if rule_flug: #Do not delete if the file does not exist
                        with open('my_rule.txt', 'r') as f:
                                rule_data = f.read()
                        url='https://networking.tyo1.conoha.io/v2.0/security-group-rules/'+rule_data
                        header={'Accept':'application/json','X-Auth-Token':''}
                        header['X-Auth-Token']=self.my_token
                        r = requests.delete(url,headers=header)
                        print "delete_rule_data="+rule_data

                if rule_flug == False:
                        print "no_rule_data"

        def set_ip(self): #Add rule to security group Setting to allow tcp communication from external specified IP, no port restriction
                url = "https://networking.tyo1.conoha.io/v2.0/security-group-rules"
                header = {'Accept':'application/json','X-Auth-Token':''}
                header['X-Auth-Token']=self.my_token
                data = {"security_group_rule":{"direction": "ingress","ethertype": "IPv4","security_group_id":"","protocol":"tcp" ,"remote_ip_prefix":""}}
                data["security_group_rule"]["security_group_id"]=self.my_rule
                data["security_group_rule"]["remote_ip_prefix"]=self.my_ip

                r = requests.post(url,data=json.dumps(data),headers=header)

                with open('rule_data.json','w') as f:
                        f.write(r.text)

                with open('rule_data.json','r') as f:
                        jsonData = json.loads(f.read())

                with open('my_rule.txt','w') as f: #Record rule ID
                        f.write(jsonData["security_group_rule"]["id"])
                        print "set_rule="+jsonData["security_group_rule"]["id"]

def set_rule_main(group): #Main processing
        groupid = group
        rule_setter = set_rule()
        rule_setter.set_data(groupid)
        rule_setter.remove()
        rule_setter.set_ip()


if __name__ == '__main__':
        set_rule_main("Security group ID")

If you only want to update, Get Token and Get IP and this time Import the script of and use it for batch processing.

Example of use

set_acl_main.py


#!/usr/bin/env python
import ip
import get_token
import set_acl

get_ip_url = "http://www.axisnetworks.biz/tools/gip/"
user = 'XXXXXX'
key = 'XXXXXX'
tenantId = 'XXXXXXX'
security_group = "XXXXXXXXX"

if __name__ == '__main__':
        ip.ip_get(get_ip_url)
        get_token.token_get_main(user,key,tenantId)
        set_acl.set_rule_main(security_group)

I had set my home IP in the ACL, but when my home router restarted, the IP changed, and it was troublesome to reset at that time, so I created it, but when I changed the line recently, it was almost It's no longer needed.

Recommended Posts

Change IP settings to ACL of conoha with python
Change Python 64bit environment to 32bit environment with Anaconda
How to specify attributes with Mock of python
A memo connected to HiveServer2 of EMR with python
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
Connect to BigQuery with Python
Record global IP with python
Connect to Wikipedia with Python
Post to slack with Python 3
[Python] Change dtype with pandas
Change retry settings with boto3
Switch python to 2.7 with alternatives
Write to csv with Python
How to change Python version
I tried to find the entropy of the image with python
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
I want to specify another version of Python with pyvenv
Settings to debug the contents of the library with VS Code
Try to automate the operation of network devices with Python
Web application with Python3.3.1 + Bottle (1) --Change template engine to jinja2
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
Try Juniper JUNOS PyEz (python library) Memo 3 ~ Change settings with PyEz ~
Change node settings in supernodes with SPSS Modeler Python scripts
Get the source of the page to load infinitely with python.
Made it possible to convert PNG to JPG with Pillow of Python
Python: How to use async with
IPynb scoring system made with TA of Introduction to Programming (Python)
How to change the log level of Azure SDK for Python
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
How to change Django's SQLite3 uploaded to python anywhere with GUI only
Try to operate Facebook with Python
Output to csv file with Python
Change python default encoding to utf-8
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
Sample to use after OAuth authentication of BOX API with Python
Feel free to change the label of the legend in Seaborn in python
I tried to create a list of prime numbers with python
Getting Started with Python Basics of Python
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
Change the Python version of Homebrew
How to get started with Python
I tried to fix "I tried stochastic simulation of bingo game with Python"
Life game with Python! (Conway's Game of Life)
What to do with PYTHON release?
10 functions of "language with battery" python
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Implementation of Dijkstra's algorithm with python
Move data to LDAP with python Change / Delete (Writer and Reader)
Easily post to twitter with Python 3
I tried to improve the efficiency of daily work with Python