Skip to content

Commit

Permalink
Checks and syntax corrections in Linux-specific functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas committed Jun 26, 2012
1 parent ee37ee1 commit a5f5787
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
52 changes: 27 additions & 25 deletions platform/linux/functions
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
. "$platform_path/services"

run_as_current_user(){
if [ "`whoami`" != "$logged_user" ]; then
if [ "$(whoami)" != "$logged_user" ]; then
DISPLAY=:0 sudo su $logged_user -c "$1"
else
eval $1
Expand All @@ -30,18 +30,26 @@ get_netmask(){
ifconfig $1 2> /dev/null | grep "Mask:" | awk '{print $4}' | sed "s/Mask://"
}

get_gateway_ip() {
get_gateway_ip(){
ip r | grep "$1" | grep default | cut -d ' ' -f 3
}

get_wifi_device() {
get_wifi_device(){
[ -z "$wifi_device" ] && wifi_device=$(iwconfig 2>&1 | grep -v "no wireless" | cut -f1 -d" " | grep -v "^$" | tail -1)
}

get_wifi_info() {
get_wifi_info(){
[ -z "$wifi_info" ] && wifi_info=$(iwconfig 2>&1 | grep -v "no wireless")
}

get_open_ssid(){
local iwlist=$(which iwlist)

# access_points=`iwlist $wifi_device | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr`

$iwlist $wifi_device scan | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr | grep "off" | head -1 | cut -d ' ' -f1 | sed 's/"//g'
}

# attempts to connect to the first open public wifi network
# if we dont have NetworkManager available, we use plain iwconfig
try_to_connect() {
Expand All @@ -55,25 +63,23 @@ try_to_connect() {
else

get_wifi_device
if [ -n "$wifi_device" ]; then

# access_points=`iwlist $wifi_device | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr`
local open_essid=`\`which iwlist\` $wifi_device scan | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr | grep "off" | head -1 | cut -d ' ' -f1 | sed 's/"//g'`

if [ -n $open_essid ]; then
log " -- SSID found! Attempting to connect to ${open_essid}..."
`which iwconfig` $wifi_device essid $open_essid
`which dhclient3` $wifi_device
else
log ' -- No open SSIDs found.'
fi

else

if [ -z "$wifi_device" ]; then
log ' !! No wifi device found!'
return 1
fi

local open_ssid=$(get_open_ssid)

if [ -z "$open_ssid" ]; then
log ' -- No open SSIDs found.'
return 1
fi

log " -- SSID found! Attempting to connect to ${open_ssid}..."
$(which iwconfig) $wifi_device essid $open_ssid
$(which dhclient3) $wifi_device

fi

}
Expand Down Expand Up @@ -127,15 +133,11 @@ get_pc_info(){
pc_name=$(hostname)

$(which laptop-detect)
if [ $? == 1 ]; then
pc_type="Desktop"
else
pc_type="Laptop"
fi
[ $? == 1 ] && pc_type="Desktop" || pc_type="Laptop"

get_distro_name
if [ "$distro_name" == "ubuntu" ]; then
pc_os_version=$(lsb_release -r | sed 's/.*\t//g')
pc_os_version=$(lsb_release -r -s)
elif [ "$distro_name" == "debian" ]; then
pc_os_version=$(cat /etc/debian_version)
fi
Expand All @@ -144,7 +146,7 @@ get_pc_info(){

get_distro_name(){

[ -n "$distro_name" ] && return 1
[ -n "$distro_name" ] && return

local proc_version=$(cat /proc/version 2>&1)

Expand Down
8 changes: 6 additions & 2 deletions platform/linux/services
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

linux_load_service(){
get_distro_name
[ -z "$distro_name" ] && return 1
eval "${distro_name}_load_service $1"
}

linux_unload_service(){
get_distro_name
[ -z "$distro_name" ] && return 1
eval "${distro_name}_unload_service $1"
}

Expand All @@ -20,14 +22,16 @@ linux_copy_init_script(){
if [ ! -e "$full_init_script_path" ]; then
ln -s "$(full_path $base_path)/${platform_path}/${2}" "$full_init_script_path" 2> /dev/null
local retval=$?
[ $retval != 0 ] && log " !! Couldn't copy init script into ${1}!"
[ $retval -ne 0 ] && log " !! Couldn't copy init script into ${1}!"
return $retval
fi
return 0
}

linux_remove_init_script(){
rm -f "$1/$2"
local script_path="${1}/${2}"
[ ! -f "$script_path" ] && return 1
rm -f "$script_path"
return $?
}

Expand Down

0 comments on commit a5f5787

Please sign in to comment.