Community
    • Categories
    • Recent
    • Popular
    • Users
    • Search
    • Register
    • Login

    Nächste freie IP Adresse per API abrufen?

    Scheduled Pinned Locked Moved Betrieb
    3 Posts 2 Posters 846 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      cgarling
      last edited by

      Hallo zusammen,

      weiß jemand ob es möglich ist über die API ( i-doit Version 1.8 ) die nächste freie IP Adresse für ein bestimmtes Subnet abzurufen?

      Viele Grüße, Christian

      1 Reply Last reply Reply Quote 0
      • C Offline
        cgarling
        last edited by

        Ich habe mir inzwischen selbst eine Funktion in Python geschrieben. Hier der Code wer's gebrauchen kann:

        
        #!/usr/bin/python
        
        # import modules
        import httplib, json, urllib
        from netaddr import *
        
        # get next unused ip address
        def i_get_next_ip(i_net_id, i_net_title):
                # define parameters
                i_params = ('{"version":"2.0", "id":"1", "method":"cmdb.category.read", "params":{'
                            '"apikey":"' + i_api_key + '", "objID":' + i_net_id + ', "category":"C__CATS__NET_IP_ADDRESSES"}}')
        
                # connect to server
                i_http_conn.connect()
        
                # send POST request
                i_http_conn.request('POST', i_api_url, i_params, i_headers)
        
                # get response
                i_response = json.loads(i_http_conn.getresponse().read())
        
                # close connection
                i_http_conn.close()
        
                # get result list
                i_result_list = i_response['result']
        
                # list of used ip addresses
                i_used_ip_list = []
                if i_result_list:
                        for i in range(len(i_result_list)):
                                i_used_ip = i_result_list[i]['title']
                                if i_used_ip and not i_used_ip in i_used_ip_list:
                                        i_used_ip_list.append(i_used_ip)
        
                # list of unused ip addresses
                subnet_unused_ip_list = []
                for subnet_ip_obj in IPNetwork(i_net_title).iter_hosts():
                        subnet_ip_str = str(subnet_ip_obj)
                        if not subnet_ip_str in i_used_ip_list:
                                subnet_unused_ip_list.append(str(subnet_ip_str))
        
                # first unused ip address
                first_unused_ip = subnet_unused_ip_list[0]
        
                # return value
                return first_unused_ip
        
        # main function
        if __name__ == '__main__':
                # global variables
                i_host = 'idoit.example.com'
                i_headers = {'Content-Type': 'application/json'}
                i_api_url = '/src/jsonrpc.php'
                i_api_key = 'abc123def456'
        
                # instantiate new httplib connection
                i_http_conn = httplib.HTTPConnection(i_host)
        
        	# next unused ip address
        	next_ip = ('42', '192.168.1.0/24')
        
        Die Funktion erwartet die objID des Layer 3 Netzes sowie dessen Bezeichnung, welche den Namen des Netzes im CIDR Format widerspiegeln muss.
        
        Viele Grüße, Christian[/i]
        
        1 Reply Last reply Reply Quote 0
        • bheisigB Offline
          bheisig i-doit Team
          last edited by

          Hallo Christian,

          danke für das Beispiel! Eine weitere Möglichkeit in PHP ist hier beschrieben:

          https://github.com/bheisig/i-doit-api-client-php#give-me-a-free-ip-address

          Viele Grüße
          Benjamin

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post