Last active
September 10, 2017 23:39
-
-
Save mansouryaacoubi/544ae11fe127c52a50de5f99530ab157 to your computer and use it in GitHub Desktop.
Easy python-script to enable Raspberry Pi OTG Ethernet/SSH over USB (dwc2, g_ether)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @author Mansour Yaacoubi | |
# @filename enable_rpi_otg.py | |
# @task Enable ssh over USB for Raspberry Pi | |
# | |
# Install pywin32 for this (https://sourceforge.net/projects/pywin32/files/pywin32/) | |
import win32api, pywintypes | |
import requests | |
from time import sleep | |
# Variables | |
api_url = 'https://api.github.com/gists/253fd2f90f949d1a86614f11e7def613' | |
api_fname = 'config-rpi.sh' | |
st = 'rootwait' | |
ins_cmdline = 'modules-load=dwc2,g_ether' | |
ins_config = '\n# Enable OTG\ndtoverlay=dwc2\n' | |
drive = 'C:/test/' | |
# Get all logical drives | |
drives = win32api.GetLogicalDriveStrings() | |
drives = drives.split('\000')[:-1] | |
# Filter compatible drives | |
temp_drives = [] | |
for val in drives: | |
try: | |
if win32api.GetVolumeInformation(val)[0] == 'boot': | |
temp_drives.append(val) | |
except pywintypes.error: | |
pass | |
drives = temp_drives | |
# Check if any compatible drive is available | |
if len(drives) <= 0: | |
print('No compatible drives found') | |
exit() | |
elif len(drives) == 1: | |
drive = drives[0] | |
print('Drive ' + drive + ' has been selected.') | |
ans = input('Do you want to continue? [Y/n]: ').lower() | |
if ans in ['n', 'no']: | |
exit() | |
else: | |
for idx, val in enumerate(drives): | |
print(str(idx) + ') ' + val) | |
print('Multiple options found.\nPlease select by typing in the number for the desired drive.') | |
while True: | |
try: | |
val = int(input("> ").strip()) | |
if val in range(len(drives)): | |
drive = drives[val] | |
break | |
else: | |
print("Please type in a number between 0 and " + str(len(drives)-1)) | |
except ValueError: | |
print("Please type in a number") | |
################# STEP 1 ## EDIT CMDLINE ############## | |
print('STEP 1: Edit cmdline.txt') | |
# Read in line | |
f = open(drive + 'cmdline.txt', 'r') | |
line = f.read() | |
f.close() | |
n = line.index(st) + len(st) | |
line = line[:n] + ' ' + ins_cmdline + line[n:] | |
# Write | |
f = open(drive + 'cmdline.txt', 'w') | |
f.write(line) | |
f.close() | |
################# STEP 2 ## EDIT CONFIG ############## | |
print('STEP 2: Edit config.txt') | |
f = open(drive + 'config.txt', 'a') | |
f.write(ins_config) | |
f.close() | |
################ STEP 3 ## CREATE SSH FILE ############# | |
print('STEP 3: Create SSH-file') | |
# Create ssh-file | |
open(drive + 'ssh', 'w').close() | |
################ STEP 4 ## DOWNLOAD config-rpi.sh ############# | |
print('STEP 4: Download config-rpi.sh into /boot') | |
r = requests.get(api_url) | |
r = r.json() | |
r = r['files'] | |
r = r[api_fname] | |
content = r['content'] | |
f = open(drive + api_fname, 'w') | |
f.write(content) | |
f.close() | |
sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment