Executing local script/command on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing local script/command on remote server
# 1  
Old 03-28-2012
Executing local script/command on remote server

I have a command that I want to run on machine B from machine A. If I run the command on machine B locally, it works fine.

Here is the command:

Code:
 for n in `find /data1/ -name 'ini*.ext'` ; do  echo cp $n "`dirname $n `/` basename $n 
    .ext`"; done

From machine A, I issue this command

Code:
ssh user@machineB  for n in `find /data1/ -name 'ini*jsem'` ; do  echo cp $n "`dirname $n `/` basename $n .jsem`"; done

But I get error
Code:
`syntax error near unexpected token do`

What is wrong? I think it has something to do with double quotes, single quotes, semi colon because executing command
Code:
 `ssh user@machineB ls`

works fine. So not issue of authentication or something else.

Thanks
# 2  
Old 03-28-2012
script_to_run:
Code:
for n in $(find /data1 -name 'ini*.ext'); do  
     echo cp $n "${n}.ext"
done

Invoking the script:
Code:
ssh user@machineB "bash -s" < script_to_run

These 3 Users Gave Thanks to complex.invoke For This Post:
# 3  
Old 03-28-2012
Thank you, that worked perfectly
# 4  
Old 03-28-2012
What you have here is the shell on machine A interpreting the command line rather than just passing it over to machine B. It seems the semi colon as part of the command to machine A.

Try adding single quotes around the entire command you wish to pass, so probably before the for and after the done although you will have to then consider if you need to escape the single quotes you have in the string you are passing. You might end up with:-
Code:
ssh user@machineB 'for n in `find /data1/ -name \'ini*jsem\'` ; do  echo cp $n "`dirname $n `/` basename $n .jsem`"; done'

Have a play with simpler code to work out what gets passed and how on your systems.

I'm not too sure if ; done is valid either. Does this exact command work when run directly on machine B? If so, then I'm obviously mistaken. So many coding styles.......


I hope that this helps,


Robin
Liverpool/Blackburn, UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Except script to run a local shell script on remote server using root access

local script: cat > first.sh cd /tmp echo $PWD echo `whoami` cd /tmp/123 tar -cvf 789.tar 456 sleep 10 except script: cat > first #!/usr/bin/expect set ip 10.5.15.20 set user "xyz123" set password "123456" set script first.sh spawn sh -c "ssh $user@$ip bash < $script" (1 Reply)
Discussion started by: Aditya Avanth
1 Replies

2. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

3. Solaris

Script to get files from remote server to local server through sftp without prompting for password

Hi, I am trying to automate the process of fetching files from remote server to local server through sftp. I have the username and password for the remote solaris server. But I need to give password manually everytime i run the script. Can anyone help me in automating the script such that it... (3 Replies)
Discussion started by: ssk250
3 Replies

4. UNIX for Dummies Questions & Answers

Executing a script in remote server

Hi All, I have 2 servers A and B. I need to connect to server B from server A and execute a shell script in B which will create some files and i need to copy those files back to server A. Required easiest possible for perfoming above task. (1 Reply)
Discussion started by: Girish19
1 Replies

5. 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

6. Shell Programming and Scripting

Condition local or remote server command

Hello, Can you help me ? $7 fits to remote server. I can launch the script from local or remote server. I would like my_script.sh to choose local or remote command depending the variable $7. Is the function f1 right or wrong ? In this moment, i can't test it. Thanks in advance. I create... (3 Replies)
Discussion started by: amazigh42
3 Replies

7. Shell Programming and Scripting

rsh help - getting the output of remote script to local server.

Hi, I have a script that runs for an hour. Have to run it on remote server and need the output it produces on the remote server to decide for failure or success. I run it through a Autosys Job which logs the outputs, both 1 & 2. I use the commands 1) rsh <SERVER> 'nohup /tmp/xyz.ksh &' 2)... (5 Replies)
Discussion started by: aster007
5 Replies

8. Shell Programming and Scripting

Can a script runned in local server access remote server?

Hi, Im creating a script that is supposed to run commands on remote server using sftp. My script is as below: #!/bin/ksh sftp remote_server mypassword cd /u08/mydir/allfiles mget * .. But this is what I got when I runned the script: Connecting to remote server...... (3 Replies)
Discussion started by: luna_soleil
3 Replies

9. UNIX for Dummies Questions & Answers

Executing ls command in remote server

Hi Guru, I have a requirement where i need to list the *.csv files in my remote server and copy a file from that server to my unix server I wrote dis code #!/bin/sh . /home/aaa/bb/GlobalHost.sh export HOST export USER export PWD ftp -n $HOST <<END_SCRIPT quote USER $USER quote... (1 Reply)
Discussion started by: pssandeep
1 Replies

10. Shell Programming and Scripting

executing a remote location script from local server

hi i am having two servers one is local and remote(FTP)server.from local server i have to connect to remote server and execute a shell script i want to run a shell script(remote location) from my local server i am having some knowledge on ftp but i am not getting the result .please give ... (2 Replies)
Discussion started by: srivsn
2 Replies
Login or Register to Ask a Question