Ravendark Posted February 23, 2012 Share Posted February 23, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/257600-telnet-php-and-bash-together/ Share on other sites More sharing options...
requinix Posted February 23, 2012 Share Posted February 23, 2012 Though, there are 2 machines out of many that when the PHP script calls the bash script, the host closes the connection. So it works when you try the bash script manually? What if you try manually running the exact same command the PHP script runs? Quote Link to comment https://forums.phpfreaks.com/topic/257600-telnet-php-and-bash-together/#findComment-1320332 Share on other sites More sharing options...
Ravendark Posted February 23, 2012 Author Share Posted February 23, 2012 Do you mean running the php command from the command line? Quote Link to comment https://forums.phpfreaks.com/topic/257600-telnet-php-and-bash-together/#findComment-1320338 Share on other sites More sharing options...
requinix Posted February 23, 2012 Share Posted February 23, 2012 Running the same command PHP tries to run: $ /var/www/html/slot_uptime/telnet_pots.sh $msan_ip $slot > /var/www/html/slot_uptime/uptime_pots.txt (after substituting values for those two variables, of course). And as a sanity check, echo out that command and make sure it is what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/257600-telnet-php-and-bash-together/#findComment-1320349 Share on other sites More sharing options...
Ravendark Posted February 23, 2012 Author Share Posted February 23, 2012 yes it works fine Quote Link to comment https://forums.phpfreaks.com/topic/257600-telnet-php-and-bash-together/#findComment-1320350 Share on other sites More sharing options...
requinix Posted February 23, 2012 Share Posted February 23, 2012 Does the remote software offer any logs you can look at? Are the two machines the last in the "list" or are they located at random within? If you shuffle the "list" does the situation change? In your shoes I would be trying three things: finding more information (eg, logs, adding whatever debugging/verbosity options you have available), looking for patterns and similarities and differences (eg, two machines on a different subnet, not all running the same software with the same settings), and trying to change the outcome by manipulating the circumstances (eg, running the commands manually, rearranging lists). Quote Link to comment https://forums.phpfreaks.com/topic/257600-telnet-php-and-bash-together/#findComment-1320352 Share on other sites More sharing options...
Ravendark Posted February 24, 2012 Author Share Posted February 24, 2012 Well it seems that I found what was wrong. I forced the php "exec" to use bash $uptime = exec("/bin/bash /var/www/html/slot_uptime/telnet_pots.sh $msan_ip $slot | grep load | awk -F \", load\" '{print \$1}' | awk -F \"up\" '{print \$2}'"); works ok now, thanks for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/257600-telnet-php-and-bash-together/#findComment-1320721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.