Skip to content

Commit

Permalink
remote-ssh/scp/sftp: add convenience -i option to pass ssh identity file
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Sep 30, 2023
1 parent 9762cc4 commit b87f47e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions WebTunnel/WebTunnelSCP/src/WebTunnelSCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ class WebTunnelSCP: public Poco::Util::Application
.argument("password"s)
.callback(OptionCallback<WebTunnelSCP>(this, &WebTunnelSCP::handlePassword)));

options.addOption(
Option("identity-file"s, "i"s, "Specify SSH identity file. This is passed on to the SCP client (-i)."s)
.required(false)
.repeatable(false)
.argument("path"s)
.callback(OptionCallback<WebTunnelSCP>(this, &WebTunnelSCP::handleIdentity)));

options.addOption(
Option("define"s, "D"s, "Define or override a configuration property."s)
.required(false)
Expand Down Expand Up @@ -204,6 +211,11 @@ class WebTunnelSCP: public Poco::Util::Application
_password = value;
}

void handleIdentity(const std::string& name, const std::string& value)
{
_identity = value;
}

void handleDefine(const std::string& name, const std::string& value)
{
defineProperty(value);
Expand Down Expand Up @@ -369,6 +381,11 @@ class WebTunnelSCP: public Poco::Util::Application

std::string remoteHost;
Poco::Process::Args scpArgs;
if (!_identity.empty())
{
scpArgs.push_back("-i");
scpArgs.push_back(_identity);
}
for (const auto& arg: args)
{
auto minPos = arg.find('-');
Expand Down Expand Up @@ -437,6 +454,7 @@ class WebTunnelSCP: public Poco::Util::Application
std::string _password;
std::string _scpClient;
std::string _protocol;
std::string _identity;
SSLInitializer _sslInitializer;
};

Expand Down
18 changes: 18 additions & 0 deletions WebTunnel/WebTunnelSFTP/src/WebTunnelSFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class WebTunnelSFTP: public Poco::Util::Application
.argument("username"s)
.callback(OptionCallback<WebTunnelSFTP>(this, &WebTunnelSFTP::handleLogin)));

options.addOption(
Option("identity-file"s, "i"s, "Specify SSH identity file. This is passed on to the SFTP client (-i)."s)
.required(false)
.repeatable(false)
.argument("path"s)
.callback(OptionCallback<WebTunnelSFTP>(this, &WebTunnelSFTP::handleIdentity)));

options.addOption(
Option("define"s, "D"s, "Define or override a configuration property."s)
.required(false)
Expand Down Expand Up @@ -216,6 +223,11 @@ class WebTunnelSFTP: public Poco::Util::Application
_login = value;
}

void handleIdentity(const std::string& name, const std::string& value)
{
_identity = value;
}

void handleDefine(const std::string& name, const std::string& value)
{
defineProperty(value);
Expand Down Expand Up @@ -424,6 +436,11 @@ class WebTunnelSFTP: public Poco::Util::Application
sftpURI += Poco::format("localhost:%hu"s, localPort);

Poco::Process::Args sftpArgs;
if (!_identity.empty())
{
sftpArgs.push_back("-i");
sftpArgs.push_back(_identity);
}
std::vector<std::string>::const_iterator itArgs = ++args.begin();
sftpArgs.insert(sftpArgs.end(), itArgs, args.end());
sftpArgs.push_back(sftpURI);
Expand All @@ -443,6 +460,7 @@ class WebTunnelSFTP: public Poco::Util::Application
std::string _username;
std::string _password;
std::string _login;
std::string _identity;
std::string _sftpClient;
SSLInitializer _sslInitializer;
};
Expand Down
18 changes: 18 additions & 0 deletions WebTunnel/WebTunnelSSH/src/WebTunnelSSH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ class WebTunnelSSH: public Poco::Util::Application
.argument("username"s)
.callback(OptionCallback<WebTunnelSSH>(this, &WebTunnelSSH::handleLogin)));

options.addOption(
Option("identity-file"s, "i"s, "Specify SSH identity file. This is passed on to the SSH client (-i)."s)
.required(false)
.repeatable(false)
.argument("path"s)
.callback(OptionCallback<WebTunnelSSH>(this, &WebTunnelSSH::handleIdentity)));

options.addOption(
Option("command"s, "m"s, "Specify remote (SSH) command. This is passed as second argument to the SSH client."s)
.required(false)
Expand Down Expand Up @@ -238,6 +245,11 @@ class WebTunnelSSH: public Poco::Util::Application
_login = value;
}

void handleIdentity(const std::string& name, const std::string& value)
{
_identity = value;
}

void handleCommand(const std::string& name, const std::string& value)
{
_command = value;
Expand Down Expand Up @@ -450,6 +462,11 @@ class WebTunnelSSH: public Poco::Util::Application
sshArgs.push_back("-l"s);
sshArgs.push_back(_login);
}
if (!_identity.empty())
{
sshArgs.push_back("-i");
sshArgs.push_back(_identity);
}
sshArgs.insert(sshArgs.end(), itArgs, args.end());
if (Poco::icompare(_sshClient, 0, 3, "scp") != 0)
{
Expand All @@ -476,6 +493,7 @@ class WebTunnelSSH: public Poco::Util::Application
std::string _username;
std::string _password;
std::string _login;
std::string _identity;
std::string _sshClient;
std::string _command;
SSLInitializer _sslInitializer;
Expand Down

0 comments on commit b87f47e

Please sign in to comment.