feat(add): create add script

This commit is contained in:
2021-09-02 19:10:41 +02:00
parent 5759b8c604
commit 9d84e96462

53
src/add-sshkey.cc Normal file
View File

@@ -0,0 +1,53 @@
#include <iostream>
#include <fstream>
int append_key(std::string &key, std::fstream &file)
{
std::string tmp;
while (getline(file, tmp))
{
if (tmp == key)
return 1;
}
file << "\n" << key;
if (file.bad())
return 2;
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "[!!] This program needs one (1) argument to work, exiting."
<< std::endl;
return 1;
}
std::string key(argv[1]);
std::fstream file("~/.ssh/authorized_keys");
int res = append_key(key, file);
file.close();
if (res == 1)
{
std::cerr << "[!!] The key has already been added, exiting."i
<< std::endl;
}
else if (res == 2)
{
std::cerr << "[!!] Error while writing key, exiting." << std::endl;
}
else
{
std::cerr << "[++] Key has been added." << std::endl;
}
return 0;
}