Avatar of AIX25
AIX25
Flag for United States of America asked on

How can I determine missing excape characters from current string command from master to remote server via SSH?

I am running into issue to determine escape characters from master to remote server over ssh.  I can return desired output on remote server with no issues.  Trying to run similar string on master server to remote server to get same result.

Below example is working fine on remote server and has expected results:

[root@remote_server ~]# echo ${passwd_expire}
Jun 17, 2020
[root@remote_server ~]# echo ${current_date}
Jan 09, 2020
[root@remote_server ~]# echo $(( ($(date --date="$(echo ${passwd_expire})" +%s) - $(date --date="$(echo ${current_date})" +%s) )/(60*60*24) ))
159

Open in new window


I would like to run those remotely and get same output, but I keep running into bash errors and I can't determine which escape character I could be missing or if it's execution process order is incorrect.

server=remote_server


[root@master ~]# echo ${passwd_expire}
Jun 17, 2020
[root@master ~]# echo ${current_date}
Jan 09, 2020
[root@master ~]# ssh -q ${server} "echo $(( ($(date --date="$(echo ${passwd_expire})" +%s) - $(date --date="$(echo ${current_date})" +%s) )/(60*60*24) ))"

Open in new window


Need some advice on how I can run similar string directly on remote server on this master server as well via ssh.
* BashProgrammingShell ScriptingUnix OS

Avatar of undefined
Last Comment
skullnobrains

8/22/2022 - Mon
Kent Olsen

Hi AXI25,

If either {current_date} or {passwd_expire} is blank, you'll get an empty substitution and the date function won't work.  Are you sure that they're set on the remote server?

Run the command through "echo" to see what text will be executed.

 echo ${server} "echo $(( ($(date --date="$(echo ${passwd_expire})" +%s) - $(date --date="$(echo ${current_date})" +%s) )/(60*60*24) ))"

Try with the variables set and empty to see the difference.
AIX25

ASKER
Hello Kent,

In my original entry, I show the output for the variables are set and not zero/blank. Below you can see more detailed output.

[root@master ~]# echo ${passwd_expire}
Jun 17, 2020
[root@master ~]# echo ${current_date}
Jan 09, 2020

Open in new window


When running through echo, i know I can see it's missing an escape or subshell psossible. I'm getting the below output.

root@master]#  echo ${server} "echo $(( ($(date --date="$(echo ${passwd_expire})" +%s) - $(date --date="$(echo ${current_date})" +%s) )/(60*60*24) ))"
date: illegal option -- date=Jun 17, 2020
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
date: illegal option -- date=Jan 09, 2020
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
bash: ( -  )/(60*60*24) : syntax error: operand expected (error token is ")/(60*60*24) ")

Open in new window

Kent Olsen

The date command in AIX is much more limited that date on other systems.

I can't get it to execute on my local AIX system, much less a remote one.  I'm getting the same errors that you are.  The date function is the culprit.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Kent Olsen

The date command in AIX will set the date, or display the current date in any of many formats.

You cannot pass a date value to the date command and have it reformat it.  That appears to be what you're trying to do.
AIX25

ASKER
On this particular example I am currently testing this on Redhat Linux, but same errors as you're pointing out. I was able to run just the reformat portion also remotely, that wasn't an issue, when trying to narrow it down.  It appears when I try to calculate the delta and the arithmetic portion it's clobbering out. I can get the date format returned fine.  What I will try next is either running a script local to the remote server and then copy data back. Or, I'll try to collect the dates locally on the master server and do the arithmetic there.

It would be nice to have it done through one SSH string command, but I have other methods that seem to be working.
Kent Olsen

Ahhh....

Based on your user I assumed that this was an AIX issue.  :)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
skullnobrains

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
AIX25

ASKER
Thank you, both. As I didn't create my script as mentioned by skullnobrains, I was still running into same issue, it did make sense that it's running command locally.  What I ended up doing as example to work was the idea of it running it remotely as a whole script instead of it remotely through SSH. Collects the data on the remote server and just copies it back to master.  It's running over 500 plus servers and doing it as multi-threaded.

#!/bin/bash
# This is local script on master server

<code>

scp /var/tmp/remote_script.bsh remote_server:/var/tmp/

<code>

ssh remote_server "/var/tmp/remote_script.bsh"

<code>

scp -p remote_script:/var/tmp/remote_output.txt /var/tmp/

exit
skullnobrains

i recommend you use the last variant i gave you. It pipes the  command directly into the remote shell which is both more efficient and more secure.

Quoting the 'EOF' ensures nothing is interpreted locally and the script is xferred exactly as typed.

I used this method with complex scripts very extensively in the past years. The return code is also transmitted properlt by ssh.