Run local command in ssh - linux

I want to run local commands with remote files in ssh, such as:
ssh server
# on server
!<local command> and remote files for executing
# still on the server
some other commands
Both remote and local are Linux systems. I guess this should be feasible, which is equivalent to passing a remote file to a local command for execution without exiting the ssh session (for example, using a local compiler to compile a remote file and generate a compiled file to the remote path where the ssh session is located). But I'm not sure if this is possible with the current ssh.

Related

SSH access parent host folder

After connecting to a remote server (A) through ssh is it possible to access host's folder/files?
This server A has access to another server (B) which I can't access from my computer. I need to run some commands on B using some config files on my computer.
I ended up copying the files from my computer to A using scp and run the commands there.

How to use ssh to run a local .sh file (present on a local linux machine) on a remote linux machine (an AWS ec2 instance)

I am having a tough time figuring out a way to execute a .sh file present on my local linux machine on to a remote linux machine which happens to be an AWS ec2 instance.
Here's what I am doing on my local machine:
ssh -i sample.pem ec2-user#server_name.amazon.com 'bash -s' < file_to_remotely_execute.sh
Error that I get is:
Warning: Identity file sample.pem not accessible: No such file or directory.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Host key verification failed.
This was referred to arrive at the above approach.
Possible issues :
The PEM file doesn't exist.
The PEM file doesn't have proper permissions.
The host is not allowed in ~/.ssh/known_hosts file.
Solutions :
Navigate to the location where PEM file exists or give full path of PEM file.
Give 400 permissions to the PEM file, (sudo chmod 400 /path/to/file.pem).
Login to the server by ssh once, and allow the host.

the usage of scp and ssh

I'm newbie to Linux and trying to set up a passphrase-less ssh. I'm following the instructions in this link: http://wiki.hands.com/howto/passphraseless-ssh/.
In the above link, it said:"One often sees people using passphrase-less ssh keys for things like cron jobs that do things like this:"
scp /etc/bind/named.conf* otherdns:/etc/bind/
ssh otherdns /usr/sbin/rndc reload
which is dangerous because the key that's being used here is being offered root write access, when it need not be.
I'm kind of confused by the above commands.
I understand the usage of scp. But for ssh, what does it mean "ssh otherdns /usr/sbin/rndc reload"?
"the key that's being used here is being offered root write access."
Can anyone also help explain this sentence more detail? Based on my understanding, the key is the public key generated by one server and copied
to otherdns. What does it mean "being offered root write access"?
it means to run a command on a remote server.
the syntax is
ssh <remote> <cmd>
so in your case
ssh otherdns /usr/sbin/rndc reload
is basically 4 parts:
ssh: run the ssh executable
otherdns: is the remote server; it's lacking a user information, so the default user (the same as currently logged in; or the one configured in ~/.ssh/config for this remote machine)
/usr/sbin/rndc is a programm on the remote server to be run
reload is an argument to the program to be run on the remote machine
so in plain words, your command means:
run the program /usr/sbin/rndc with the argument reload on the remote machine otherdns

Using SCP command to download files from Linux server to client server

I'm creating files on a Linux server that I'm logged into and I'm adding the ability for the user to download these files from the Linux server on to the connecting computer. I'm writing a scrip and using the scp command to download these files:
scp data.txt user#usraddress:/home/usr
However, I don't want to specify "user#usraddress:/home/usr" to be just my computer. I want whoever is logged onto the linux server to be able do download these files. Is there a way to get the address of the connecting computer?
How would I do this?
Forgive me if this seems elementary, I'm very new to scripting.
When you open a remote session in a GNU/Linux machine, the ssh server sets the environment variable SSH_CONNECTION with some connection information. You can use this variable and the $USER variable to fill that parameters:
scp data.txt $USER#${SSH_CONNECTION%% *}:/home/$USER
Note that as far as I know you couldn't assume the client home directory is at /home. As said by chepner, you could omit the destination directory to use the default location, the home directory.
scp data.txt $USER#${SSH_CONNECTION%% *}:

Execute command on remote server via ssh

I am attempting to execute a command on a remote linux server via an ssh command on a local server like this:
ssh myremoteserver 'type ttisql'
where ttisql is an executable on the path of my remote machine.
The result of running this is:
bash: line 0: type: ttisql: not found
When I simply connect first and do:
ssh myremoteserver
and then enter the command:
[myuser#myremoteserver~]$: type ttisql
I get back the path of the ttisql exe as I would expect.
The odd thing is that when I execute the first command in my beta environment it works as expected and returns the path of the exe. In the beta scenario, machine A is connecting to remote machine B but both machines are onsite and the ssh command connects to the remote machine quickly.
The problem is encountered in our production environment when machine A is local and machine B is offsite and the ssh command takes a second or two to connect.
The only difference I can see is the time it takes the production ssh to connect. The path on the remote system is correct since the command works if entered after the initial connection.
Can anyone help me understand why this simple command would work in one environment and not the other? Could the problem be related to the time it takes to connect via ssh?
Your PATH is setup differently when your shell is interactive (= when you are logged in on the server), and when not interactive (running commands with ssh).
Look into the rc files used by your shell, for example .bashrc, .bash_profile, .profile (depends on your system). If you set PATH at the right place, then ttisql can work when you run it via ssh.
Another solution is to use the absolute path of ttisql, then it will not depend on your PATH setup.
The environment can be different in a non-interactive session (ssh command) from an interactive session (ssh, then command). Try echo $PATH in both cases.
ssh myremoteserver 'echo $PATH'
vs
ssh myremoteserver
[myuser#myremoteserver~]$: echo $PATH
If they differ, look in all startup script for some differentiated behavior based on $PS1 or $-

Resources