hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
 
wpa_ret ( )
{
   if [ $1 != "OK" ]
       then
       if [ $3 == 1 ]
           then
           wpa_cli remove_network $2>/dev/null
           echo "wpa_cli command failed on $2"
       fi
       exit 1
   fi
}
 
connect_ap ( )
{
   #if [ $1 -eq 1 ]
       #then
       ssid=\""$2"\"
       pw=\""$3"\"
       declare -i scantimes=1
       nw_no=$(wpa_cli add_network | sed '1d')
       ret=$(wpa_cli set_network $nw_no ssid $ssid | sed '1d')
       echo "Set ssid for nw $nw_no: $ret"
       wpa_ret $ret $nw_no 1
       if [ $5 == "WPA" -o $5 == "WPA2" ]
           then
           ret=$(wpa_cli set_network $nw_no psk $pw | sed '1d')
                           echo "Set PSK: $ret"
                        wpa_ret $ret $nw_no 1
       elif [ $5 == "WEP40" -o $5 == "WEP128" -o $5 == "OPEN" ]
           then
           ret=$(wpa_cli set_network $nw_no key_mgmt NONE | sed '1d')
                           echo "Set Key-mgmt: $ret"
                        wpa_ret $ret $nw_no 1
           ret=$(wpa_cli set_network $nw_no auth_alg OPEN | sed '1d')
                        echo "Set Aith-alg: $ret"
                        wpa_ret $ret $nw_no 1
           if [ $5 != "OPEN" ]
               then
               ret=$(wpa_cli set_network $nw_no wep_key0 $pw | sed '1d')
                            echo "Set wep key: $ret"
                            wpa_ret $ret $nw_no 1
           fi
       fi 
       #echo "Set proto"
       #ret=$(wpa_cli set_network $nw_no proto WPA | sed '1d')
       #wpa_cli status #echo $ret
       #wpa_ret $ret $nw_no 1    
       #echo "Set key_mgmt"
       #ret=$(wpa_cli set_network $nw_no key_mgmt WPA-PSK | sed '1d')
       #wpa_cli status #echo $ret
       #wpa_ret $ret $nw_no 1    
       #echo "Set pairwise"
       #ret=$(wpa_cli set_network $nw_no pairwise CCMP | sed '1d')
       #wpa_cli status #echo $ret
       #wpa_ret $ret $nw_no 1    
       #echo "Set group"
       #ret=$(wpa_cli set_network $nw_no group CCMP | sed '1d')
       #wpa_cli status #echo $ret
       #wpa_ret $ret $nw_no 1    
       ret=$(wpa_cli enable_network $nw_no | sed '1d')
       echo "Enable nw $nw_no: $ret"
       wpa_ret $ret $nw_no 1    
       #echo "Connect"
       #ret=$(wpa_cli reconnect | sed '1d')
       #wpa_cli status #echo $ret
       #wpa_ret $ret $nw_no 1    
       state=$(wpa_cli status -i $1 | grep wpa_state | sed 's/wpa_state=//g')
       form_st="INACTIVE"
       while [ $state != "COMPLETED" ]
       do
           if [ $form_st != $state ]
               then
               echo "Status: $state"
           fi
           if [ $state == "SCANNING" ]
               then
               sleep 1
           elif [ $state == "DISCONNECTED" ]
               then
               if [ $scantimes <= $4]
                   then
                   echo "Connect"
                            ret=$(wpa_cli reconnect | sed '1d')
                   wpa_ret $ret $nw_no 1
                   $scantimes=$scantimes + 1
               else
                   echo "exceed scan times=$scantimes"
               fi
           fi
           form_st=$state
           state=$(wpa_cli status -i $1 | grep wpa_state | sed 's/wpa_state=//g')
       done
       if [ $state == "COMPLETED" ]
           then
           echo "Connected and request for IP address"
           dhclient -4 $1 >/dev/null
       fi
   #fi
}
 
if [ $# -eq 5 ]
   then
   echo "Scanning"
   declare -i total_scantime=$4
        for((i=1; i<=$4; i=i+1))
   do
       echo "scan loop:$i" 
       scan_st=$(wpa_cli scan -i $1)
       total_scantime=total_scantime-1
       sleep 2
       result=$(wpa_cli scan_results -i $1 | grep -c $2)
       if [ $result -eq 1 ]
           then
           echo "Target AP $2 is found."
           break;
       fi
   done
        if [ $result -eq 1 ]
       then
       connect_ap $1 $2 $3 $total_scantime $5
   else
       echo "Target ap is not found"
   fi
 
 
elif [ $3 == "off" ]
   then
   echo "Disconnect AP"
   dhclient -r $1 >/dev/null
   dhclient -x >/dev/null 
   nw_no=$(wpa_cli list_network | grep CURRENT | awk '{print $1}')
   echo "Disable network"
   ret=$(wpa_cli disable_network $nw_no | sed '1d')
   wpa_ret $ret $nw_no 1    
   echo "Remove network"
   ret=$(wpa_cli remove_network $nw_no | sed '1d')
   wpa_ret $ret $nw_no 0    
else
   echo "====================================================================================="
   echo "manual scan & connect script"
   echo "./scan_conn.sh <interface> <BSSID> <PW> <scan_times>"
   echo "./scan_conn.sh <interface> <BSSID> off"
   echo "====================================================================================="
fi