expect自测练习
#!/bin/bash
for ((i=128 ;i<=255; i++))
do
echo $i
str=`echo "quit" | nc -w 1 "192.168.113.$i" 22 |sed 's/^[ ]*//;s/[ ]*$//'`
if [ -n "$str" ]
then
echo "=====ssh 192.168.113.$i======="
expect test.exp qiqi 123123 192.168.113.$i
fi
done
test.exp
proc usage {} {
puts stderr "usage: $::argv0 username password ipaddress"
exit 1
}
proc connect {pass} {
expect {
"(yes/no)?" {
send "yes\n"
expect "*password:" {
send "$pass\n"
expect {
"*#" {
return 0
}
}
}
}
"*password:" {
send "$pass\n"
expect {
"*]*" {
send "/sbin/ifconfig\n"
return 0
}
}
}
}
return 1
}
if {$argc != 3} { usage }
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostip [lindex $argv 2]
spawn ssh ${username}@${hostip}
connect $password
if {[connect $password]} {
exit 1
}
expect eof