Skip to content

Instantly share code, notes, and snippets.

@natsumerinchan
Last active December 7, 2023 14:54
Show Gist options
  • Save natsumerinchan/6c938e4ffc84c7b68eaba5adb9848946 to your computer and use it in GitHub Desktop.
Save natsumerinchan/6c938e4ffc84c7b68eaba5adb9848946 to your computer and use it in GitHub Desktop.

Module Installer

These scripts are used to install Magisk/KernelSU modules to Android Device by PC

You should install platform-tools(include adb,fastboot...) at first.

module_installer.ps1 : Support Windows,Linux and Mac.(Need install powershell
and type Set-ExecutionPolicy RemoteSigned,Mac has not tested.)

module_installer.sh : Support Linux.

How to use?

# module_installer.sh
./module_installer.sh <module_path> [device]

# module_installer.ps1
pwsh ./module_installer.ps1 <module_path> [device]
Function ui_print {
param($message)
Write-Host $message
}
Function print_title {
param([string]$line1, [string]$line2)
$line1len = $line1.Length
$line2len = $line2.Length
$len = [Math]::Max($line1len, $line2len)
$len += 2
$bar = '*' * $len
ui_print $bar
ui_print " $line1 "
if ($line2) {
ui_print " $line2 "
}
ui_print $bar
}
Function abort {
param([string]$message)
ui_print $message
exit 1
}
Function push_module {
param([string]$modPath, [string]$device)
if (-not $modPath) {
abort "! You have not chosen a module file to install!"
}
$cachePath = Join-Path $HOME "module.zip"
Copy-Item $modPath $cachePath -Force
$adbCommand = if (-not $device) {
"adb shell rm -rf /data/local/tmp/module.zip; adb push `"$cachePath`" /data/local/tmp"
} else {
"adb -s $device shell rm -rf /data/local/tmp/module.zip; adb -s $device push `"$cachePath`" /data/local/tmp"
}
Invoke-Expression $adbCommand
Remove-Item $cachePath
}
Function module_install {
param([string]$device)
if (-not $device) {
ui_print "- Selected Default Device"
} else {
ui_print "- Selected Device: $device"
}
$ROOTMETHOD = adb shell su -v | Select-String -Pattern ':' | ForEach-Object { $_.ToString().Split(':')[1].Trim() }
$installCommand = if ($ROOTMETHOD -eq "KernelSU") {
if (-not $device) {
"adb shell su -c /data/adb/ksud module install /data/local/tmp/module.zip"
} else {
"adb -s $device shell su -c /data/adb/ksud module install /data/local/tmp/module.zip"
}
} else {
if (-not $device) {
"adb shell su -c magisk --install-module /data/local/tmp/module.zip"
} else {
"adb -s $device shell su -c magisk --install-module /data/local/tmp/module.zip"
}
}
Invoke-Expression $installCommand
adb shell rm -rf "/data/local/tmp/module.zip" | Out-Null
}
# Check for help argument
if ($args[0] -eq "-h") {
ui_print "Module Installer"
ui_print "usage: $PSCommandPath <mod_path> [device]"
exit
}
# Clear the screen
Clear-Host
# Run Functions
print_title "Module Installer"
$ROOTMETHOD = adb shell su -v | Select-String -Pattern ':' | ForEach-Object { $_.ToString().Split(':')[1].Trim() }
if (-not $ROOTMETHOD) {
abort "! No device detected or multiple devices exist."
}
ui_print "- Root Method: $ROOTMETHOD"
push_module $args[0] $args[1]
module_install $args[1]
#!/bin/bash
ui_print() {
echo "$1"
}
print_title() {
local len line1len line2len bar
line1len=$(echo -n $1 | wc -c)
line2len=$(echo -n $2 | wc -c)
len=$line2len
[ $line1len -gt $line2len ] && len=$line1len
len=$((len + 2))
bar=$(printf "%${len}s" | tr ' ' '*')
ui_print "$bar"
ui_print " $1 "
[ "$2" ] && ui_print " $2 "
ui_print "$bar"
}
abort() {
ui_print "$1"
exit 1
}
push_module() {
if [ ! $1 ]; then
abort "! You have not choose module file to install!"
fi
cp "$1" "$HOME/.cache/module.zip"
if [ ! $2 ]; then
adb shell rm -rf "/data/local/tmp/module.zip"
adb push "$HOME/.cache/module.zip" "/data/local/tmp" > /dev/null
else
adb -s $2 shell rm -rf "/data/local/tmp/module.zip"
adb -s $2 push "$HOME/.cache/module.zip" "/data/local/tmp" > /dev/null
fi
rm "$HOME/.cache/module.zip"
}
module_install() {
if [ ! $1 ]; then
ui_print "- Selected Default Device"
if [ "$ROOTMETHOD" == "KernelSU" ]; then
adb shell su -c /data/adb/ksud module install "/data/local/tmp/module.zip"
else
adb shell su -c magisk --install-module "/data/local/tmp/module.zip"
fi
else
ui_print "- Selected Device: $1"
if [ "$ROOTMETHOD" == "KernelSU" ]; then
adb -s $1 shell su -c /data/adb/ksud module install "/data/local/tmp/module.zip"
else
adb -s $1 shell su -c magisk --install-module "/data/local/tmp/module.zip"
fi
fi
adb shell rm -rf "/data/local/tmp/module.zip"
}
if [ "$1" == "-h" ]; then
ui_print "Module Installer"
ui_print "usage: ./module_installer.sh <mod_path> <device>"
exit
fi
clear
print_title "Module Installer"
adb shell echo "Hello World!" > /dev/null || abort "! No device detect or multiple devices exist."
ROOTMETHOD=$(adb shell su -v | awk -F ':' '{print $2}')
ui_print "- Root Method: $ROOTMETHOD"
push_module $1 $2
module_install $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment