Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm terrible when it comes to Unix scripting so I'm asking for some help. I have a list of IP addresses sometimes up to 2 or 3 hundred. What I have to do is telnet to each device in the list. I made a file containing all of these devices and put a simple script to read each of the addresses. I looks like this:
$ more script
#!/bin/sh
for device in `cat /export/home/rjohnson/scriptstuff/file3`
do
telnet $device
done
$How could I put something in this script to fail to the next device listed if unable to telnet to one of the devices in the file?
Here's what's up: There are some addresses in the file that are no longer used or are not reachable. When telnet gets to that address, it just keeps trying and trying. I end up doing a control C, deleting all addresses up to and including that one and starting the script over again.
Here's what else I'd like to do to this.
When you telnet to a device that has TACACs configured, it will prompt you for Username:. If TACACs isn't configured, it will prompt for Password:. I would like to have the script look at what is returned; Username: or Password:. If it is Username:, I'd like to have it put in my username which is johnsonr - I will type in the password for that username. If it prompts for password I'd don't want anything done...just wait for input at the keyboard (i.e. the password).

The bourne shell does not lend itself well to interactivity as required by a telnet login. Have a look at expect.
The following is an sample expect script.
set username "johnsonr "
set password "password"
while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "ogin"
send "$username\r"
expect "Password"
send "$password\r"
send "\r\r"
"Welcome" {
incr pass0
set n 5
set p 0
close
}
"from" {
send "id\r"
expect {
$username {
incr pass0
set n 5
close
}
}
}
"login: $" {
set pass1 0
puts stdout "Expect return: $expect_out(0,string)"
puts stdout "Failed login"
send "\x1d\x0d"
expect "telnet"
send "close\r"
send "quit\r"
close
}
}
incr n
}

![]() |
auto file transfer from u...
|
mpstat & vmstat contr...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |