Skip to content

Commit

Permalink
Add example remote command program.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Naylor committed Jan 7, 2019
1 parent 42a4820 commit a3aaffd
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
73 changes: 73 additions & 0 deletions RemoteCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2019 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "RemoteCommand.h"

#include "UDPSocket.h"

#include <cstdio>

int main(int argc, char** argv)
{
if (argc < 3) {
::fprintf(stderr, "Usage: RemoteCommand <port> <command>\n");
return 1;
}

unsigned int port = (unsigned int)::atoi(argv[1]);
std::string command = std::string(argv[2]);

if (port == 0U) {
::fprintf(stderr, "RemoteCommand: invalid port number - %s\n", argv[1]);
return 1;
}

CRemoteCommand command(port);

return send(command);
}

CRemoteCommand::CRemoteCommand(unsigned int port) :
m_port(port)
{
}

CRemoteCommand::~CRemoteCommand()
{
}

int CRemoteCommand::send(const std::string& command)
{
CUDPSocket socket(0U);

bool ret = socket.open();
if (!ret)
return 1;

in_addr address = CUDPSocket::lookup("localhost");

ret = socket.write(command.c_str(), command.len(), address, m_port);
if (!ret) {
socket.close();
return 1;
}

socket.close();

return 0;
}
36 changes: 36 additions & 0 deletions RemoteCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef RemoteCommand_H
#define RemoteCommand_H

#include <string>

class CRemoteCommand
{
public:
CRemoteCommand(unsigned int port);
~CRemoteCommand();

int send(const std::string& command);

private:
unsigned int m_port;
};

#endif

2 comments on commit a3aaffd

@shawnchain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a Unix shell command to replace this program ?
Like :
echo $cmd | nc - u $port

@g4klx
Copy link
Owner

@g4klx g4klx commented on a3aaffd Jan 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On UNIX/Linux yes, but it's not portable to other operating systems.

Please sign in to comment.