Merge "[openwrt_authentication] Skip creating RSA key if exists." into main
diff --git a/acts/framework/acts/controllers/openwrt_lib/ b/acts/framework/acts/controllers/openwrt_lib/
index 28fd60b..a300fff 100644
--- a/acts/framework/acts/controllers/openwrt_lib/
+++ b/acts/framework/acts/controllers/openwrt_lib/
@@ -32,8 +32,9 @@
     self.password = password
     self.port = port
     self.public_key = None
-    self.public_key_file = None
-    self.private_key_file = None
+    self.key_dir = '/tmp/openwrt/'
+    self.public_key_file = f'{self.key_dir}id_rsa_{self.hostname}.pub'
+    self.private_key_file = f'{self.key_dir}id_rsa_{self.hostname}'
   def generate_rsa_key(self):
@@ -46,27 +47,30 @@
       PermissionError: If there is a permission error while creating the directory for saving the keys.
       Exception: If an unexpected error occurs while generating the RSA key pair.
+    # Checks if the private and public key files already exist.
+    if os.path.exists(self.private_key_file) and os.path.exists(self.public_key_file):
+      logging.warning("RSA key pair already exists, skipping key generation.")
+      return
       # Generates an RSA key pair in /tmp/openwrt/ directory."Generating RSA key pair...")
       key = paramiko.RSAKey.generate(bits=2048)
       self.public_key = f"ssh-rsa {key.get_base64()}"
-"Public key: {self.public_key}")
+      logging.debug(f"Public key: {self.public_key}")
       # Create /tmp/openwrt/ directory if it doesn't exist.
-"Creating /tmp/openwrt/ directory...")
-      os.makedirs('/tmp/openwrt/', exist_ok=True)
+"Creating {self.key_dir} directory...")
+      os.makedirs(self.key_dir, exist_ok=True)
       # Saves the private key to a file.
-      self.private_key_file = '/tmp/openwrt/id_rsa_%s' % (self.hostname)
-"Saved private key to file: {self.private_key_file}")
+      logging.debug(f"Saved private key to file: {self.private_key_file}")
       # Saves the public key to a file.
-      self.public_key_file = '/tmp/openwrt/' % (self.hostname)
       with open(self.public_key_file, "w") as f:
-"Saved public key to file: {self.public_key_file}")
+      logging.debug(f"Saved public key to file: {self.public_key_file}")
     except (ValueError, paramiko.SSHException, PermissionError) as e:
       logging.error(f"An error occurred while generating the RSA key pair: {e}")
     except Exception as e: