Using vim to remotely edit a file on serverB only accessible from serverA - vim

Although I have never tried this, it is apparently possible to remotely edit a file in vim as described here. In my particular case the server I need access to can only be accessed from on campus, hence I have to log into my university account like so:
ssh user#login.university.com
then from there log into the secure server like so:
ssh user#secure.university.com
I have keyless ssh set up, so I can automate the process like so:
ssh user#login.university.com -t "ssh user#secure.university.com"
is there anyway to remotely edit a file such as secure.university.com/user/foo.txt on my local machine?
EDIT:
My intention is to use vim on my local machine as it is impractical (move .vim folder, copy .vimrc) and in some cases impossible (recompile vim with certain settings, patch vim source, install language beautifiers) to make vim on the remote machine behave the way I want it to behave. What I want is to issue something like this (this is not accurate scp, I know)
vim scp://user#login.university.com scp://user#secure.university.com//home/user/foo.txt

OK after a little working around I figured it out. First you have to edit (or create) your .ssh/config file as described here. For our purposes, we will add a line like this, which essentially adds a proxy.
Host secure
User Julius
HostName secure.university.com
ProxyCommand ssh Tiberius#login.university.com nc %h %p 2> /dev/null
Then we can simply copy (via scp) the file secure.university.com:/home/Julius/fee/fie/fo/fum.txt to the local computer like so
scp secure:/home/Julius/fee/fie/fo/fum.txt fum.txt
Extending on this, we can load it into vim remotely like so:
vim scp://secure//home/Julius/fee/fie/fo/fum.txt
or using badd like so:
:badd scp://secure//home/Julius/fee/fie/fo/fum.txt
To simplify my life, I added this shortcut to my .vimrc file for the most commonly used subfolder:
nnoremap <leader>scp :badd scp://secure//home/Julius/fee/fie/fo/fum.txt
So far vim has proven to be pretty aware that this is a remote file, so if the C file includes a file like so:
#include "foo.h"
it won't complain that "foo.h" is missing

Once you SSHed in the machine you can run any command(also vim) in remote host on your shell. After logging run vim as you are running in your machine.

Since you are using ssh, you basically have access to the server via the CLI, as if you were sitting in front of the machine itself. With that said, you can use any program on that machine, just like you would use it on your own machine. Assuming that the secure.university.com/user/foo.txt means that there is a text file called foo.txt at location /user on the secure server, then the following commands would work after logging in through ssh:
cd /user
vim foo.txt
You could also use nano or any other CLI based editor that is installed on the machine.

Related

Running commands on remote server transparently

I am developing a number of applications that need a bit more power than my local machine has. So I'd like to run them on a remote machine. This is all fairly straightforward and runs something like: 1) rsync the files in the current directory to the remote machine in some location, 2) ssh to remote machine run the command. In some cases, if the remote command generates a file, I'd need to pull that back locally also.
It feels to me like such a common set of tasks that should be a nice command that puts it all together. Say something like
## Run make on the the files in the current directory on big-server-box
rrun big-server-box make
## Do the same, but pull output.txt back afterward
rrun big-server-box -f output.txt make
## Open a shell, having synced files first
rrun big-server-box --shell
Is there any tool that achieves this?
There is already a mechanism for accomplishing what you’re looking for: ssh.
If you want your specific syntax, you could very easily write a wrapper script. The only things different from your syntax above is the -f flag to copy files back to your local machine (which would be easy to implement with scp) and the --shell flag, which can be omitted since that behavior is implied with ssh. Other than that, the syntax is identical: call ssh instead of rrun and you have what you’re looking for.

What happens when I execute vim remotely over ssh? [duplicate]

