remote execute awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remote execute awk script
# 1  
Old 09-02-2010
remote execute awk script

Hi gurus I need to execute awk command on remote machine and write output to stdout.

So far Ive tried:

Code:
ssh server "ifconfig | grep -A 1 "^eth" | awk '{if ($1~/^eth/) {temp=$1} else {print temp $0}}'"

Code:
ssh server "exec " < file_containing_above_command

But neither works
Without awk everything work, awk on local side also works. I thing problem will be somewhere with qoutes or escaping.
Thanks a lot
# 2  
Old 09-02-2010
Yes, you need to escape the special characters (the $ in your case).
Or you could change your command like this:

Code:
ssh <hostname> /sbin/ifconfig | 
  awk '/^eth/ { 
    n = $1; getline; print n, $0 
    }'

To execute it with the local awk (if available).

To get everything executed remotely:

Code:
ssh <hostname> "/sbin/ifconfig | 
  awk '/^eth/ { 
    n = \$1; getline; print n, \$0 
    }'"


Last edited by radoulov; 09-02-2010 at 11:46 AM..
# 3  
Old 09-13-2010
Nice works like a charm Smilie is also possible to write it to one line ?
# 4  
Old 09-13-2010
Yes, just remove the newline characters Smilie
# 5  
Old 09-13-2010
Smilie thanks, - I did not expect that $ can be problem - I expected ' {} or ()

Now I am facing another problems:
I use ssh in script to access various systems (solaris, hp-ux, linux...) if I want remote use ifconfig somewhere it works somewhere not.
- I found that that may be the PATH problem or something with sourcing and variables - I am not familiar with it and I am a little bit confused, but anyway it is recommend to use /sbin/ifconfig (as you did)
OK but what if somewhere is ifconfig in another path ? Is possible to use classic ifconfig in script?

- Second problem: Is possible to remote use bash script for loop on system which did not have bash ? Example is more than 1000 words:
Code:
#!/bin/bash
for hpux_server in `cat hpux_servers`
do
  output=`ssh $hpux_server "for lan in \$(/usr/sbin/lanscan | grep lan | awk '{print \$5}'); do /usr/sbin/ifconfig \$lan; done" | grep -v 127.0.0.1 | grep 'inet'`
  #       ssh hpux_server  "for lan in \$(/usr/sbin/lanscan | grep lan | awk '{print \$5}'); do /usr/sbin/ifconfig \$lan; done"
  echo -e "${hpux_server}:\n=============\n${output}\n=============\n=============\n" >> final_hpux_list
done

The commented line in above code works if it is not in bash script

- Third problem: Is good practice to set environment variables when I log on server via script (to avoid expecting input) ? Is it necessary for script to have set environment variables or is there any better solution ?
Code:
ssh server "DISPLAY=:0; TERM=xterm; /sbin/ifconfig -a" | grep -v 127.0.0.1 | grep 'inet addr'

Many many thanks
# 6  
Old 09-14-2010
Quote:
I use ssh in script to access various systems (solaris, hp-ux, linux...) if I want remote use ifconfig somewhere it works somewhere not.
- I found that that may be the PATH problem or something with sourcing and variables - I am not familiar with it and I am a little bit confused, but anyway it is recommend to use /sbin/ifconfig (as you did)
OK but what if somewhere is ifconfig in another path ? Is possible to use classic ifconfig in script?
No, as far as I know, ifconfig's position and syntax varies accross different operating systems,
so I would suggest to use a custom code for your environment.

Quote:
- Second problem: Is possible to remote use bash script for loop on system which did not have bash ? Example is more than 1000 words:

[...]

The commented line in above code works if it is not in bash script
Could you post the exact error message or explain what you mean by "not working"?

Quote:
- Third problem: Is good practice to set environment variables when I log on server via script (to avoid expecting input) ? Is it necessary for script to have set environment variables or is there any better solution ?

Code:

ssh server "DISPLAY=:0; TERM=xterm; /sbin/ifconfig -a" | grep -v 127.0.0.1 | grep 'inet addr'
I'm not sure about this one. Some ssh client implementations support the -t/-T switch to enable/disable pseudo-tty allocation,
I don't know if setting the above mentioned variables has some impact.
# 7  
Old 09-14-2010
Thank you for other replies Smilie
[QUOTE=radoulov;302453138]
Could you post the exact error message or explain what you mean by "not working"?
[QUOTE]
Code:
ssh server  "for lan in \$(/bin/netstat -in | awk 'if (!(NR==1||NR==2)) {print \$1}'); do /sbin/ifconfig \$lan; done"

