Hello,
I wrote a bash script that opens a telnet connection to a machine. Then again it opens another telnet connection within that machine (telnets to a card which has a local IP) and runs an uptime command. The script is:
#!/bin/bash
MSAN_IP=$1
SLOT_IP=$2
ARGS=2
if [ $# -ne $ARGS ]; then
echo "`basename $0`: Wrong number or parameter supplied"
echo "`basename $0`: Usage: `basename $0` <MSAN_IP> <SLOT_LOCAL_IP>"
exit 0;
else
expect << EOF
set timeout 5
spawn telnet $MSAN_IP 2323
expect "IPCP login: "
send "root\r"
expect "Password: "
send "***\r"
expect "~ # "
send "telnet 10.1.3.$SLOT_IP\r"
expect "60xADSL login: "
send "root\r"
expect "Password: "
send "weblin1\r"
expect "60xADSL# "
send "uptime\r"
expect "60xADSL# "
send "exit\r"
expect “~ # ”
send "exit\r"
exit
EOF
fi
Now...I have a php webpage as an interface for this script.
This is the code that calls the bash script:
exec("/var/www/html/slot_uptime/telnet_pots.sh $msan_ip $slot > /var/www/html/slot_uptime/uptime_pots.txt");
$uptime = exec("cat /var/www/html/slot_uptime/uptime_pots.txt | grep load | awk -F \", load\" '{print \$1}'| awk -F \"up\" '{print \$2}'"); // | awk -F \", load\" '{print \$1}'
if ($uptime == "") {
echo "<h2>Could not retrieve uptime</h2><br>";
}
The script works fine if I run it from the command line.
It even works fine when the php script calls it.
Though, there are 2 machines out of many that when the PHP script calls the bash script, the host closes the connection.
Successful output:
./telnet_adsl.sh 10.193.0.3 4
spawn telnet 10.193.0.3 2323
Trying 10.193.0.3...
Connected to 10.193.0.3.
Escape character is '^]'.
You are logging into Marconi IP Common Part "IPCP"
IPCP login: root
Password:
~ # telnet 10.1.3.4
Entering character mode
Escape character is '^]'.
Linux 2.4.20_mvl31-wds-mips_fp_be (60xADSL) (0)
60xADSL login: root
Password:
this is motd file to inform any information to user
BusyBox v1.00-rc2 (2004.08.03-10:23+0000) Built-in shell (ash)
Enter 'help' for a list of built-in commands.
60xADSL# uptime
06:58:28 up 15 days, 8:10, load average: 0.00, 0.00, 0.00
60xADSL# exit
Connection closed by foreign host.
Not successful:
spawn telnet 10.193.0.5 2323
Trying 10.193.0.5...
Connected to 10.193.0.5.
Escape character is '^]'.
Connection closed by foreign host.
All the machines have the exactly the same software running on them (some sort or linux distro).
Any ideas?
Thanks in advance!