I have a remote file that I edit regularly. I would like to edit it with a quick, simple command that would work likely via SSH. At present, my workflow is to connect to the remote computer via SSH, open the file using an editor (say vim or nano), edit, save and then close the connection.
I am aware that I can mount the remote computer filesystem using SSHFS or Nautilus capabilities, but I'm really looking for a single command to run in the terminal which shall open the file in an editor, allow me to save and then exit, closing all connections to the remote computer.
Currently, I am trying to do this by passing a command to the remote computer via SSH, but I am running into difficulties. For VIM, the command is something like the following:
ssh user1#computer1 "vim /path/laboratory_notebook_1.md"
Using this procedure, VIM does not run correctly and presents the following error:
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
For nano, the command is something like the following:
ssh user1#computer1 "nano /path/laboratory_notebook_1.md"
Using this procedure, nano does not run and the following error is presented:
Error opening terminal: unknown.
I'm not sure how to proceed on this line of thought. I would appreciate assistance on this method and suggestions on other ways to edit remote files briskly with a minimum amount of interaction.
Force Pseudo-TTY Allocation
You can force pseudo-tty allocation with one or more -t flags. The SSH(1) man page says:
-t Force pseudo-tty allocation. This can be used to execute arbi-
trary screen-based programs on a remote machine, which can be
very useful, e.g. when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty.
Example
Using your own example, slightly modified, the following would work if you have a local TTY or PTY:
ssh -t user1#computer1 'vim /path/laboratory_notebook_1.md'
It works fine for me with OpenSSH_6.2p2. Your mileage (and operating environment) may vary.
If you are using vim. Vim comes with a plugin called netrw which will allow you to do this.
vim scp://hostname/path/to/file
Will copy the file to you local machine and on save reupload it.
Take a look at netrw's documentation :h netrw

copy file from one server to another in linux

how to run commands like ftp or sftp or scp in background? Also how to setup password less connection for running these command?
Look for manual pages for scp or rsync, which both can do this job well, if not being forced you don't want to use sftp or even the non encrypted ftp file transfer!
something like the following, for example:
rsync [some other parameters] -e ssh SOURCE TARGET
Assuming these commands are coming from a bash script , you would need to make sure that the two (or more ) systems have ssh certificates generated that allow you to access said systems without providing a "password" per se.
Briefly, you could do it by running this command on one system:
ssh-keygen
following through, this will generate a key. Then run:
ssh-copy-id user#some-remote-system
to copy it to the remote system, which will allow passwordless access, enabling scripts to go about their business without stalling for password prompts.

What is a quick way to edit a remote file on Linux?

I have a remote file that I edit regularly. I would like to edit it with a quick, simple command that would work likely via SSH. At present, my workflow is to connect to the remote computer via SSH, open the file using an editor (say vim or nano), edit, save and then close the connection.
I am aware that I can mount the remote computer filesystem using SSHFS or Nautilus capabilities, but I'm really looking for a single command to run in the terminal which shall open the file in an editor, allow me to save and then exit, closing all connections to the remote computer.
Currently, I am trying to do this by passing a command to the remote computer via SSH, but I am running into difficulties. For VIM, the command is something like the following:
ssh user1#computer1 "vim /path/laboratory_notebook_1.md"
Using this procedure, VIM does not run correctly and presents the following error:
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
For nano, the command is something like the following:
ssh user1#computer1 "nano /path/laboratory_notebook_1.md"
Using this procedure, nano does not run and the following error is presented:
Error opening terminal: unknown.
I'm not sure how to proceed on this line of thought. I would appreciate assistance on this method and suggestions on other ways to edit remote files briskly with a minimum amount of interaction.
Force Pseudo-TTY Allocation
You can force pseudo-tty allocation with one or more -t flags. The SSH(1) man page says:
-t Force pseudo-tty allocation. This can be used to execute arbi-
trary screen-based programs on a remote machine, which can be
very useful, e.g. when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty.
Example
Using your own example, slightly modified, the following would work if you have a local TTY or PTY:
ssh -t user1#computer1 'vim /path/laboratory_notebook_1.md'
It works fine for me with OpenSSH_6.2p2. Your mileage (and operating environment) may vary.
If you are using vim. Vim comes with a plugin called netrw which will allow you to do this.
vim scp://hostname/path/to/file
Will copy the file to you local machine and on save reupload it.
Take a look at netrw's documentation :h netrw

Saving grep results to a file on a remote box

I'm somewhat new in Linux. I have to read logs on a remote host and save certain lines, found with grep command to a file. The problem is that I don't have permissions to create a file on the host. Is there a workaround the issue? Thanks!
You can run something like the following:
ssh remotehost "grep certainline logs*" > file
to save the file locally.
Otherwise, you might be able to create a file in /tmp.
You don't mention but I'm going to assume you're using ssh to access the remote machine. So you can run the command on the remote machine and redirect the output on the local machine like so:
ssh remotehost 'grep pattern /var/log/mylog' > mylocalfile
Note that the redirection occurs outside the quoted command that is given to ssh to send to the remote host. If you were to put it inside the quotes then the redirection would occur on the remote side.

Resources