bash: !: event not found

I think problem will be with ! (bang or what is the name in English) I also tried escape it but when i did it then error is in awk not in bash

Code:
awk: line 1: syntax error at or near if

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute shell script on remote machine

I want to execute a shell script(set of commands) on remote machine and that script takes input from text file(local machine). Please refer below: ssh user@hostname 'bash -s'< ./test.sh file.txt But i got the error file.txt doesn't exist. Can anyone help me on this. Content of test.sh: ... (2 Replies)
Discussion started by: manishtri88
2 Replies

2. Shell Programming and Scripting

Shell script help to execute ssh remote commands

Hi, The below command is not giving me the count , Can somebody help me in re-writing this pls . Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef | grep -v grep | grep "${ICM_Proc}" |wc -l ` Also the same problem with below... (13 Replies)
Discussion started by: Y.balakrishna
13 Replies

3. UNIX for Dummies Questions & Answers

Execute shell script in remote machine

Hi All, We have 2 servers A and B. B is having a sctipt called b.sh in path /home/dev/scripts. Now my requirement is i want to execute b.sh from server A. Kindly help me. (3 Replies)
Discussion started by: Girish19
3 Replies

4. Shell Programming and Scripting

Execute a local script against a remote server

I am unable to run the below script against a remote server due to syntax error (then unexpected), but i am able to run it locally. Am i executing it correctly or is there any other way to execute it. ssh username@servernname ksh -s < scriptname #!/bin/ksh function record { ((end =... (5 Replies)
Discussion started by: NarayanaPrakash
5 Replies

5. IP Networking

Execute script located on a remote machine

So, is there way of automating this ? My ultimate goal is to run some cmd script in windows and it should connect to a remote unix host and run a script x.sh located on the remote unix host. I was wanting to achieve this by using WinSCP and Putty only. If possible let me know how and if not... (25 Replies)
Discussion started by: mohtashims
25 Replies

6. Shell Programming and Scripting

Expect script to execute a script on a remote host

Hi, I am new to the expect scripting. I have this expect script as below : spawn ssh remote_server -l id set pass "12345" set opt "s" expect "Password:" {send "$pass\r" ; } expect "*ENTER*" {send "Enter\r"; exp_continue } expect "Please select option :" {send... (2 Replies)
Discussion started by: curt137
2 Replies

7. Windows & DOS: Issues & Discussions

Windows remote to Solaris to execute ksh script

Hi all, I'm not sure if it is correct to post here. I am facing problem wanting to create a batch that run from my Windows XP pc to remote to multiple Solaris server to execute the server's ksh script. :wall: Can anyone give me a hints on how to do that? Thanks. (6 Replies)
Discussion started by: beginningDBA
6 Replies

8. Shell Programming and Scripting

Using ssh to execute a remote script in the background

Help please!! I want to use ssh to execute a remote exe and while it's running I want to query for the process ID of the exe (2 different ssh commands) 1. sshpass -p "<passwd>" ssh -f -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@<ipaddress> nohup /tmp/mmds_asyn & 2.... (0 Replies)
Discussion started by: rvompoluTMW
0 Replies

9. Shell Programming and Scripting

execute remote script asynchronous

Does anyone know how to execute remote script asynchronously? Here is my command TargetList=$testmaker/config/prod_domain.list for targethost in `cat $TargetList`; do rsh $targethost -l bvuser "$HOME/var/script-root/afp/bin/run_nrtp_cache_flush.sh $appName" done (1 Reply)
Discussion started by: leemjesse
1 Replies

10. Shell Programming and Scripting

how to execute a script on remote machine

hi unix guru's i am new to unix shell programming. i found a trouble in executing a script(bali.ksh) which is available on serverA with username xyza, this script contains sqlplus command to retrive the data from the database available on other serverC. Now i need to run the above script... (4 Replies)
Discussion started by: balireddy_77
4 Replies
Login or Register to Ask a Question