SSH from Local to A, A to B and run multiple commands on B - shell

Im currently using the line of script below to ssh from my local machine to a server (lets call it ip-address1) then from that machine i want to ssh to another machine (lets call this machine ip-address2). The script i use is as follows:
sshpass -p mypassword ssh -tt user#ip-address1 ssh -tt -i /root/.ssh/vm_private_key user#ip-address2 "pwd; ls;"
The problem is only the first command (pwd) executes on ip-address2 then it closes and the ls command executes on ip-address1 before it then closes. I want both commands to execute on ip-address2. The output in my terminal is something like the following:
/home/user (pwd command executing here)
Connection to ip-address2 closed.
//files then get outputted here (ls command executes after ip-address2 has
closed)
Connection to ip-address1 closed.
I think there may be something wrong with my quotation but i cant figure out what. Please help.
Thanks.

I don't have any way to test this, but try the following:
sshpass -p mypassword ssh -tt user#ip-address1 \
"ssh -tt -i /root/.ssh/vm_private_key user#ip-address2 'pwd; ls;'"
You definitely need to quote the entire command you want to run on the ip_address1, including the command you'll pass to ip_address2.
Edit
I'm in an environment where I have multiple machines to test; the following command works for me:
ssh snewell#<host sanitized> \
"ssh <host2 sanitized> 'hostname; ls -a <path sanitized>;'"
hostname definitely displays the result of the final server (host2), and ls is listing a directory that the first host doesn't have.

Related

In Linux, How do you automatically run commands with custom parameters you feed it on SSH login?

This question:
https://superuser.com/questions/355029/linux-how-to-automatically-run-commands-on-ssh-login#355030
Only tells me how to run static commands. What it does not tell me is how I can feed it parameters. So say for example, I would like something like this to happen:
$ ssh boat#programming.com -p 2222 --parameter1 "boat programming"
Last login: Sat Dec 10 03:59:37 2016 from some place
boat programming
# .bashrc executes something like this:
echo $parameter1
If I have to use a language like expect, fine, but ideally I would like to keep this straightforward and simple.
Save this short script (on your remote system) with name run in a directory which is part of your $PATH variable (on your remote system) and make it executable.
#!/bin/bash
echo "$2"
Use it this way:
ssh boat#programming.com -p 2222 run --parameter1 "'boat programming'"
Output:
boat programming
The way, in theory, to do this is to put parameter1 in your environment, and let the remote process inherit it from ssh.
$ parameter1="boat programming" ssh boat#programming.com -p 2222
In practice, this probably will not work, because it requires the remote sshd agent to be running with the PermitUserEnvironment option enable, which is not true by default.
The only way to force this would be to run bash explicitly on the remote host with the appropriate environment.
$ ssh boat#programming.com -p 2222 'parameter1="boat programming" bash -i'
Here is my working solution:
0) If you want to automate password entry (Completely optional):
sudo apt install sshpass
1) SSH into your server, and create a file called run
#!/bin/bash
echo $parameter1
2) Make it executable
chmod +x run
3) Handle the parameters like this (Concatenate these lines.):
Avoid manual input of the password. (Be Careful!)
sshpass -p "$YOURPASSWORD"
Standard SSH login
ssh -o StrictHostKeyChecking=no boat#programming.com -p 2222
Parameters must go first. Use single quotes within double quotes otherwise day will be returned as an unknown command error:
parameter1="'happy day'"
Give an absolute path to the program you wrote.
/path/to/run
Alltogether:
sshpass -p "$YOURPASSWORD" ssh -o StrictHostKeyChecking=no boat#programming.com -p 2222 parameter1="'happy day'" /path/to/run

Bash Script : Execute unix commands inside a remote server

I am trying to login to Server B from Server A and perform simple UNIX commands on Server B using a shell script. The code is as follows. But ls -al is displaying the result of Server A and not the one that is logged on to i.e Server B. Any inputs are highly appreciated. Thanks
#!/bin/bash
clear
sshpass -p password ssh hostname
ls -al
exit
When the shell interprets a script file, it creates a child process to
execute each command line. So, the command lines after sshpass -p password ssh hostname are not actually executed inside the ssh
session to hostname, but in the host where the bash instance is
running.
To achieve what you want, you can check ssh(1) usage line and note that there is a [command] argument, that says:
If command is specified, it is executed on the remote host instead
of a login shell.
So, one way to do it is sshpass -p password ssh hostname ls -la. Another way which can provide some more flexibility is:
#!/bin/bash
clear
cat | sshpass -p password ssh hostname <<EOF
ls -la
EOF
Which would make ssh start a login shell in the remote host and pass
to its stdin the lines provided in the Here Document. The remote
shell would then interpret those strings as commands and execute them.
If you just want to run ls -al on the remote server, put it on the same line as the ssh command like
sshpass -p password ssh hostname ls -al
it will automatically exit when it gets to the end of the command so you don't need to put exit
Also, if you're going to be doing this and don't want to interactively enter the password, you might want to look at sharing public/private keys and using that so it won't ever ask for a password (unless you password protect your private key)

command execution on remote server shell

I am running a simple command on remote server using ssh on shell.
I am currently logged in myHost01 and i'm running this command on myHost02
The expected result is this is a test myHost02. HOWEVER, I get this is a test myHost01
This is my command
sshpass -p root ssh -q root#127.0.0.2 "echo this is a test `hostname`"
I don't know why it is taking the hostname of the server I'm running the command from!!!
It is also noticeable that when I run this
sshpass -p root ssh -q root#127.0.0.2 "hostname"
I get myHost02 (which is the correct output)
Pass the command in single quotes, like below. The double quotes cause the shell to execute command substitution hostname (locally) in-place and pass the resultant string this is a test myHost01 to ssh
sshpass -p root ssh -q root#127.0.0.2 'echo this is a test `hostname`'

Remote sudo, execute a command and write output in local terminal

What I try to do, I connect to a remote server as a normal user with sudo right, then sudo to root, and execute a command & see output in my local terminal. I wrote a small script like this:
#!/bin/bash
my_argument=$1
ssh -t username#hostname 'sudo su -; /path_to_my_script $1'
I type the password twice (one for ssh, the other for sudo), but I see nothing in my local terminal, and script looks terminated in remote host. I believe second problem could be resolved by using exit, but I am a little bit confused how I can get this output to my local terminal.
Thanks
String inside '' is taken literally. So, you are passing the dollar sign and 1 as a parameter to the script. If you want the string to be interpreted, place it inside "", like:
ssh -t username#hostname "sudo /path_to_my_script $1"

bash script to ssh into a box and get me to a python shell

I want to write a script that will get me straight to a python shell on another box so that i don't have to first run ssh and second run python.
When I do "ssh hostname python" it just hangs - it's something to do with the fact that python is interactive. "ssh hostname cat x" works fine.
Is there some ssh option that will make this work?
ssh -t user#host python
The -t flag forces ssh to allocate a pseudo-terminal to the connection. Normally it won't do this if a command is given on the ssh command line, which results in python running in a non-interactive mode.
actually figured it out, i needed to do ssh -t hostname python
You need the -t option to force the allocation of a pseudo-tty
ssh -t host python

Resources