2021-08-31 16:50:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/rexec/rexec.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-24 05:25:03 +01:00
|
|
|
#include <sys/types.h>
|
2021-08-31 16:50:41 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define REXEC_BUFSIZE 512
|
|
|
|
#define REXEC_SERVER_PORT 512
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct rexec_arg_s
|
|
|
|
{
|
|
|
|
FAR const char *password;
|
|
|
|
FAR const char *command;
|
|
|
|
FAR const char *user;
|
|
|
|
FAR char *host;
|
2022-03-24 05:25:03 +01:00
|
|
|
sa_family_t af;
|
2021-08-31 16:50:41 +02:00
|
|
|
int port;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int do_rexec(FAR struct rexec_arg_s *arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usage(FAR const char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [-u user] [-H host] [-p password] "
|
2021-09-09 05:14:35 +02:00
|
|
|
"[-P port] [-4|-6|-r] command\n", progname);
|
2021-08-31 16:50:41 +02:00
|
|
|
fprintf(stderr, "Remote Execution Client:\n"
|
|
|
|
" -u, Specify the user, default is \\0\n"
|
|
|
|
" -H, Specify the hostname\n"
|
|
|
|
" -p, Specify the password, default is \\0\n"
|
|
|
|
" -P, Specify the port to connect to, default is 512\n"
|
|
|
|
" -4, Specify address family AF_INET(default)\n"
|
2021-09-09 05:14:35 +02:00
|
|
|
" -6, Specify address family AF_INET6\n"
|
|
|
|
" -r, Specify address family AF_RPMSG\n");
|
2021-08-31 16:50:41 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_rexec(FAR struct rexec_arg_s *arg)
|
|
|
|
{
|
|
|
|
char buffer[REXEC_BUFSIZE];
|
|
|
|
int sock;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sock = rexec_af(&arg->host, arg->port, arg->user,
|
|
|
|
arg->password, arg->command,
|
|
|
|
NULL, arg->af);
|
|
|
|
if (sock < 0)
|
|
|
|
{
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
ret = read(sock, buffer, REXEC_BUFSIZE);
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(STDOUT_FILENO, buffer, ret);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(arg->host);
|
|
|
|
close(sock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, FAR char **argv)
|
|
|
|
{
|
2021-10-20 10:00:58 +02:00
|
|
|
char cmd[CONFIG_NSH_LINELEN];
|
2021-08-31 16:50:41 +02:00
|
|
|
struct rexec_arg_s arg;
|
|
|
|
int option;
|
2021-10-20 10:00:58 +02:00
|
|
|
int i;
|
2021-08-31 16:50:41 +02:00
|
|
|
|
|
|
|
memset(&arg, 0, sizeof(arg));
|
|
|
|
|
|
|
|
/* The default port of rexec to connect rexecd */
|
|
|
|
|
|
|
|
arg.port = REXEC_SERVER_PORT;
|
|
|
|
arg.af = AF_INET;
|
|
|
|
|
2021-09-09 05:14:35 +02:00
|
|
|
while ((option = getopt(argc, argv, "u:H:p:P:46r")) != ERROR)
|
2021-08-31 16:50:41 +02:00
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'u':
|
|
|
|
arg.user = optarg;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
arg.host = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
arg.password = optarg;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
arg.port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
arg.af = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
arg.af = AF_INET6;
|
|
|
|
break;
|
2021-09-09 05:14:35 +02:00
|
|
|
case 'r':
|
|
|
|
arg.af = AF_RPMSG;
|
|
|
|
break;
|
2021-08-31 16:50:41 +02:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind == argc || !arg.host)
|
|
|
|
{
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
|
2021-10-20 10:00:58 +02:00
|
|
|
cmd[0] = '\0';
|
|
|
|
for (i = optind; i < argc; i++)
|
|
|
|
{
|
|
|
|
strcat(cmd, argv[i]);
|
|
|
|
strcat(cmd, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
arg.command = cmd;
|
2021-08-31 16:50:41 +02:00
|
|
|
return do_rexec(&arg);
|
|
|
|
}
|