Remote ssh and return values..

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remote ssh and return values..
# 1  
Old 05-10-2010
Remote ssh and return values..


I'm executing below 2 cmds which is working file.. ( cmd will ssh to remote host and look for pattern in remote file)

ssh $USER@$HOST "grep -n \"$PATTERN\" $RDIR/$RFILE | awk -F":" '{print \$1}'|tr '\n' ':'|sed 's/:$//g'" > /tmp/_log_out
VAR=`cat /tmp/_log_out`

output in /tmp/_log_out comes as expected if any file having that pattern

I want to achieve in one line which wants to include in the ssh cmd itself if the no pattern found then a variable should be set to 0.

like ex. ( directly storing the result in a variable and result is nothing variable should be 0)

ssh user@host "VAR=`grep -n \"$PATTERN\" $RDIR/$RFILE | awk -F":" '{print \$1}'|tr '\n' ':'|sed 's/:$//g'"`; if [ -z "$VAR" ];then VAR="0";fi"

reason for asking this....after getting the line number executing below line also to get others details (no. of chars in file's 1st line & file inode) which is also working...

my intention is achieve this 2ssh cmds in one line.

ssh $USER@$HOST "cat $RDIR/$RFILE | grep -v '^$' | head -1 |wc -c;ls -li $RDIR/$RFILE|awk '{print \$1}'" |xargs -n 2 | awk '
{ print $1":"$2":" }' > /tmp/_log-chk-output_$$

[just fyi] ===> objective: This script ssh to many servers and looks for pattern in few log files and store in a local flat file in below format
line # of pattern found:total no. of chars in file's 1st line:file inode number

Script is working one, only concern im ssh to servers twice (first time to get the line of pattern & 2nd to get the rest of dtls). Now looking to optimize in 1ssh cmd to achieve both. appreciate your help

Last edited by Scott; 05-10-2010 at 03:06 AM.. Reason: Please use code tags
# 2  
Old 05-10-2010
You can use something like this:

ssh -T > local_file hostname <<\!
perl -nle'BEGIN {
  $ino = (stat $ARGV[0])[1];
  $. == 1 and $fl = length;
  /pattern/ and $ln = $.;
  END {
    print join ":", $ln ? $ln : 0, $fl ? $fl : 0, 
    }'  remote_file

Or, of course, use Net::SSH:Perl if it's available.
# 3  
Old 05-11-2010
Using Net::SSH::Perl To Find A Pattern in a remote file and print info on matched file

Originally Posted by radoulov
Or, of course, use Net::SSH:Perl if it's available.
Here's How You Can Do That:
#! /usr/bin/perl

use strict;
use warnings;
use Net::SSH::Perl;

my $user = '';
my $pass = '';
my $host = '';
my $port = 22;
my $DEBUG = 0;
my $attr = { debug => $DEBUG, port => $port, protocol => '2,1'};

my $cmd = qq~perl -lne 'if ( /(PAT_HERE)/ ) { printf "PATTERN: %s LINE: %d FILE: %s INODE %s", \$1, \$., \$ARGV, (stat \$ARGV)[1]; }' *~;
my $ssh = Net::SSH::Perl->new($host, %$attr);
$ssh->login($user, $pass);
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print $stdout, "\n";

The above code expects you to replace the string "PAT_HERE" with the one you are looking for.

The code searches the files in the user-you-logged-in-as' ( $user ) home directory. If you want to search in other directories change the "cmd" line to something like this, where the the string " \/some\/dir\/* " is a directory you want to search:

my $cmd = qq~perl -lne 'if ( /(PAT_HERE)/ ) { printf "PATTERN: %s LINE: %d FILE: %s INODE %s", \$1, \$., \$ARGV, (stat \$ARGV)[1]; }' \/some\/dir\/*~;

More effort will be required if you want to handle situations in which no matches are returned gracefully.

Hope That Helps.

Last edited by deindorfer; 05-11-2010 at 04:13 AM.. Reason: Add Escaping Slashes
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. BSD

Connection SSH to remote by ssh

Hello guys! I am setting up a script to access a unix remote server. My problem is that when I put the ssh line "my host", the script does not wait for the server response asking for the password to execute the line in which I put the password, that is, I need to put a form in which script has a... (1 Reply)
Discussion started by: aroucasp
1 Replies

2. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

3. Shell Programming and Scripting

Need Multiple Return Values

Hi, I need to retrun multiple values function errorFileCreation { echo "Before" return -1 "Siva"; echo "Aftyer" } echo ${?} - This can be used to getting first value. how can i get second one. Advance Thanks... Shiv (3 Replies)
Discussion started by: rsivasan
3 Replies

4. Solaris

how to login with ssh to remote system with out applying the remote root/usr password

how to login with ssh to remote system with out applying the remote root/user password with rlogin we can ujse .rhosts file but with ssh howits possible plz guide (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

5. Shell Programming and Scripting

Can $? return multiple values?

Hi, I have a script which does something like the below: $arg1 $arg2 `exec-some-cmd` if then; do something else do something else fi However, during some cases, there is an error saying: line xxx: [: too many arguments at the line number which has... (5 Replies)
Discussion started by: laloo
5 Replies

6. UNIX for Dummies Questions & Answers

Command does not want to return on ssh

Hi, I am trying to use this command to backup my remote server: ssh ftp nice -19 bru -cXiAf mail-02:/dev/nst1 /etc When I run I get: connect to address port 544: Connection refused connect to address port 544: Connection refused trying normal rsh... (1 Reply)
Discussion started by: mojoman
1 Replies

7. Shell Programming and Scripting

can quoted text return values

Hi All, index=10.5 let "res = $index + 1.7" echo "res = $res" Can anybody explain what this piece of code does. (7 Replies)
Discussion started by: kinny
7 Replies

8. Shell Programming and Scripting

Help: return values from awk

Hi. I have a script like this: nawk 'BEGIN {FS=","; TOT1=0; REJ1=0;} { if($7=="TOTAL") { TOT1=TOT1 +$8} if($7=="REJS") { REJ1=REJ1 +$8} }' FILE_123.dat and... (1 Reply)
Discussion started by: mrodrig
1 Replies

9. Shell Programming and Scripting

Possible return values for $?

I think the $? returns 0 if the last issued command was successful and otherwise if not. But does anyone knows the value list that may be returned ? (or it is only zero/one ? ) Thanks in advance, Abrahao. (3 Replies)
Discussion started by: 435 Gavea
3 Replies

10. UNIX for Dummies Questions & Answers

exit/return values

Sys: HP-UX 9000 In the calling script how do I 'read' the return/exit value of a called script?:confused: THX in advance for any assistence.:) (1 Reply)
Discussion started by: vslewis
1 Replies
Login or Register to Ask a Question

Featured Tech Videos