Ping: redirect output and error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ping: redirect output and error
# 1  
Old 03-30-2005
Ping: redirect output and error

Hi, I use this function (now modified with elif) for check if a pc is up:
Code:
check_pc() {
 
  $PING $PC 1 2> /dev/null

  if [ $? -eq 0 ]; then
   
    check_dir #Other function
   
    echo "Begin backup operation for $PC"

    echo "$SMBTAR -s $PC -u $USER -p $PASS -x $SHARE$EXCL -t - | gzip -c > $PC.tar.gz"
   
    echo "End backup operation for $PC"
    echo ""    

  elif [ $? -eq 1 ]; then  
     
    echo "Unable to ping $PC"
    echo ""

  else  
    #Not in /etc/hosts
    echo "Unable to find $PC"
    echo ""

  fi

} #end check_pc

But.. With
Code:
$PING $PC 1 2> /dev/null

output like no answer from pc3 don't finish in /dev/null ... If I try
Code:
$PING $PC 1 > /dev/null

don't finish in /dev/null /usr/sbin/ping: unknown host pc123. How redirect error and output in /dev/null?
And.. can I use ping error to write in a log file "Unable to find $PC".. now the last else don't work. I use /bin/sh ... Thanks, bye.
# 2  
Old 03-30-2005
You can redirect output and error mode data into /dev/null as,

ping <IP> 1>/dev/null 2>&1
ping <IP> 1>/dev/null 2>/dev/null

ping <ip> 1>/tmp/logfile 2>&1
it will redirect into /tmp/logfile

ping <ip> 1>/tmp/logfile 2>errorfile
output to logfile and error to errorfile.

HTH.
# 3  
Old 03-30-2005
Quote:
Originally Posted by muthukumar
ping <IP> 1>/dev/null 2>&1
ping <IP> 1>/dev/null 2>/dev/null
Ok, it works. Smilie
Quote:
Originally Posted by muthukumar
ping <ip> 1>/tmp/logfile 2>errorfile
output to logfile and error to errorfile.
If I want do something for 0, 1 or 2 ping result? Like
Code:
if [PINGOK]

  //do_something

elif [PINGRETURN1]

  //do_something1

else #error

  //do_something2

Smilie
# 4  
Old 03-31-2005
You can get the result code from ping as,

ping <ip> 1>/dev/null 2>&1
ret=$?

if [[ $ret -eq 2 ]]
then
action..
elif [[ $ret -eq 1 ]]
then
action
elfi [[$ret -eq 0 ]]
then
action..
fi

HTH.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

2. IP Networking

Linux load balancer ping redirect to other interface

Im configuring centos with load balance with ip route and ip rule Eth0 192.168.1.5 Eth1 192.168.5.128 # ip route 192.168.5.0/24 dev eth1 scope link src 192.168.5.128 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.5 169.254.0.0/16 dev eth1 scope link # ip route show... (2 Replies)
Discussion started by: hadinetcat
2 Replies

3. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

4. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

5. Shell Programming and Scripting

Redirect Output and Error in 2 different files

hi, i want to redirect my output & error if generated to two different files. I have written the code for redirecting the output, i dnt have ne idea how to go abt it for errors. I tried spooling which was given in one of the threads on this forum.But it didn't work.The script i wrote as a lot... (4 Replies)
Discussion started by: bankimmehta
4 Replies

6. Shell Programming and Scripting

Ambiguous output redirect error

Hi everyone, While I was trying to do DATE=`date +"%Y%m%d_%H%M%S"` STARTLOG=$TUXSTDDIR/start_$DATE.log tmboot -y > $STARTLOG 2>&1 I got an error i.e. Ambiguous output redirect error. Here the first part is to boot the account so there is nothing wrong with that.... (6 Replies)
Discussion started by: pareshan
6 Replies

7. Shell Programming and Scripting

Redirect Output

Hi, I would like to list files: ls *.hdf But I would like a copy of the output directed to the screen, but also APPENDED to a text file: test.txt I have tried: ls *.hdf | tee test.txt However, that will just write over everything already existing in test.txt. How can I append the... (1 Reply)
Discussion started by: msb65
1 Replies

8. Shell Programming and Scripting

redirect only the standard error output to mail

I'm writing a script using file descriptor 2 (std error) to send an email only if the command fails or errors out but the script always emails me irrepective of whether it fails or not. It will not email the /tmp/check.error file output if doesn't error out just the mail with the subject "Cannot... (3 Replies)
Discussion started by: barkath
3 Replies

9. Shell Programming and Scripting

How redirect output(error and normal) to 2 different files

Hello, I have a java program which i am calling in shell script. I wanted to redirect output to 2 differetn files. Output should have both 1 & 2 (normal and error) in both file. pls help (2 Replies)
Discussion started by: balareddy
2 Replies

10. Shell Programming and Scripting

Redirect output

Hi all, I have a script which call a java program, the logging (to log file) in the program is done using log4j. However, as a safety measure, i still choose to direct standard error to another log file as follow /usr/bin/java -classpath ${classpath} -Xmx128m TestingProgram 2>>... (1 Reply)
Discussion started by: mpang_
1 Replies
Login or Register to Ask a Question