Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to I grep the output of this command ?

Posted on 2012-09-17
8
Medium Priority
?
636 Views
Last Modified: 2012-09-19
If I Have key setup, I run the scp line and I get the redhat relase line back
scp     -o  PasswordAuthentication=no 10.10.10.10:/etc/redhat-release  .
redhat-release                                  100%   54     0.1KB/s   00:00
 


 do not have key setup I get the "pemission denied" line back
scp     -o  PasswordAuthentication=no 10.10.10.13:/etc/redhat-release  .
Permission denied (publickey,password).


I want to "grep 'publickey | wc -l' the output of the command


I have tried 2>&1 and different options to no avail.  

what am I not understanding ?
0
Comment
Question by:TIMFOX123
  • 4
  • 3
8 Comments
 
LVL 81

Expert Comment

by:arnold
ID: 38408049
What is it you want to get, if you want to check whether the retrieval occurred, use the exit code of the scp as the check
scp
If [ $? -eq 0 ]; then
   Echo command executed successfully
Else
  Echo command failed
Fi

If you must know the cause, you could use Case instead of the if
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html

Case $? In
0) echo good
;;
127) echo error
;;
.
.
.
Esac

If you have the scp scripted, you could also use the IP of the server you are connecting to as the name of the local file
Scp -o 10.0.0.10:/etc/redhat-release redhat-release.10.0.0.10
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 38408426
From our previous thread I know that you're after checking whether the key of your userid is present at the target host or not.

So why use scp instead of simply ssh?

ssh 10.10.10.10 -o PasswordAuthentication=no "date" >/dev/null 2>&1
if  [[ $? -ne 0 ]] ; then
    echo "Connection attempt failed"
  else
      echo "Connection attempt succeeded"
fi

It doesn't make much sense testing for particular returncodes here, because ssh would almost always set "255" in case of a failure.

By the way, when using this syntax your scp version should give you a "1" in case of such a failure:

scp -o  PasswordAuthentication=no 10.10.10.10:/etc/redhat-release  .  2>&1 | grep publickey |wc -l

wmp
0
 

Author Comment

by:TIMFOX123
ID: 38409672
wmp
you were 100% correct.  I 'thought" that would connect me with the box instead of just test the connection.  my bad

I have an issue the script just stops on serveral hosts and will not "time out" or anyting.  Just sits there. I have to stop the script, delete the host from my datafile and rerun the script.

Any suggestions on changes to this script ?

ssh 10.10.10.10 -o PasswordAuthentication=no "date" >/dev/null 2>&1
if  [[ $? -ne 0 ]] ; then
    echo "Connection attempt failed"
  else
      echo "Connection attempt succeeded"
fi
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38409738
The stopping is most probably due to a host being unreachable (wrong address?)

Unfortunately we do not have a real "timeout" in the ssh client.

So you should run a preliminary "ping" test, e.g. like this:

Host=10.10.10.10
if ping -c1 -w1 $Host >/dev/null 2>&1 ; then
  ssh $Host -o PasswordAuthentication=no "date" >/dev/null 2>&1
    if  [[ $? -ne 0 ]] ; then
        echo "Connection attempt failed"
      else
          echo "Connection attempt succeeded"
    fi
 else echo "$Host is unreachable"
fi
0
 

Author Comment

by:TIMFOX123
ID: 38410068
Actually wmp, I did the ping first

in the words of Yoda  " confused am I "
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38410089
So am I  ...
0
 

Author Comment

by:TIMFOX123
ID: 38412768
wmp

it is a firewall issue.  

can not say more

got it and thank you very much.
0
 

Author Closing Comment

by:TIMFOX123
ID: 38412771
thx
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
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 Month10 days, 7 hours left to enroll

571 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