SSH and AWK Scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SSH and AWK Scripting
# 1  
Old 07-14-2011
Question SSH and AWK Scripting

I am trying to put an awk command in ssh, for example:
Code:
ERRCOUNT=`ssh -n $HOST "ps -ef | grep .job | grep -v grep | grep -v alert_jobs_still_running |wc -l"`

From korn shell prompt this works:
Code:
awk '/^Jul 12 16/ {print $0}' /u01/app/oracle/jobs/adhoc/test.dat | wc -l

My data file: /u01/app/oracle/jobs/adhoc/test.dat:
Code:
Jul 12 16:13:59 spu76 fctl: [ID 517869 scsi.warning] WARNING: fp(1)::1e0a00 NS failure pkt state=dreason=9, expln=1, NSCMD=0112, NSRSP=8001
Jul 14 11:13:59 spu76 fctl: [ID 517869 scsi.warning] WARNING: fp(1)::GPN_ID for D_ID=1e0200 failed
Jul 12 17:43:59 spu76 fctl: [ID 517869 kern.warning] WARNING: fp(1)::N_x Port with D_ID=1e0200, PWWN=202200a0b8482ab8 disappeared from fabric
Jul 12 14:13:59 spu76 fctl: [ID 517869 kern.warning] WARNING: fp(0)::140a00 NS failure pkt state=dreason=9, expln=1, NSCMD=0112, NSRSP=8001

In a script test2.bat:
Code:
#!/bin/ksh
AWKCOUNT=0
HOST=spu76
 
echo "Plain SSH"
ssh -n $HOST "awk '/^Jul 12 16/ {print $0}' /u01/app/oracle/jobs/adhoc/test.dat"
 
echo "AWKCOUNT"
AWKCOUNT=`ssh -n $HOST "awk '/^Jul 12 16/ {print $0}' /u01/app/oracle/jobs/adhoc/test.dat | wc -l"`

I get errors:
Code:
(oracle@spu76) /u01/app/oracle/jobs/adhoc > ./test2.bat
Plain SSH
awk: syntax error near line 1
awk: illegal statement near line 1
AWKCOUNT
awk: syntax error near line 1
awk: illegal statement near line 1
(oracle@spu76) /u01/app/oracle/jobs/adhoc >

Ultimately, I want to run this awk statement using ssh:

test4.bat:
Code:
#!/bin/ksh
MONTH=`date | awk '{print $2}'`
DAY=`date '+%d`
HOUR=`date +"%H"`
echo "MON: $MONTH DAY: $DAY HOUR: $HOUR"
AWKCOUNT=0
 
AWKCOUNT=`awk -F: '/^'"$MONTH $DAY $HOUR"'/ {print $0} ' /u01/app/oracle/jobs/adhoc/test.dat | awk -F" " '/scsi/ {print $0}' | wc -l`
echo "AWKCOUNT is $AWKCOUNT"

Output:
Code:
(oracle@spu76) /u01/app/oracle/jobs/adhoc > ./test4.bat
MON: Jul DAY: 14 HOUR: 11
AWKCOUNT is 1

I have tried many variation of single and double quotes, and escaping characters. Any assistance would be appreciated!

Diane

Last edited by Franklin52; 07-14-2011 at 06:02 PM.. Reason: Please use code tags
# 2  
Old 07-14-2011
Thank you! I was able to get your example one to work!
# 3  
Old 07-14-2011
For something that complicated in backticks, I'd forget the quoting and put it in a separate file:
Code:
RESULT=`ssh $HOST exec /bin/sh -s PARAM1 PARAM2 < local-file`

where local-file would be an ordinary script file with no special quoting.

If you need to pass variables into the script, PARAM1 will be available as $1 in the remote script, PARAM2 as $2, etc, etc.

You could also use a here-document like so:

Code:
STUFF=`ssh $HOST exec /bin/sh -s PARAM1 PARAM2 <<"EOF"
echo "PARAM1 is $1 PARAM2 is $2"
EOF
`

You should be able to put anything you want between <<"EOF" and EOF unmolested as long as none of the lines inbetween start with EOF.

That final EOF line should be on the beginning of the line, no whitespace in front.

---------- Post updated at 01:07 PM ---------- Previous update was at 12:29 PM ----------

Also, for test4.bat, instead of running 3 separate date instances and an awk instance, why not just do date +"MON: %m DAY: %d HOUR: %H" to print what you want from the beginning?

