Skip to content

Instantly share code, notes, and snippets.

@EmperorPenguin18
Created May 10, 2021 23:31
Show Gist options
  • Save EmperorPenguin18/cc3652832cce6929e92db02e86cc0952 to your computer and use it in GitHub Desktop.
Save EmperorPenguin18/cc3652832cce6929e92db02e86cc0952 to your computer and use it in GitHub Desktop.
Automate setting up a RPI
//Raspberry pi configuration
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
string name;
string address;
string password;
FILE *ssh;
int command(string input)
{
fprintf( ssh, "%s\n", (input).c_str() );
return 0;
}
int print(string words)
{
cout << words << '\n';
return 0;
}
int main()
{
print("Welcome to the Raspbian setter upper!");
print("Please enter the IP address of the device.");
cout << "->";
cin >> address;
cout << '\n';
print("Please enter the hostname you want for the device.");
cout << "->";
cin >> name;
cout << '\n';
print("Please enter the new password you want for the device.");
cout << "->";
cin >> password;
cout << '\n';
ssh = popen( ("ssh pi@" + address).c_str(), "w");
command("sudo su");
command("apt-get update && apt-get -y upgrade");
command("rm /etc/hostname");
command("touch /etc/hostname");
command("echo \"" + name + "\" >> /etc/hostname");
command("rm /etc/hosts");
command("touch /etc/hosts");
command("echo \"127.0.0.1 localhost\" >> /etc/hosts");
command("echo \"::1 localhost ip6-localhost ip6-loopback\" >> /etc/hosts");
command("echo \"ff02::1 ip6-allnodes\" >> /etc/hosts");
command("echo \"ff02::2 ip6-allrouters\" >> /etc/hosts");
command("echo \"\" >> /etc/hosts");
command("echo \"127.0.1.1 " + name + "\" >> /etc/hosts");
command("passwd pi\n" + password + "\n" + password);
command("rm /etc/locale.gen");
command("touch /etc/locale.gen");
command("echo \"en_CA.UTF-8 UTF-8\" >> /etc/locale.gen");
command("locale-gen");
command("rm /etc/environment");
command("touch /etc/environment");
command("echo \"LANGUAGE=en_CA.UTF-8\" >> /etc/environment");
command("echo \"LC_ALL=en_CA.UTF-8\" >> /etc/environment");
command("echo \"LANG=en_CA.UTF-8\" >> /etc/environment");
command("rm /etc/localtime");
command("ln -s /usr/share/zoneinfo/America/Toronto /etc/localtime");
command("reboot");
pclose(ssh);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment