I have 2 servers , lets say local A and remote B, I need to use both as a part of a pipeline. The folder structure is shared between the two, so I can access the same files and folders from both A and B.
When I try to ssh into B from A, it always goes to my home directory in B , I want to go into a specific folder instead and want to pass the path of the folder as a variable to ssh.
What are my options? Please assist
This is what I tried
[/serverA/]$ workdir1="/My/Intended/Path/"
sshpass -p password ssh userid@serverB.xxx.org << ENDSSH
workdir1=\$workdir1
cd \$workdir1
echo $(pwd)
## Other operations on server B
ENDSSH
The echo statement shows the path of my home directory in B , instead of my intended path. So the variable $workdir1 is not being passes by ssh and the cd is not working.
I think the problem lies in the escaping of the $ sign when expanding variables in your here document. Just as a rule of thumb: escape it to use a "remote" variable, don't escape it for using local vars. Having local and remote variables with identical names doesn't help debugging nor understanding what's going on...
Yes, but for me, I don't want to go to a different directory every time I login because I tend to work on the same project for a day or two
If I changed directories every time I logged in, I would just ssh in and cd manually.
It's less key stokes and less time to do it that way. If you add a different directory to ssh every time you ssh in, then that is more work (for me at least) than just ssh'ing as normal and cd'ing to where I want to go.
I want to cd as well, but the path is not being passed from local to remote. I cant manually cd , this is part of a pipeline, so the cd has to be part of a variable based shell-script.
Any variable for that matter, is there a way to pass variables?
--- Post updated at 10:59 AM ---
Sorry about the confusion, I tried every combination without any luck,
You have a server onto which you want to log on using a certain user. If you connect from host A you want the user, after logging in, to go to one certain directory, if you connect from host B you want to go to another directory and so on. Is that right?
If so: there are several options you have. First you could use the output of who -a to find out from where you have connected. A sample output will look similar to:
bakunin@host $ who -a
system boot 2019-03-20 15:29
run-level 3 2019-03-20 15:29
LOGIN tty1 2019-03-20 15:29 7581 id=tty1
bakunin + pts/0 2019-03-21 16:01 . 8762 (10.1.1.1)
Using this information you can create a script like this to change to the respective directory:
icomefrom=$(who -a | sed -n '/bakunin/ {;s/.*(//;s/)$//p;}')
case "$icomefrom" in
10.1.1.1)
cd /dir/A
;;
10.1.1.2)
cd /dir/B
;;
.....
esac
Notice that you may also have a variable "SSH_CONNECTION" set which holds essentially the same information.
Another possibility would be to use different users to connect to the host so you can have different login scripts which put you into different directories. All these users might at the end switch their effective UID (via su ) so that you end up with always the same user but in different directories.
There will be a myriad of other possibilites, but as long as you do not explain what you (in the grand scheme of things) want to achieve it is hard to find out which one of them is feasible and which one isn't. So tell us about your plans and we will be better able to help you.
When I set up pipeline processes using ssh, I always use ssh without passwords by just exchanging public and private key pairs... (a one time setup, but saves a lot of time and effort afterwards)
In fact, even when I login for normal work, I mostly use password-less ssh with crypto keys. It is faster, more secure, and easier to set up pipelines of ssh commands.
Not quite. The echoed value shown in red above is also from the local shell; not the shell running on serverB.xxx.org. And, in Bourne based shells, the current working directory is stored in the variable named PWD , not the variable named pwd (and they are distinct variables because the shell is case-sensitive).
You probably want to replace that line in your script with either:
echo \$PWD
or:
pwd
and, if you're having any doubts about which variables are being expanded where, I would suggest using the 2nd form.
Good point, Don.
I thought executing echo $(pwd) (as it's not a variable, but rather an active function) through ssh on a remote system, would return the output of command pwd from the remote, rather than local... echo $(pwd) is superfluous anyway - should be either plain pwd or as you indicated echo \${PWD} .
Hi vgersh99,
Ouch. Yes, you're right that was a command substitution; not a variable expansion. (I do know that I need new glasses and I have an appointment with my ophthalmologist Tuesday morning.)
But, just like a variable expansion, if the <dollar-sign> isn't escaped in a command substitution, it will be performed by the local shell; not by the remote shell.
I guess seeing echo $(pwd) I interpreted it as echo ${pwd} instead because pwd would be so much easier and would be interpreted by the remote shell instead of by the local shell (true for variable expansion and for command substitution). I apologize for any confusion my comments may have raised.