-
-
Save penguin2716/6409429 to your computer and use it in GitHub Desktop.
諸事情により他人の権限でaptを叩くことになりました
This file contains 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
#!/bin/bash | |
# package agent script | |
# you can run apt command by other user's permission. | |
set -e | |
PASSWORD_FILE=$HOME/.pa/password | |
USERNAME_FILE=$HOME/.pa/username | |
function gen_password_file { | |
echo "Initialize user information" | |
echo "password and username file will be placed in ~/.pa" | |
read -p "management user: " management_user | |
read -p "password for $management_user: " -s password | |
echo | |
read -p "encryption key: " -s encryption_key | |
echo | |
mkdir -p $HOME/.pa | |
echo $management_user | openssl des-ede3-cbc -e -k "$encryption_key" > $HOME/.pa/username | |
echo $password | openssl des-ede3-cbc -e -k "$encryption_key" > $HOME/.pa/password | |
} | |
function get_userinfo { | |
if [ -n "$encryption_key" ]; then | |
decryption_key=$encryption_key | |
else | |
read -p "decryption key: " -s decryption_key | |
echo | |
fi | |
exec_user=`cat $USERNAME_FILE | openssl des-ede3-cbc -d -k "$decryption_key"` | |
password=`cat $PASSWORD_FILE | openssl des-ede3-cbc -d -k "$decryption_key"` | |
} | |
function auto-apt-get { | |
if [ -z "$decryption_key" ]; then | |
get_userinfo | |
fi | |
expect -c " | |
set timeout -1 | |
spawn su -c \"sudo apt-get $@\" $exec_user | |
expect \"Password:\" | |
send \"$password\n\" | |
expect \"password for $exec_user:\" | |
send \"$password\n\" | |
interact | |
" | |
} | |
function show_help { | |
echo "usage: pa [<action> [<package>]|<package>] | |
action: | |
update system with no augument. | |
f: fix package tree | |
u: update system | |
i <package>: install <package> | |
s <package>: seach <package> | |
example: | |
pa | |
update system | |
pa f | |
fix package tree | |
pa i emacs | |
pa emacs | |
install emacs | |
pa s emacs | |
search emacs from package tree | |
* password and username file are placed in ~/.pa | |
" | |
} | |
if [ ! \( -f $PASSWORD_FILE -a -f $USERNAME_FILE \) ]; then | |
gen_password_file | |
fi | |
if [ $# -eq 0 ]; then | |
echo "[update system]" | |
auto-apt-get update | |
auto-apt-get dist-upgrade -y | |
elif [ $# -eq 1 ]; then | |
case "$1" in | |
f ) | |
echo "[fix package tree]" | |
auto-apt-get install -f | |
;; | |
u ) | |
echo "[update system]" | |
auto-apt-get update | |
auto-apt-get dist-upgrade -y | |
;; | |
??* ) | |
echo "[install package] $1" | |
auto-apt-get install $1 -y | |
;; | |
* ) | |
show_help | |
;; | |
esac | |
elif [ $# -eq 2 ]; then | |
case "$1" in | |
i ) | |
echo "[install package] $2" | |
auto-apt-get update | |
auto-apt-get install $2 -y | |
;; | |
s ) | |
echo "[search package] $2" | |
apt-cache search $2 | |
;; | |
* ) | |
show_help | |
;; | |
esac | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment