How to know if a machine is an Google Compute Engine instance

前端 未结 4 1485
感动是毒
感动是毒 2021-02-13 09:59

Is there a way to know from a command line shell if I am currently on a Google Compute Engine machine or somewhere else (development machine)?

4条回答
  •  逝去的感伤
    2021-02-13 10:32

    Here is python implementation via socket.getaddrinfo

    import socket
    
    
    def is_gce_instance():
      """Check if it's GCE instance via DNS lookup to metadata server.
      """
      try:
        socket.getaddrinfo('metadata.google.internal', 80)
      except socket.gaierror:
        return False
      return True
    
    
    

提交回复
热议问题