Vagrant: How can you run scripts on the host via commands in the guest shell? - bash

It's possible to open ports, network files, and there are plug-ins that allow for running guest or host [shell] commands during Vagrant's Provisioning process.
What I'd like to do is be able to (perhaps through a bash alias) run a command in the Vagrant guest/VM, and have this execute a command on the host, ideally with a variable being passed on the command line.
Example: In my host I run the Atom editor (same applies to TextMate, whatever). If I want to work on a shared file in the VM, I have to manually open that file from over in the host, either by opening it directly in the editor, or running the 'atom filename' shell command.
I want parity, so while inside the VM, I can run 'atom filename', and this will pass the filename to the 'atom $1' script outside of the VM, in the host, and open it in my host editor (Atom).
Note: We use Salt for Vagrant Provisioning, and NFS for mounting, for what it's worth. And of course, ssh with key.
Bonus question: Making this work with .gitconfig as its merge conflict editor (should just work, if the former is possible, right?).

This is a very interesting use case that I haven't heard before. There isn't a native method of handling this in Vagrant, but this functionality was added to Packer in the form of a local shell provisioner. You could open a GitHub issue on the Vagrant project and propose the same feature. Double check the current list of issues, though, because it's possible someone has beaten you to it.
In the meantime, though, you do have a workaround if you're determined to do this...
Create an ssh key pair on your host.
Use Salt to add the private key in /home/vagrant/.ssh on the box.
Use a shell provisioner to run remote ssh commands on the host from the guest.
These commands would take the form of...
ssh username#192.168.0.1 "ls -l ~"
In my experience, the 192.168.0.1 IP always points back to the host, but your mileage may vary. I'm not a networking expert by any means.
I hope this works for you and I think a local shell provisioner for Vagrant would be a reasonable feature.

Related

SCP file from ssh session to localhost

I have a headless file server on which I store and manage downloads and media, but occasionally I have to transfer small files back to my computer (Mac, using bash shell). The problem is that some files have more user-friendly names and commonly have spaces in them, and they are buried in the file directory hierarchy I have set up on my server.
When I'm using scp from my local machine, I don't have tab completion, so I have to manually type out the entire path and name with spaces escaped. When I ssh into the server first, the command:
scp /home/me/files/file\ name\ with\ spaces.png Me#localhost:/Users/Me/MyDirectory
fails with the error "Permission denied, please try again" even though I'm entering my local machine user password properly.
I've learned a little bit of sftp since I've been told that may be a better tool for file transfer. However, the utility seems outdated and I still don't have tab completion after establishing a connection to the server (on my Terminal when pressing Tab I just get a tab character).
My question is this: what can I do to allow tab completion while using scp from my Mac? Or am I using incorrect syntax for scp while in an ssh session, and is there something in that command I should fix? Or, is there a (better? newer?) tool other than sftp that would offer tab completion on a server?
Finally, if none of these problems have simple solutions, is there some package I could install (e.g. a completion package from Homebrew or the like) that would facilitate better tab-completion with any of these commands?
Looks to me like this is some incorrect scping.
This is the format of the command
scp ./localFile.txt remoteUser#remoteHost:/remoteFile.txt
You were so close, but you have localhost set where you should have your remoteHost.
localhost is the name that resolves to the machine that you are currently on - so in your workflow, you are sshing to a machine, and then trying to scp that file to the same machine you are already sshd into.
What you need to do, is figure out the IP address, or the physical host name of the computer that you are trying to connect to, and use that instead.
scp ./localFile.txt remoteUser#192.168.1.100:/remoteFile.txt
# where 192.168.1.100 would be the IP of your Mac
I am assuming the reason you were getting permission denied, was because you were using your the login credentials for you mac, but unknowingly trying to login again to your headless machine.

Injecting bash prompt to remote host via ssh

I have a fancy prompt working well on my local machine. However, I'm logging to multiple machines, on different accounts via ssh. I would love to have my prompt synchronized everywhere by ssh command itself.
Any idea how to get that? In many cases I'm accessing machines using root account and I can't change permanently any settings there. I want the prompt synchronized.
In principle this is just setting variable PS1.
Try this :
ssh -l root host -t "bash --rcfile /path/to/special/bashrc"
maybe /path/to/special/bashrc can be /tmp/myrc by example

How to include a sub-script in a remote shell from remote location?

I am running a local bootstrap.sh script from OSX on a remote Ubuntu server which does some "if else then" stuff to load a specific subscript.sh when a specific condition is met.
I am running that local script with:
ssh user#host "bash -s" <~/projects/projectname/bootstrap.sh
I am having issues with getting the subscript.sh sourced (loaded/included).
You can't. You're only sending the contents of bootstrap.sh to the remote shell. It's attempting to source subscript.sh on the remote machine, and it isn't there.
You'll need to either copy subscript.sh (or both scripts!) to the remote machine, or insert the contents of subscript.sh into bootstrap.sh in place of the source command.
What I would recommend is to rsync your 'bootstrap.sh' from your local computer to your server. You should be able to do this with your ssh credentials.
A very cool utility is Transmit. It is $25 and allows you to cleanly mount your server as if it were a portable hard drive (Transmit can also do synchronizations). All you need is ssh credentials and is very user friendly.
If you are allowed to install on your server, then I would install qsub on it. (Actually just check to see if it is installed.) Then just mount your computer's drive and you can submit scrips with qsub (I actually would just make a small server on your mac). This is what I use for using a linux cluster from my OSX computer.
Alternatively you can make a small server from your osx and have it mounted on your linux server.

how to call a script in the remote machine without using ssh

