?
Solved

Assign stdout to a variable

Posted on 2010-08-31
2
Medium Priority
?
552 Views
Last Modified: 2013-12-26
Hi.

I`m working in on a script that will be used to ping a remote computer and create a file, that i will read with php, and present on a webpage.

I export a file (input.txt) with php that i will run throug my script and return a file (output.txt).
The input format is like this; Index IP-router IP-server:
1 10.10.10.2 10.2.1.0
2 15.4.2.3 32.12.15.0
3 10.0.0.1 10.3.2.1

And I want the output to look like this (text i npharantes not included in file)
1 0 0 (If ping-commands succeed)
2 1 1 (If both ping command fails)
3 0 1 (If router respondsm and server fails)


I am not able to get the right parts from my input file, assigned to the corresponding variable, any tip?

Regards,
Anders
while read line; do $line

echo $line | cut -d' ' -f1
index=$?
echo $line | cut -d' ' -f2
routerip=$?
echo $line | cut -d' ' -f3
serverip=$?

ping -c1 $routerip | grep -q "64 bytes from $routerip"
routerstatus=$?
ping -c1 $serverip | grep -q "64 bytes from $serverip"
serverstatus=$?

#echo $index
#echo $routerip
#echo $serverip
#echo "$index Router ($routerip) ping status: $routerstatus"
#echo "$index Server ($serverip) status: $serverstatus"
#echo "$index $routerstatus $serverstatus" >>output.txt;
done < input.txt
end

Open in new window

0
Comment
Question by:andoman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
gremwell earned 500 total points
ID: 33569415
<code>
echo $line | cut -d' ' -f1
index=$?
</code>

should be written as
<code>
index=`echo $line | cut -d' ' -f1`
</code>

Also, if you send a single packet, you can ignore the output of ping and just check its exit code:

<code>
abb@d820:~$ ping -c 1 127.0.0.1 > /dev/null 2>&1
abb@d820:~$ echo $?
0
abb@d820:~$ ping -c 1 1.2.3.4 > /dev/null 2>&1
abb@d820:~$ echo $?
1
</code>

Perhaps you want to have a look at <a href="http://nmap.org/book/nping-man.html">nping</a> and <a href="http://www.bitwizard.nl/mtr/">mtr</a> tools. Both can produce machine parseable output and nping can give you a state of TCP ports in addition to ICMP.
0
 

Author Closing Comment

by:andoman
ID: 33569443
Great, thanks. I`ll might look into nmap.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question