diff --git a/main/asterisk.c b/main/asterisk.c
index beed0108312b4e1b9678afdb3d0076f2b13a62f1..07186f655fb3fd3f11f48066db5cad2a6eb241d1 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -2112,6 +2112,11 @@ static int remoteconsolehandler(char *s)
 	/* Called when readline data is available */
 	if (!ast_all_zeros(s))
 		ast_el_add_history(s);
+
+	while (isspace(*s)) {
+		s++;
+	}
+
 	/* The real handler for bang */
 	if (s[0] == '!') {
 		if (s[1])
diff --git a/main/cli.c b/main/cli.c
index dd9f3bcd0792ae85d87ba4fd3bf6630c046242ed..54efd6f4cd767a5e4c434619fc4d5cb53a02a3a1 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -2292,6 +2292,13 @@ static char *parse_args(const char *s, int *argc, const char *argv[], int max, i
 		return NULL;
 
 	cur = duplicate;
+
+	/* Remove leading spaces from the command */
+	while (isspace(*s)) {
+		cur++;
+		s++;
+	}
+
 	/* scan the original string copying into cur when needed */
 	for (; *s ; s++) {
 		if (x >= max - 1) {