LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with ssh options in a script (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-ssh-options-in-a-script-4175514236/)

rraina 08-11-2014 10:12 AM

Need help with ssh options in a script
 
Hello, I am trying to implement the below in a script but the escape characters are not working for the regular expressions:

Code:

ssh -n oracle@lnx123 "cd /fmw/admin/scripts/instances/extapp_wt_inst[1-2]/config/OHS/extapp_ohs[1-2]/abcdocs ; touch outage.html " 2>/dev/null
done

I need to run this on sets of two servers, with the below paths -
/fmw/admin/scripts/instances/extapp_wt_inst1/config/OHS/extapp_ohs1/abcdocs
/fmw/admin/scripts/instances/extapp_wt_inst2/config/OHS/extapp_ohs2/abcdocs


I have tried the below using the escape characters but still no luck:

Code:

ssh -n oracle@lnx164 "cd /fmw/admin/scripts/instances/\extapp_wt_inst\\[1-2\\]\/config/OHS/extapp_ohs\\[1-2\\]/abcdocs "
Please let me know how I can correct this?

jpollard 08-11-2014 10:42 AM

It depends on WHERE you want the [1-2] to be interpreted...and WHAT you want to accomplish.

The regular expressions need to be evaluated on the remote system. But you are using them in a "cd" command which can only take one target - the expression [1-2] may be 0 or more.

It is an error if both directories exist on a target.

What are the errors you get from what you tried?

Guttorm 08-11-2014 10:42 AM

Hi

It's complicated. The ssh command gets the parameters without quotes, and so does the shell on the other end. One way to do it, is to mix single and double quotes:

Code:

ssh -n oracle@lnx123 "cd '/fmw/admin/scripts/instances/extapp_wt_inst[1-2]/config/OHS/extapp_ohs[1-2]/abcdocs' ; touch outage.html " 2>/dev/null
Other way is to add quotes inside the parameter with a \ prefix:

Code:

ssh -n oracle@lnx123 "cd \"/fmw/admin/scripts/instances/extapp_wt_inst[1-2]/config/OHS/extapp_ohs[1-2]/abcdocs\" ; touch outage.html " 2>/dev/null
Edit:
Or maybe I misunderstood. You want to cd to any of those 2 if they exist? Then your code should work without any escaping. Does this work?

Code:

ssh -n oracle@lnx123 "touch /tmp/f1 ; touch /tmp/f2 ; ls /tmp/file[1-2]"

rraina 08-11-2014 10:51 AM

Hi jpollard, it will never be possible that both the directories exist on the same target..I want the script to login to those servers at the specified location and create an html file " outage.html "

When I try, it does not evaluates the regular expression and gives the error:

/fmw/admin/scripts/instances/extapp_wt_inst[1-2]/config/OHS/extapp_ohs[1-2]/abcdocs not found

rraina 08-11-2014 11:00 AM

Hi Guttorm,

I tried the first 2 options you suggested, but both give the same error:

bash: line 0: cd: /fmw/admin/scripts/instances/extapp_wt_inst[1-2]/config/OHS/extapp_ohs[1-2]/abcdocs: No such file or directory

I have to make this work on 2 sets of servers which have different directory names but the same filename has to be created on both.

I don't know if there is any other way to implement this ?

Thanks for ur help!

Guttorm 08-11-2014 11:06 AM

Well, as jpollard said above, the script is not very solid. You can get everything from 0 to 4 directories. It doesn't work without any escaping? And it does work if you ssh first and then run the command? Please post both.

jpollard 08-11-2014 11:08 AM

Quote:

Originally Posted by rraina (Post 5218864)
Hi jpollard, it will never be possible that both the directories exist on the same target..I want the script to login to those servers at the specified location and create an html file " outage.html "

When I try, it does not evaluates the regular expression and gives the error:

/fmw/admin/scripts/instances/extapp_wt_inst[1-2]/config/OHS/extapp_ohs[1-2]/abcdocs not found

That looks like some combination of config1 extapp_ohs1, 1 2, 2 1, 2 2, do not exist.

I believe your first example should have worked - but only when the path actually exists on the host.

You might verify that /fmw/admin/scripts/instances/extapp_wt_inst1/onfig/OHS/extapp_ohs1/abcdocs (or some combination of the numbers) actually exists.

rraina 08-11-2014 12:05 PM

Hi guys!

Indeed I was trying to go to wrong path ... I feel so silly now !!!

Thanks all for your prompt help.

I got it working without any escape characters.

ssh -n oracle@lnx164 "cd /fmw/admin/instances/APP_WT/extapp_wt_inst[1-2]/config/OHS/extapp_ohs[1-2]/abcdocs ;touch outage.html"

Thanks again!


All times are GMT -5. The time now is 04:23 AM.