bash scripting question
Hi All (maybe Volv can help me here seeing how he's a linuxer)
I'm using the Mac OS X Terminal from work, and I have to connect to our shared directory via ssh, I found a way to mount the thing using fuse (MacFUSE in my case). This is useful to me because I periodically have to move around large numbers of files to specific folders, and stuff like that so its a lot easier to click and drag rather than to type "mv <sourcepath> <destinationpath>" all over the place.
Now its possible to use the ssh mounting program (sshfs) from the commandline, so I decided that I'd automate the process via a program that I have that can run scripts whenever I plug my laptop in at work (MarcoPolo).
Now the problem is that using the sshfs program from the command-line requires me to type in the password for connecting to the ssh directory. However if I use MarcoPolo to run the script I never see the the Terminal, so I never see the password prompt, I cant enter any password, so the script fails.
Is there anything that I can add to the script that will enter the password automatically, or popup a window to prompt me for the password (although that would be less desirable). The sshfs program itself does not have an option to allow password entry before execution.
I'm not a Mac user yet (still debating the move), and I've never used sshfs, but I do use openssh on Linux. No guarantees... here goes....
First you need a program that will prompt for a password, e.g. x11-ssh-askpass. This appears to be available via Fink, although it seems it's not a final package so you would have to compile it yourself. Or find something else that fills the ssh-askpass role (for all I know you already have something--search for "ssh-askpass" on your computer).
Then in your environment (either your login settings/files or within the script itself), set the SSH_ASKPASS environment variable to that program. Whenever a password is required for ssh, that program will pop up a little window for you to type the password. From the ssh man page:
If ssh needs a passphrase, it will read the passphrase from the
current terminal if it was run from a terminal. If ssh does not
have a terminal associated with it but DISPLAY and SSH_ASKPASS
are set, it will execute the program specified by SSH_ASKPASS
and open an X11 window to read the passphrase. This is particu-
larly useful when calling ssh from a .xsession or related
script. (Note that on some machines it may be necessary to
redirect the input from /dev/null to make this work.)
hm I messed around with ssh_askpass for a while but I couldnt figure it out -__-, and there are pretty much no tutorials for Macs regarding the matter. oh well.
You can try ssh-agent and ssh-add. ssh-agent stays in memory and holds your credentials for whenever an ssh process needs it (no more password prompts!). ssh-add actually adds your credentials to ssh-agent. I don't believe this will work with plain passwords so you'll have to generate proper keys if you haven't already.
The trick is that you need to start ssh-agent sometime before any of those ssh processes so that they pick up the environment settings that tell them where to find ssh-agent. Interestingly, in the linux distro I use, the window manager startup script is configured to run ssh-agent out of the box. Then, also before you use ssh, you'll need to ssh-add (just once per login session). Once that's done, whenever you use ssh it won't bug you about passwords.
You might want to read that too... never fucked with SSH network shares. No idea how it's done. That page has some nice tips for automating and such though... might be able to apply it. Sorry, all I can find.
thanks to Silverwmoon!