I have to run a shell script which is on a remote machine from the script on my local machine.
I thought of using ssh in my script but it would prompt for password which does not help as I will need to make this script run as a cronjob.
Could anyone just guide me like what would be ideal to use rather than using ssh to connect to the remote machine and run the script present there.
First of all you can set up passwordless ssh connections using private keys (that's what I should do).
If that's not an option, well there is expect, and here's an old but working tutorial.

Opening a remote file with TextWrangler

My current solution for editing files on a remote web server is to use Fetch to browse the remote machine and TextWrangler to make the edits. But since I'm getting more comfortable navigating the command line on the remote machine (but not comfortable enough to use VIM...), I'd like to be able to type something like 'open filename.txt' on the remote machine and have TextWrangler open up on my local machine. I've heard the term "reverse tunneling" tossed around as an option, but I have no idea what to do next. Any suggestions are greatly appreciated - thanks!
Personally, I use Cyberduck as my S/FTP browser. In Cyberduck's preferences, you can define a default text editor to edit remote files. Now I can just hit Cmd+K when I have a file selected, and it will open up in TextWrangler. Whenever I save, the changes are automatically transferred to the remote file.
I was actually looking to do the same thing, and no one had written it up, so I figured this out today.
There's 2 required and 3 optional parts to this:
Enable ssh login on both computers (required)
Set up an ssh tunnel from the remote machine to your machine (required)
Set up an alias for the ssh tunnel (optional)
Set up an alias for TextWrangler on the remote machine (optional)
Set up ssh keys so you don't have to enter your password every time (optional)
You need to be able to ssh from local to remote to run the commands, and you need to be able to ssh from remote to local so it can send commands to TextWrangler.
To set up the ssh tunnel, you need to run a command on your local machine like:
ssh -f -N -R 10022:localhost:22 [username on remote machine]#[remote machine hostname]
The -f and -N flags put ssh into the background and leave you on your machine. The -R flag binds a port on the remote computer to a port on your local computer. Anything contacting the remote machine on port 10022 will be sent to port 22 on your local computer. The remote port can be anything you want, but you should choose a port > 1024 to avoid conflicts and so you don't have to be root. I chose 10022 because it's similar to ssh's default port of 22. Replace the brackets with your username and machine name.
You'll need to run that once after you log in. To make the command easier on yourself, you can add an alias in your bash profile. Add the following to your local ~/.bash_profile:
alias open-tunnel='ssh -f -N -R 10022:localhost:22 [username on remote machine]#[remote machine hostname]'
Of course, you can choose whatever alias name you like.
Once you've set up the tunnel, you can use a command like this on the remote machine:
ssh -p 10022 [username on local machine]#localhost "edit sftp://[username on remote machine]#[remote machine hostname]//absolute/path/to/file.txt"
The -p flag says to use port 10022 (or whichever port you chose earlier). This will cause the remote machine to connect to your local machine and execute the command in the double quotes without opening an interactive ssh session. The command in the quotes is the command you would run on your local machine to open the remote file in TextWrangler.
To make the command easier on yourself, you can add a function in your bash profile. Add the following to your remote ~/.bash_profile:
function edit { if [[ ${1:0:1} = "/" ]]; then abs_path="$1"; else abs_path="`pwd`/$1"; fi; ssh -p 10022 [username on local machine]#localhost "edit sftp://[username on remote machine]#[remote machine hostname]/$abs_path"; }
This is assuming that you don't have the TextWrangler command line tools installed on the remote machine. If you do, you should name the function something other than edit. For example, tw. Here, ${1:0:1} looks at the first character of the first parameter of the function, which should be the file path. If it doesn't begin with /, we figure out the absolute path by adding the current working directory (pwd) to the beginning. Now, if you're on the remote machine in /home/jdoe/some/directory/ and you run edit some/other/directory/file.txt, the following will be executed on your local machine:
edit sftp://[username on remote machine]#[remote machine hostname]//home/jdoe/some/directory/some/other/directory/file.txt
Lastly, you should set up ssh keys in both directions so you're not prompted for a password every single time. Here's a guide someone else wrote: http://pkeck.myweb.uga.edu/ssh/
I dont think this will allow opening from the command-line, but
Eclipse with Remote-System-Explorer also supports editing of files via ssh connection
I think what you're referring to is called "X11 forwarding" over ssh. Take a look at the ssh_config(5) manpage for configuration (or just use 'ssh' with the '-X' parameter). As far as i know, this does only work with X11 programs (gvim, xemacs, etc.), because the editor is actually running on the host you're connecting to - only the display stuff happens on your local machine. So TextWrangler is not an option, because it's not an X11 program.
I use Interarchy (from nolobe) for remote editing. It's a fairly advanced ftp/sftp client that gives you a finder-style view of your remote files and allows you to use your favourite editor to work on those files.
If you don't like to pay for such a program, there's an Open-Source program called "Fugu" available from the Univerity of Michigan which you can also use.
FileZilla offers this functionality as well. You can download it here. Once you've connected to your sftp you can right-click on the text file and open it with the text editor of your choice.
Minimal answer
You can use Applescript. So from the command line execute this:
osascript <<EOF
tell application "TextWrangler"
activate
open location {"sftp://myusername:#my.server:22222//home/username/.bashrc"}
end tell
EOF
Notes
Obviously you wouldn't want to type a here document on every invocation, so my suggestion would be to put this logic inside a regular shell script:
osascript <<EOF
tell application "TextWrangler"
activate
open location {"$1"}
end tell
EOF
Then invoke the script like this:
sh ~/bin/textwrangler.sh "sftp://myusername:#my.server:22222//home/username/.bashrc"
Specifying a host-qualified path can get tedious each time so either hardcode that in your script, or bind the script invocation to a keystroke via your shell. For bash:
bind '"\et":"sh ~/bin/textwrangler.sh \"sftp://myusername:#my.server:22222/\""'
Now you generate the majority of the command by pressing Alt-t

Resources