LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2015, 11:11 AM   #1
galron010
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Rep: Reputation: Disabled
BASH: passing variables through sed via ssh remote command




Hello BASH gurus, I am having issues with inserting variables into a sed command, and then passing that command through SSH. Here is my scenario:

I have a file on multiple different servers. I am trying to create a script on my management server that will log in to each of the remote servers, and edit the file with server-specific information (i.e. IP-address information).

Here is the line I am using in my script

Quote:
ssh $server "sed -i 's/'$nicIP'=.*/'$nicIP'='$newIP'.100/g' file.txt"
In the above example:
  • $server=root@<server name>
  • $nicIP=The configuration option in the file to set IP
  • $newIP=The first 3 octets of the new IP scheme

When the script is executed, and it gets to that portion, the script hangs. I am not sure what to do here. I have tried several variations (removing extra single quotes to leave only 1st and last single quotes, using "s/<old>/<new>/g", etc.), all to no avail.

Any and all possible combinations that might solve this issue will be appreciated.

Thanks.


Last edited by galron010; 09-10-2015 at 11:12 AM.
 
Old 09-10-2015, 11:17 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try to debug:
Code:
REMOTECMD="sed -i 's/'$nicIP'=.*/'$nicIP'='$newIP'.100/g' file.txt"
printf "Now I will try this: %s\n" "$REMOTECMD"
ssh "$server" "$REMOTECMD"
Edit: of course the problem might be in the connecting, so test that too:

Code:
ssh "$server" 'cat /etc/motd'

Last edited by NevemTeve; 09-10-2015 at 11:23 AM.
 
Old 09-10-2015, 11:31 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
As the shell will interpret the variables in the double quotes first I do not think you need all the single quotes:
Code:
ssh $server "sed -i 's/$nicIP=.*/$nicIP=$newIP.100/g' file.txt"
 
Old 09-10-2015, 01:25 PM   #4
galron010
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled


NevemTeve:

Your suggestion for debugging was helpful. It now returns the following:

Quote:
Now I will try this: sed -i 's/nicOption=.*/nicOption=A.B.C.100/g' file.txt
Now I will try this: sed -i 's/nicOption=.*/nicOption=A.B.C.100/g' file.txt
Now I will try this: sed -i 's/nicOption=.*/nicOption=A.B.C.100/g' file.txt
<repeated until ^C is sent>
As you can see, I modified my command slightly, but it now attempts to re-run the command infinitely. When I log into the test server, I see the desired line is changed (and only the desired line is changed, which is of course the desired effect).

It is worth noting that the <ssh $server "sed....."> is in a loop in the script. But that loop is set to run this command, then increment to the next server/next IP in the appropriate arrays, then move past the loop once the script has reached the end of the array. That being the case, I don't think that would be the cause of this loop.

Attempted solutions:

I have tried changing the "/g'" to a "/1g'", and I have tried adding a ";exit" after "file.txt" (a shot in the dark on both. I honestly didn't think either would work)



grail:

Your suggestion was also correct, thank you. Removing the extra single-quotes is, at the very least, extremely helpful in trouble-shooting, as it keeps the code clean.

Anyone have suggestions as to where to go from here?

 
Old 09-10-2015, 11:52 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Add command set -xv into your script to see what it is doing
 
Old 09-11-2015, 11:16 AM   #6
galron010
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
That helped me to discover there was actually a logic error in my loop. Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing variables which contain special characters to sed Tech109 Programming 7 06-22-2012 11:08 AM
Bash Script - Passing command string between variables Devcon Programming 13 01-10-2011 10:13 AM
ssh : remote command execution doesn't work for modifying env variables pypieuvre Programming 10 02-24-2006 09:50 AM
passing variables to sed jjfate Programming 8 07-31-2003 04:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration