62 bool g_handlers_created {};
64 vector<SshSocketHandlerPtr> g_handlers;
67 lock_guard<mutex> lock(g_mutex);
69 if (g_handlers_created) {
70 for (
auto & handler : g_handlers)
75 void dump_usage_and_exit() {
79 cout <<
"This application uses libssh 0.6.4 (https://www.libssh.org/)\n";
80 cout <<
"libssh is licensed under the GNU Lesser General Public License\n";
82 cout <<
"usage: ht_ssh [options] <hosts-specification> <command>\n";
85 cout <<
" --debug Turn on verbose debugging output\n";
86 cout <<
" --random-start-delay <millis>\n";
87 cout <<
" Wait a random amount of time between 0 and <millis>\n";
88 cout <<
" prior to starting each command\n";
89 cout <<
" --libssh-verbosity <level>\n";
90 cout <<
" Set libssh verbosity level to <level>. Valid values\n";
91 cout <<
" are: none, warning, protocol, packet, and functions.\n";
92 cout <<
" Default value is protocol.\n";
94 quick_exit(EXIT_FAILURE);
102 int main(
int argc,
char **argv) {
112 for (
int i=1; i<argc; i++) {
113 if (!strcmp(argv[i],
"--debug"))
115 else if (!strcmp(argv[i],
"--libssh-verbosity")) {
118 dump_usage_and_exit();
121 else if (!strcmp(argv[i],
"--random-start-delay")) {
124 dump_usage_and_exit();
125 start_delay = atoi(argv[i]);
127 else if (host_spec.empty())
129 else if (command.empty())
130 command.append(argv[i]);
133 command.append(argv[i]);
138 dump_usage_and_exit();
140 vector<string> hosts;
145 cout <<
"Invalid host specification: " << e.what() << endl;
146 quick_exit(EXIT_FAILURE);
149 (void)signal(SIGINT, sig);
150 (void)signal(SIGQUIT, sig);
155 g_handlers.reserve(hosts.size());
157 for (
auto & host : hosts)
158 g_handlers.push_back(make_shared<SshSocketHandler>(host));
160 auto now = chrono::system_clock::now();
161 auto deadline = now + std::chrono::seconds(30);
164 lock_guard<mutex> lock(g_mutex);
165 g_handlers_created =
true;
167 quick_exit(EXIT_FAILURE);
171 for (
auto & handler : g_handlers) {
172 if (!handler->wait_for_connection(deadline)) {
173 handler->dump_log(cerr);
174 quick_exit(EXIT_FAILURE);
179 lock_guard<mutex> lock(g_mutex);
181 quick_exit(EXIT_FAILURE);
184 vector<string> failed_hosts;
190 chrono::time_point<chrono::steady_clock> start_time;
191 for (
auto & handler : g_handlers) {
192 start_time = chrono::steady_clock::now()
194 start_map[start_time] = handler;
196 chrono::time_point<chrono::steady_clock> now;
197 for (
auto & entry : start_map) {
198 now = chrono::steady_clock::now();
199 this_thread::sleep_for(chrono::duration_cast<std::chrono::milliseconds>(entry.first-now));
200 if (!entry.second->issue_command(command)) {
201 entry.second->dump_log(cerr);
202 failed_hosts.push_back(entry.second->hostname());
203 entry.second.reset();
208 for (
auto & handler : g_handlers) {
209 if (!handler->issue_command(command)) {
210 handler->dump_log(cerr);
211 failed_hosts.push_back(handler->hostname());
218 for (
auto & handler : g_handlers) {
220 handler->set_terminal_output(
true);
221 if (!handler->wait_for_command_completion()) {
222 handler->dump_log(cerr);
223 failed_hosts.push_back(handler->hostname());
225 handler->set_terminal_output(
false);
229 if (!failed_hosts.empty()) {
230 cerr <<
"Command failed on hosts: ";
232 for (
auto & host : failed_hosts) {
239 cerr << endl << flush;
245 quick_exit(EXIT_SUCCESS);
Retrieves system information (hardware, installation directory, etc)
std::shared_ptr< SshSocketHandler > SshSocketHandlerPtr
Smart pointer to SshSocketHandler.
static struct ssh_threads_callbacks_struct * get()
Gets libssh threads callbacks structure.
Declarations for HostSpecification.
PropertiesPtr properties
This singleton map stores all options.
static void initialize(uint16_t reactor_count)
Initializes I/O reactors.
Converts host specification pattern to list of host names.
static void initialize(const String &install_directory=String())
Initializes the static class members; checks header version vs.
static int32_t get_processor_count()
The processor count.
static uint32_t number32(uint32_t maximum=0)
Returns a random 32-bit unsigned integer.
Declarations for SshThreadsCallbacks.
A timer class to keep timeout states across AsyncComm related calls.
Logging routines and macros.
int main(int argc, char **argv)
Compatibility Macros for C/C++.
Initialization helper for applications.
Declarations for SshOutputCollector.
static void set_libssh_verbosity(const std::string &value)
Sets libssh logging verbosity level.
static void enable_debug()
Enables debug logging output.
Random number generator for int32, int64, double and ascii arrays.
Declarations for ReactorFactory.
This is a generic exception class for Hypertable.
Error codes, Exception handling, error logging.
const char * version_string()
Declarations for SshSocketHandler.