def _modify_device_variable(self, var_name, var_descr, var_type, var_value): try: cur_var = device_variable.objects.get(Q(device=self.device) & Q(name=var_name)) except device_variable.DoesNotExist: cur_var = device_variable( device=self.device, name=var_name) cur_var.description = var_descr cur_var.set_value(var_value) cur_var.save()
def update(self): if self.__act_dv: self.__act_dv.description = self.__description self.__act_dv.var_type = self.__var_type setattr(self.__act_dv, "val_{}".format(self.__var_type_name), self.__var_value) else: self.__act_dv = device_variable(description=self.__description, var_type=self.__var_type, name=self.__var_name, device=self.__device) setattr(self.__act_dv, "val_{}".format(self.__var_type_name), self.__var_value) self.__act_dv.save()
def do_ssh(conf): conf_dict = conf.conf_dict # also used in fetch_ssh_keys ssh_types = [("rsa1", 1024), ("dsa", 1024), ("rsa", 1024), ("ecdsa", 521)] ssh_field_names = [] for ssh_type, _size in ssh_types: ssh_field_names.extend( [ "ssh_host_{}_key".format(ssh_type), "ssh_host_{}_key_pub".format(ssh_type), ] ) found_keys_dict = {key: None for key in ssh_field_names} for cur_var in device_variable.objects.filter(Q(device=conf_dict["device"]) & Q(name__in=ssh_field_names)): try: cur_val = base64.b64decode(cur_var.val_blob) except: pass else: found_keys_dict[cur_var.name] = cur_val print( "found {} in database: {}".format( logging_tools.get_plural("key", len(list(found_keys_dict.keys()))), ", ".join(sorted(found_keys_dict.keys())) ) ) new_keys = [] for ssh_type, key_size in ssh_types: privfn = "ssh_host_{}_key".format(ssh_type) pubfn = "ssh_host_{}_key_pub".format(ssh_type) if not found_keys_dict[privfn] or not found_keys_dict[pubfn]: # delete previous versions device_variable.objects.filter(Q(device=conf_dict["device"]) & Q(name__in=[privfn, pubfn])).delete() print("Generating {} keys...".format(privfn)) sshkn = tempfile.mktemp("sshgen") sshpn = "{}.pub".format(sshkn) if ssh_type: _cmd = "ssh-keygen -t {} -q -b {:d} -f {} -N ''".format(ssh_type, key_size, sshkn) else: _cmd = "ssh-keygen -q -b 1024 -f {} -N ''".format(sshkn) c_stat, c_out = subprocess.getstatusoutput(_cmd) if c_stat: print("error generating: {}".format(c_out)) else: found_keys_dict[privfn] = open(sshkn, "rb").read() found_keys_dict[pubfn] = open(sshpn, "rb").read() try: os.unlink(sshkn) os.unlink(sshpn) except: pass new_keys.extend([privfn, pubfn]) if new_keys: new_keys.sort() print("{} to create: {}".format( logging_tools.get_plural("key", len(new_keys)), ", ".join(new_keys) )) for new_key in new_keys: if found_keys_dict[new_key] is not None: new_dv = device_variable( device=conf_dict["device"], name=new_key, var_type="b", description="SSH key {}".format(new_key), val_blob=base64.b64encode(found_keys_dict[new_key]) ) new_dv.save() for ssh_type, key_size in ssh_types: privfn = "ssh_host_{}_key".format(ssh_type) pubfn = "ssh_host_{}_key_pub".format(ssh_type) _pubfrn = "ssh_host_{}_key.pub".format(ssh_type) for var in [privfn, pubfn]: if found_keys_dict[var] is not None: new_co = conf.add_file_object("/etc/ssh/{}".format(var.replace("_pub", ".pub"))) new_co.bin_append(found_keys_dict[var]) if var == privfn: new_co.mode = "0600" if ssh_type == "rsa1": for var in [privfn, pubfn]: new_co = conf.add_file_object("/etc/ssh/{}".format(var.replace("_rsa1", "").replace("_pub", ".pub"))) new_co.bin_append(found_keys_dict[var]) if var == privfn: new_co.mode = "0600"