def telnet(self, port): host = IPManager.get_ips(self) if len(host) == 0: host = self.name else: host = host[0] proc = Popen(['telnet', host, str(port)]) proc.communicate()
def bind_ip_to_osport(self, ip, osportname, ipman=None, porttype=None, portnum=None): """bind an IP to an os port and optionally also asign the os port name to a physical port If the given ip is already allocated to this device then use it. If it isn't, try to allocate it from a matching IPManager. """ if (porttype != None) ^ (portnum != None): raise Exception("both portype and portnum need to be specified or set to None") try: clusto.begin_transaction() if not self.has_ip(ip): if not ipman: ipman = IPManager.get_ip_manager(ip) ipman.allocate(self, ip) clusto.flush() else: ipman = IPManager.get_ip_manager(ip) number = ipman.get_resource_number(self, ip) ipattrs = ipman.get_resource_attrs(self, ip, number=number) if porttype is not None and portnum is not None: self.set_port_attr(porttype, portnum, 'osportname', osportname) self.set_attr(ipattrs[0].key, number=ipattrs[0].number, subkey='osportname', value=osportname) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def connect(self, porttype, num, ssh_user='******'): if porttype != 'console-serial': raise DriverException("Cannot connect to a non-serial port") host = IPManager.get_ips(self) if len(host) == 0: host = self.name else: host = host[0] proc = Popen(['ssh', '-p', str(num + 3000), '-l', ssh_user, host]) proc.communicate()
def add_ip(self, ip=None, ipman=None): if not ip and not ipman: raise ResourceException('If no ip is specified then an ipmanager must be specified') elif ip: if not ipman: ipman = IPManager.get_ip_manager(ip) return ipman.allocate(self, ip) else: return ipman.allocate(self)
def _snmp_connect(self, port=161): ip = IPManager.get_ips(self) if not ip: raise ValueError('Device %s does not have an IP' % self.name) ip = ip[0] community = self.attr_values(key='snmp', subkey='community', merge_container_attrs=True) if not community: raise ValueError('Device %s does not have an SNMP community attribute' % self.name) sock = socket(AF_INET, SOCK_DGRAM) sock.connect((ip, port)) return (str(community[0]), sock)
def _snmp_connect(self, port=161): ip = IPManager.get_ips(self) if not ip: raise ValueError('Device %s does not have an IP' % self.name) ip = ip[0] community = self.attr_values(key='snmp', subkey='community', merge_container_attrs=True) if not community: raise ValueError( 'Device %s does not have an SNMP community attribute' % self.name) sock = socket(AF_INET, SOCK_DGRAM) sock.connect((ip, port)) return (str(community[0]), sock)
def has_ip(self, ip): ipman = IPManager.get_ip_manager(ip) return self in ipman.owners(ip)