~popey/phablet-tools/download-only

« back to all changes in this revision

Viewing changes to phablet-deploy-networking

  • Committer: Sergio Schvezov
  • Date: 2013-02-06 19:31:17 UTC
  • mfrom: (40.1.5 trunk)
  • Revision ID: sergio.schvezov@canonical.com-20130206193117-2nrqgox676i8qut5
Initial public release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
# This program is free software: you can redistribute it and/or modify it
3
 
# under the terms of the the GNU General Public License version 3, as
4
 
# published by the Free Software Foundation.
5
 
#
6
 
# This program is distributed in the hope that it will be useful, but
7
 
# WITHOUT ANY WARRANTY; without even the implied warranties of
8
 
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
9
 
# PURPOSE.  See the applicable version of the GNU Lesser General Public
10
 
# License for more details.
11
 
#.
12
 
# You should have received a copy of the GNU General Public License
13
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
 
#
15
 
# Copyright (C) 2012 Canonical, Ltd.
16
 
 
17
 
set -e
18
 
 
19
 
export LC_ALL=C
20
 
 
21
 
usage() {
22
 
cat <<EOF
23
 
usage: $0 [OPTIONS]
24
 
 
25
 
Copies ACTIVE network manager connection into device
26
 
 
27
 
OPTIONS:
28
 
  -h    Show this message
29
 
  -s    Specify the serial of the device to install (see adb $ADBOPTS devices)
30
 
  -i    Install openssh-server in chroot
31
 
  -n    Select network file
32
 
 
33
 
EOF
34
 
}
35
 
 
36
 
ADBOPTS=""
37
 
OPTION_SSH=0
38
 
OPTION_NETWORK_FILE=""
39
 
 
40
 
while getopts "hin:s:" OPTION
41
 
do
42
 
    case $OPTION in
43
 
        h)
44
 
            usage
45
 
            exit 1
46
 
            ;;
47
 
        n)
48
 
            OPTION_NETWORK_FILE="$OPTARG"
49
 
            ;;
50
 
        i)
51
 
            OPTION_SSH=1
52
 
            ;;
53
 
        s)
54
 
            ADBOPTS="-s $OPTARG"
55
 
            ;;
56
 
        ?)
57
 
            usage
58
 
            exit
59
 
            ;;
60
 
    esac
61
 
done
62
 
shift $(($OPTIND - 1))
63
 
 
64
 
NETWORK_MANAGER=/etc/NetworkManager/system-connections
65
 
 
66
 
find_active_network() {
67
 
    wireless_adapter="$(nmcli -t -f device,type dev | egrep wireless | cut -d: -f1)"
68
 
    network_active="$(nmcli -t -f name,devices,vpn con status | egrep $wireless_adapter | egrep ":no$"| cut -d: -f1)"
69
 
    network_file=$(sudo grep "$network_active" $NETWORK_MANAGER/* |grep ssid | cut -d: -f1)
70
 
 
71
 
    if [ -z "$network_active" ]
72
 
    then
73
 
        echo "No active wifi network connection, exiting"
74
 
        exit 1
75
 
    fi
76
 
    echo "$network_file"
77
 
}
78
 
 
79
 
if [ -z $OPTION_NETWORK_FILE ]; then
80
 
    network_file=$(find_active_network)
81
 
else
82
 
    network_file=$OPTION_NETWORK_FILE
83
 
fi
84
 
 
85
 
if [ ! -f "$network_file" ]
86
 
then
87
 
    echo "Network connection file \"$network_file\" cannot be read"
88
 
    exit 1
89
 
fi
90
 
 
91
 
echo Network file is $network_file
92
 
 
93
 
TMP_FILE=$(mktemp)
94
 
sudo grep -v mac-address "$network_file" > $TMP_FILE
95
 
 
96
 
echo Provisioning network file to device
97
 
adb $ADBOPTS root
98
 
sleep 3
99
 
target_network_file=/data/ubuntu/$NETWORK_MANAGER/active_ws_connection.conf
100
 
adb $ADBOPTS push $TMP_FILE $target_network_file
101
 
adb $ADBOPTS shell "/system/bin/chmod 600 $target_network_file"
102
 
 
103
 
rm -f $TMP_FILE
104
 
 
105
 
echo
106
 
echo Network setup complete
107
 
 
108
 
if [ $OPTION_SSH -eq 1 ]; then
109
 
    TMP_FILE=$(mktemp)
110
 
    # Indentation is going to be ugly here
111
 
    cat > $TMP_FILE << 'EOF'
112
 
#!/bin/bash
113
 
export PATH="/sbin:/usr/sbin:/bin:/usr/bin" 
114
 
apt-get update
115
 
apt-get install --yes openssh-server
116
 
EOF
117
 
 
118
 
    echo Installing ssh
119
 
    UBUNTU_ROOT="/data/ubuntu"
120
 
    adb $ADBOPTS push $TMP_FILE $UBUNTU_ROOT/$TMP_FILE
121
 
    adb $ADBOPTS shell chmod 755 $UBUNTU_ROOT/$TMP_FILE
122
 
    adb $ADBOPTS shell chroot $UBUNTU_ROOT $UBUNTU_ROOT/$TMP_FILE
123
 
 
124
 
    rm -f $TMP_FILE
125
 
 
126
 
    echo
127
 
    echo openssh-server install complete, to connect execute
128
 
    echo adb $ADBOPTS forward tcp:PORT tcp:22
129
 
    echo with a proper PORT, e.g.\; 8888
130
 
fi
131