Problem with executing script using ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with executing script using ssh
# 1  
Old 08-13-2008
Problem with executing script using ssh

I'm writing a script which is meant to ssh into a remote machine, sed a file there, and write the output to a new file on the remote machine. Obviously the easy way to do this is to write and execute the script on the remote machine, but I'm trying to do this all via ssh since I like to keep and execute my scripts from a central location.

So here's my extremely ugly attempt so far:

#Do the following on a remote server:
#ssh in, then operates on some files in the remotedir directory
#sed from an "mg" file (such as host1.mg), replacing all instances of
#host1 and replacing it with a new string from a after.conf
#and then writing the output to the remotedir directory.
#Assume that the after.conf only has one value right now (host2)

remotebox="remote1"
adminssh="sudo -u admin ssh $remotebox"
before="host1"
remotedir="/dir/remotedir"


for after in `cat after.conf`
do
echo $target
$adminssh sed -e 's/$source/$target/g' $remotedir/$source.mg > $target.mg

done

========================================
This works up to a point for me. But as it stands now, it bombs out with the following error:

sed: -e expression #1, char 0: no previous regular expression

I'm assuming this is because I am mis-using sed somehow. Even if I replace the sed command with something like this:

$adminssh "cat $remotedir/$source.mg > $remotedir/$target.mg"

the script bombs out - it seems like the redirect is trying to force the output onto my local machine I think. I'm sure I'm doing several things wrong here. Can anyone help?
# 2  
Old 08-14-2008
Nobody knows? Smilie

Is this the completely wrong way to go about doing this?
# 3  
Old 08-14-2008
variables in sed

Using variables in sed scripts is tricky, and it looks like you're caught on the first step.

Ever program in C? When this command runs:

sed -e 's/$source/$target/g' $remotedir/$source.mg

think about the values of argv.

What I imagine you WANT is for argv[2] to be something like:

s/\/old\/directory\/oldfile/\/new\/directory\/newfile/

What you HAVE is:

s/$source/$target/g

Because you're using single quotes, not double quotes, and sh isn't expanding those variables.

So the first step is to switch those single quotes to double quotes,

"s/$foo/$bar/g"

The next step is to make sure $foo and $bar don't contain any stray '/' characters that are going to confuse sed.

Best of luck!

The problem here is: 's/$source/$target/g'


---

for after in `cat after.conf`
do
echo $target
$adminssh sed -e 's/$source/$target/g' $remotedir/$source.mg > $target.mg

done
# 4  
Old 08-14-2008
sed and bash

Also note that when you use commands with ssh, your command gets launched instead of a login shell.

So redirects like > don't work: those are part of the shell.

IIRC, you can do something like,

ssh remotehost '/bin/bash -l -c '\''echo hi > $HOME/foo'\'''

What command gets run on the remote host? It's

/bin/bash -l -c 'echo hi > $HOME/foo'

which causes bash to log in and run:

echo hi > /home/dir/foo

(the quoting around HOME is awkward here)
# 5  
Old 08-14-2008
Extremely helpful - thank you so much. I'll give this a shot.

How does this board work anyway? Any way to thank people like blesses or points? Or do people help others out of the goodness of their own hearts? Smilie

This is my first time using this board.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remote script via SSH not executing

I have worked on multiple scenarios to execute remote script via ssh. This problem I am not able to resolve. 2 linux hosts. Server1, Server2 on Server1 I have script called ~/scripts/start_standalone.sh XXXX cd $JBOSS_HOME NODENAME=xyz; IP_ADDR=`hostname`; MGMT_IPADDR=`hostname`;... (3 Replies)
Discussion started by: oraclermanpt
3 Replies

2. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script test.sh which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

3. Shell Programming and Scripting

Executing a background script using ssh keys

Greetings, i've been working with a user-friendly menu on ksh to allow users execute scripts located on a remote server, so they wont have to login and manually launch those scripts every single time. This is a HP-UX box and currently on a /usr/bin/ksh shell. I've setup ssh keys on both... (1 Reply)
Discussion started by: nbriozzo
1 Replies

4. Shell Programming and Scripting

having problem while executing the script

Hi i am getting stuck while executing the script in the below line .Please help me out if ;then Name=`grep -i $Size $FILE|awk '{print $(NF-1),$NF}' |head -1` else Name="$Nam" fi (3 Replies)
Discussion started by: soumyamishra
3 Replies

5. Shell Programming and Scripting

executing command in a remote machine through ssh - shell script

Hi All, i have two machines like x and y . my requirement is i should connect to machine Y from x through ssh connection . and do some operation such as copy and move and delete files in Y machine . i tried with this code but it is doing in machine x only . and i need to exit from Y when... (1 Reply)
Discussion started by: rateeshkumar
1 Replies

6. UNIX for Dummies Questions & Answers

Error Executing a script(SSH problem)

Hi i m writing a script which will fetch data from different machines and display it on single terminal. I have created a file named SERVERNAMES containing ip address of machines i need to monitor. Then for every IP i m issuing ssh command to get date on that machine. However When i m trying... (3 Replies)
Discussion started by: pinga123
3 Replies

7. Shell Programming and Scripting

Problem executing a script

Hi Friends, When I try to execute one of my script script1.shl, I am getting the error message as "/tmp/sh1871978.13: cannot create" . I could not find the specified sh* file in the /tmp directory and I also checked the disk space of /tmp and it is only 60% filled. Please help me on... (5 Replies)
Discussion started by: mr_manii
5 Replies

8. Shell Programming and Scripting

Problem executing script through at

Hey fellows, i've got a probkem while executing a command through a scheduled at-job; Somehow my script starts looping, when triggered throug at; It doesnt do so if started manually. Any ideas? #!/bin/sh # atrun uid=33 gid=33 # mail www-data 0 umask 22 cd /var/django/webripper/ripper... (2 Replies)
Discussion started by: viktor4124
2 Replies

9. Shell Programming and Scripting

Executing a script on a remote system via SSH

Hello all, I have a relatively simple script I wrote to generate a count of errors broken down. What I would like to do is execute this script from another server so that I don't actually have to log in to the server to run the check. The script on what we'll call "Server A" is: ... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

Problem executing a script

The script startserver.sh has permissions to execute. Still the nohup command returns error with 'No such file or directory' Any sugggestions: $ nohup ./startserver.sh & Error $ nohup: appending output to `nohup.out' nohup: cannot run command `./startserver.sh': No such file or... (4 Replies)
Discussion started by: hemangjani
4 Replies
Login or Register to Ask a Question