OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [find-linuxes.sh] - Blame information for rev 620

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 548 jeremybenn
#!/bin/bash
2
 
3
# Copyright (C) 2011 Embecosm Limited
4
 
5
# Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
6
 
7
# This file is a script to start up a group of Linux instances
8
 
9
# This program is free software; you can redistribute it and/or modify it
10
# under the terms of the GNU General Public License as published by the Free
11
# Software Foundation; either version 3 of the License, or (at your option)
12
# any later version.
13
 
14
# This program is distributed in the hope that it will be useful, but WITHOUT
15
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17
# more details.
18
 
19
# You should have received a copy of the GNU General Public License along
20
# with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
# ------------------------------------------------------------------------------
23
 
24 565 jeremybenn
# See if telnet or ftp is working
25
 
26
# We call the program with a sequence
27
 
28
# command (telnet or ftp)
29
# wait for prompt ("telnet> "or "ftp> ")
30
# send open <ip address>
31
# wait for prompt ("login: " or "Name (<ip address>:<user>): ")
32
# send root
33
# wait for prompt ("# " or "ftp> ")
34
# send exit
35
# wait for closing message ("Connection closed by foreign host." or
36
#                           "221 Operation successful")
37
 
38
# @param[in] $1  The command to use (ftp or telnet)
39
# @param[in] $2  The IP address
40
# @param[in] $3  The connection prompt (regular expression)
41
# @param[in] $4  The operational prompt (regular expression)
42
# @param[in] $5  The closing message (regular expression)
43
 
44
# @return  0 on success, non-zero otherwise.
45
function check_remote_command {
46
 
47
    command=$1
48
    ip=$2
49
    conn_prompt=$3
50
    op_prompt=$4
51
    close_mess=$5
52
 
53
    expect -f - <<EOF > /dev/null 2>&1
54
    spawn "${command}"
55
 
56
    set timeout 30
57
    expect {
58
        "${command}> " {}
59
        timeout    {exit 1}
60
    }
61
 
62
    send "open ${ip}\n"
63
 
64
    expect {
65
        -re "${conn_prompt}" {}
66
        timeout   {exit 2}
67
    }
68
 
69
    send "root\n"
70
 
71
    expect {
72
        -re "${op_prompt}" {}
73
        timeout   {exit 3}
74
    }
75
 
76
    send "exit\n"
77
 
78
    expect {
79
        -re "${close_mess}" {exit 0}
80
        timeout {exit 4}
81
    }
82
EOF
83
 
84
    return $?
85
}
86
 
87
 
88
# See if telnet is working.
89
 
90
# We call check_remote_command with appropriate arguments
91
 
92
# @param[in] $1  The IP address
93
 
94
# @return  0 on success, non-zero otherwise.
95
function check_telnet {
96
    check_remote_command telnet $1 "^.* login: " "^.*# " \
97
        "Connection closed by foreign host."
98
 
99
    return $?
100
}
101
 
102
 
103
# See if FTP is working.
104
 
105
# We call check_remote_command with appropriate arguments
106
 
107
# @param[in] $1  The IP address
108
 
109
# @return  0 on success, non-zero otherwise.
110
 
111
function check_ftp {
112
    check_remote_command ftp $1 "Name \\\($1:.*\\\): " "ftp> " \
113
        "221 Operation successful"
114
 
115
    return $?
116
}
117
 
118
 
119
# See if telnet and FTP are working.
120
 
121
# We combine calls to check_telnet and check_ftp. Note that we do check both,
122
# even though we really only need to check one for failure. However in the
123
# future the additional information may be useful.
124
 
125
# @param[in] $1  The IP address
126
 
127
# @return  0 on success, non-zero (10 x telnet failure + ftp failure)
128
#          otherwise.
129
function check_telnet_ftp {
130
    check_telnet $1
131
    res_telnet=$?
132
    check_ftp $1
133
    res_ftp=$?
134
 
135
    return $(( $res_telnet * 10 + $res_ftp ))
136
}
137
 
138
 
139 548 jeremybenn
# IP list excluding all known IPs
140 553 jeremybenn
ip_list="192.168.0.2-39,41-59,61-79,82-127,129,130,133-255"
141 548 jeremybenn
ip_file=`dirname ${DEJAGNU}`/ip-avail.txt
142 565 jeremybenn
tmp_file=/tmp/find-linuxes-$$
143 548 jeremybenn
 
144
# Keep using nmap until we get the same number of IP addresses twice running.
145
nmap -sP -n ${ip_list} | \
146
    sed -n -e 's/Nmap scan report for //p' | \
147
    sort > ${ip_file}
148
len1=`wc -l ${ip_file} | cut -d " " -f 1`
149
nmap -sP -n ${ip_list} | \
150
    sed -n -e 's/Nmap scan report for //p' | \
151
    sort > ${ip_file}
152
len2=`wc -l ${ip_file} | cut -d " " -f 1`
153
 
154
while [ ${len1} -ne ${len2} ]
155
do
156
    len1=${len2}
157
    nmap -sP -n ${ip_list} | \
158
        sed -n -e 's/Nmap scan report for //p' | \
159
        sort > ${ip_file}
160
    len2=`wc -l ${ip_file} | cut -d " " -f 1`
161
done
162
 
163 565 jeremybenn
# If we have a -c flag, check that the targets respond to telnet and ftp
164
if [ $# > 0 -a "x$1" == "x-c" ]
165
then
166
    mv ${ip_file} ${tmp_file}
167
    touch ${ip_file}
168
 
169
    len=`wc -l ${tmp_file} | cut -d " " -f 1`
170
    while [ ${len} -gt 0 ]
171
    do
172
        # Get each IP address in turn
173
        ip=`tail -${len} ${tmp_file} | head -1`
174
        len=$(( ${len} - 1 ))
175
 
176
        check_telnet_ftp ${ip}
177
        res=$?
178
 
179
        if [ ${res} -eq 0 ]
180
        then
181
            echo ${ip} >> ${ip_file}
182
        else
183
            echo "${ip} failed to respond to telnet/FTP: result ${res}"
184
        fi
185
    done
186
 
187
    rm ${tmp_file}
188
fi
189
 
190 548 jeremybenn
# Print the results
191
cat ${ip_file}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.