If you're doing lots and lots of separate ssh instances, it may be smart to do everything in just one, which will save lots of time otherwise wasted connecting, disconnecting, and exchanging keys...
# 4  
Old 07-14-2011
I couldn't figure out how to get Jul instead of 07 using date formats. Also, where would I embed the date +"MON: %m DAY: %d HOUR: %H"?
I tried:
Code:
AWKCOUNT=`ssh spu76 exec /bin/ksh -s Jul 14 15 < /u01/app/oracle/jobs/adhoc/test8.bat`

.. which worked...but I couldn't get the following to work:
Code:
AWKCOUNT=`ssh spu76 exec /bin/ksh -s `date +"%m %d %H"` < /u01/app/oracle/jobs/adhoc/test8.bat`

test8.bat:
Code:
awk -F: '/^'"$1 $2 $3"'/ {print $0} ' /u01/app/oracle/jobs/adhoc/test.dat | awk -F" " '/scsi/ {print $0}' | wc -l

Thanks!

Last edited by Franklin52; 07-15-2011 at 04:30 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh scripting SSH to Compare File Sizes

Hello, I currently have very little experience with Shell scripting and trying to create a script for the purpose of collecting the size of a couple sizes on 4 different Hosts. The Idea is to collected the information from the files in which the script is kicked off on, store the values into... (17 Replies)
Discussion started by: Abstract3000
17 Replies

2. Shell Programming and Scripting

Help needed on ssh in bash scripting

HI I have the following requirement I have a script a.sh which will deploy files in multiple servers .The argument for the a.sh is abc.gz host1.conf where abc.gz is a zip file and one.conf will contain all the database connection string . Now I have to write a b.sh which will... (7 Replies)
Discussion started by: harry00514
7 Replies

3. Shell Programming and Scripting

SSH to remote hosts in shell scripting

Hi There, I have a file contaning some 100 servers names one by one the file called redhat_servers.txt I want to prepare a script where it should give me the host name and kernal version. I wrote like this, #!/bin/bash while read line do ssh $line "uname -nr" done <... (3 Replies)
Discussion started by: kumar85shiv
3 Replies

4. Shell Programming and Scripting

Need some help in bash scripting with ssh

Hi @ all I have the following scenario: As Admin of a cupple of servers I tried to write the following script to figure out, if the machine is up and available and if some directory´s were available. But my script is having some probs, while running. Maybe some of you have a better way to... (9 Replies)
Discussion started by: muogli
9 Replies

5. UNIX for Dummies Questions & Answers

ksh scripting, skip server if asks for password with SSH

Hi, I am running a script that connets to a list of servers with SSH and runs a command but I have some servers that are asking for password (authorized keys is not configured properly). Is there any way to do so that if I get a prompt for password just skip that entry? my script: ... (1 Reply)
Discussion started by: galuzan
1 Replies

6. Shell Programming and Scripting

Scripting ssh question

I am new here so I apologize if this question is in the wrong section or outside of the realm of this board. Also, this is just my first week into shell programming so I am probably doing lots of things wrong. I am trying to write a script to ssh to a machine as one user and then run a command... (0 Replies)
Discussion started by: Parva
0 Replies

7. Linux

ssh and passwd scripting execution problems on linux

I'm having a problem here and I was wondering if anyone could help me? I'm putting together a password script. First off, I don't have root access. I have sudo access. Lets say the User ID is Trevor1, the password is H!rry23! and the server name is Linux1234 This is how the script begins ... (5 Replies)
Discussion started by: wdog17
5 Replies

8. Shell Programming and Scripting

ssh scripting

Hi, I'm trying to write a script that will ssh into a number of boxes and run 'top' and 'ps', then output the results to a file with the hostname. The script only seems to run top or ps on the local machine though. Any help would be apperciated #ssh into box while read box do ssh -n $box ... (3 Replies)
Discussion started by: Brimak86
3 Replies

9. Shell Programming and Scripting

need help scripting tar over ssh

Hello - I've used 'expect' in FTP scripts before without any problems, but am unsure as to how I would script a tar over ssh session? I need to send password for authentication to the remote Suse machine. :confused: Any assistance provided would be GREATLY appreciated. (6 Replies)
Discussion started by: rm -r *
6 Replies

10. Shell Programming and Scripting

scripting an ssh session?

I know the root login/password for a machines, and I want to automate some commands like this from each: ssh root@remoteHost1 "tail /var/log/messages" ssh root@remoteHost2 "tail /var/log/messages" ssh root@remoteHost3 "tail /var/log/messages" ssh root@remoteHost4 "tail /var/log/messages" ssh... (2 Replies)
Discussion started by: jjinno
2 Replies
Login or Register to Ask a Question