0

I used the following command to initiate a ssh connection to a remote macOS .Objective is to perform successful ssh login and mount a particular network share (via the mount.sh script).The script mount.sh when stored and executed on the remote macOS works fine.The script intakes user inputs and mounts the same into a user defined mount point.But am stuck whenever I would try to locally execute the script-mount.sh on the remote system via ssh.

In fact the ssh login works well but the script-mount.sh doesn't gets executed.

 #!/bin/bash

read -p "Enter  Username: " username
    ssh $username@<Server IP> "$(< mount.sh)"

Any suggestions/advice to improve the code are welcome :).

The mount.sh script code :

    #!/bin/bash
read -p "Enter the name of share mount :" lpmount
echo ""
read -p "Enter your  User name: " username
echo ""
read -p "Enter the year (e.g. 2018,2019) :" year
mkdir /Gross/$lpmount
mount_smbfs //$username@<Server-IP>/Evi/$year /Gross/$lpmount
mount_status=$? # Checks if the mounting was successfull.
if [ $mount_status -eq 0 ]
then
        echo ""
        echo "Share mounted SUCCESSFULLY! at /Gross/$lpmount"
        sleep 3
else
        echo ""
        echo " Share not mounted SUCCESSFULLY! at/Gross/$lpmount"

fi
  • What is mount.sh doing (can you share that script)? – Kusalananda Mar 22 at 12:17
  • can you try to run script with -x option as that will show where it is failing !! – Vivek Kanadiya Mar 22 at 12:27
  • My point in getting to know what mount.sh is doing is: mount.sh could probably do the ssh itself as something simple, like ssh user@server mount .... – Kusalananda Mar 22 at 12:34
  • @Kusalananda, Agreed. that if mount.sh is simple command then it should be passed as stdin but just in case it's doing more than just command !! – Vivek Kanadiya Mar 22 at 12:47
  • You say "the ssh login works well but the script-mount.sh doesn't gets executed". What actually happens when you run the ssh command? Do you get any messages at all? What do the messages say? – Kenster Mar 22 at 15:59
0

Explanation:

You can add bash -s onption which will process the script. Please see below Modification:

Modification:

#!/bin/bash

read -p "Enter  Username: " username
    ssh $username@<Server IP> 'bash -s' < mount.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.