Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <boost/fusion/adapted.hpp> |
| 9 | #include <boost/spirit/home/x3.hpp> |
| 10 | #include <optional> |
| 11 | #include <stdexcept> |
| 12 | #include <unistd.h> |
| 13 | #include "cli-netconf.hpp" |
| 14 | |
| 15 | SshProcess sshProcess(const std::string& target, const std::string& port) |
| 16 | { |
| 17 | namespace bp = boost::process; |
| 18 | bp::pipe in; |
| 19 | bp::pipe out; |
| 20 | auto sshPath = bp::search_path("ssh"); |
| 21 | if (sshPath.empty()) { |
| 22 | throw std::runtime_error("ssh not found in PATH."); |
| 23 | } |
| 24 | if (target.front() == '@') { |
| 25 | throw std::runtime_error("Invalid username."); |
| 26 | } |
| 27 | bp::child ssh(sshPath, |
| 28 | target, |
| 29 | "-p", |
| 30 | port, |
| 31 | "-s", |
| 32 | "netconf", |
| 33 | bp::std_out > out, bp::std_in < in); |
| 34 | |
Václav Kubernát | d7d793c | 2020-11-26 03:15:48 +0100 | [diff] [blame] | 35 | return {.process = std::move(ssh), .std_in = std::move(in), .std_out = std::move(out)}; |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 36 | } |