Continuing shell script execution after SSHing into guest machine? - bash

I have an Ubuntu guest box setup on my Windows host using Vagrant and VirtualBox. I'm trying to write a shell script that will...
vagrant up
vagrant ssh once vagrant up is complete
cd into a specific project directory in the guest machine once successfully SSHed into the guest machine
Right now my vagrant_shell_script.sh file contains the following:
vagrant up && vagrant ssh && echo 'cd vagrant/rails_tutorial/sample_app'
Everything works fine when I execute it in Git Bash, up to and including connecting via SSH to the guest machine, however after it successfully connects, the script seems to stop working and does not execute the final cd command. I presume this is because it is no longer able to communicate directly with my host machine through that particular Bash instance (please correct me if I'm wrong).
Is there any way to have it navigate directly to the target directory once the SSH connection is successful?
Please forgive me if this is a dumb question--relatively new to bash scripting.

This solved it. It's kind of hacky, but running
vagrant up && vagrant ssh -- -t 'cd /vagrant/rails_tutorial/sample_app; /bin/bash' gets you in. For some reason vagrant keeps kicking you out if you don't launch the shell.
vagrant ssh -- allows you to pass commands into the SSH client. This is vagrant's own utility. The next flag, -t is an SSH flag and it allows SSH to execute certain commands before it hands control back to you. You put your command after the -t flag, but make sure to end it with <last command> ; /bin/bash so that it launches a shell for you and you don't get kicked out.

you can also use Heredoc to run the commands after you ssh by using something similar to this in your script:
# Use heredoc to send script over ssh
$ssh_cmd << 'END_DOC'
cd <path>
commands
exit
END_DOC
echo $ssh_cmd

Related

How to ssh into remote linux server using username / ip address and password from window using shell script?

What I am trying to do is to login to remote Linux server using SSH from my Windows machine by running a shell script via git bash.
I would like to write a script which will be used by an user with basic IT knowledge. This script will execute a bunch of commands on the remote machine, so it does need to establish a SSH connection.
What I have tried to write in this script so far is:
ssh username#ip <password>
EDIT: You should consider installing Jenkins on the remote system.
Running a command on a remote system can be done via:
ssh user#host command arg1 arg2
If you omit the password, a password prompt will appear, to get around the password prompt, you should consider setting passwordless SSH login. https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login
Rephrasing what you said, you want to write a script (namely script1.sh) which does the following (in this order):
Start a ssh connection with a remote
Execute some commands, possibly written in another script (namely script2.sh)
Close the connection / Keep it open for additional command line commands
If you want to put the remote commands on script2.sh instead of listing them in script1.sh, you need to consider that script2.sh must be on the remote server. If not, then you may consider to delegate to script1.sh the creation/copy of script2.sh on the remote machine. You may copy it in a temporary folder.
In this case, my suggestion is to write script1.sh as follows:
Copy of script2.sh with
scp /path/to/local/script2.sh user#host:/path/to/remote/script2.sh
Switch bash sheel to remote shell with
ssh user#host
Execution of script2.sh
sh /path/to/remote/script2.sh
Instead, if you prefer to list everything in just one script file, you may want to write:
Switch bash sheel to remote shell with
ssh user#host
Execution of commands
echo "This command has been executed on the remote server"
echo "This command has also been executed on the remote server"
..
Possibly closing the SSH connection to prevent that the user execute additional commands
You may consider to copy the ssh-keys on the remote server so to avoid password prompts. More information here.

How to SSH from local linux into specific directory on windows 10 remote

I want to ssh from my local linux computer into a specific directory on a windows 10 remote. The shell that is used on the remote is git bash. I don't want to keep changing the directory every time I log into my remote using ssh.
for linux remotes this is easily done using something like this:
ssh -t user#x.x.x.x "cd /targetDir ; \$SHELL --login"
The question is how can the same thing be achieved for Windows 10 remotes? If nothing else works I would also accept changing the default entry point in git bash for any ssh sessions on the remote.
Please note that I am not looking for help setting up ssh (already works). I just want to jump right into a specific directory when a session is started.
I was able to figure this thing out myself. The following command gets the job done. Using double and single quotes together is required to make it work (in no particular order).
ssh -t user#x.x.x.x "'cd /targetDir ; bash'"

Running bash command through ssh that only exists on remote

I am writing a bash script that involves ssh-ing into a remote host and running commands there. That in itself is not a problem. The issue is that I want to run a command which doesn't exist locally, only on the remote. The script fails with bash: line 1: type: remote_only_command: not found, even though it's successfully connecting to the remote host and can run basic commands without issue.
I can run the command on the remote host if I ssh in and run it manually. I've tried writing a separate bash script on the remote host and running that through the script (sh remote_script.sh), but that gets the same command not found error.
ssh $REMOTE var=$var 'bash -s' << 'EOF'
ls # works no problem, lists files on the remote server
remote_only_command # bash: line 1: type: remote_only_command: not found
EOF
Is it possible to run a command that is only accessible from the remote host and not locally where the script is being run?
I think this is the way it should work, as the command is only executed on the remote host. But i suspect your problem is the environment, which is NOT permitted over ssh. Try to use the complete path to the command, eg:
/path/to/remote_command

How to return to the script once sudo is done

I am on a server and running a script file that has following code.
ssh username#servername
sudo su - root
cd /abc/xyz
mkdir asdfg
I am able to ssh... but then the next command is not working.. the script is not sudo-ing. any idea?
Edit: Able to create a mech id and then do the things.. though still looking for the answer to above question :|
First of all your command will "stuck" on the first line because it will go into an interactive mode. The ssh command will require a password to be provided by a user (unless there is an sshkey being used) . And if the ssh is logged into the remote server then it will wait for user commands from standard input.
Secondly the lines following the ssh command will be executed only when the first process has exited. This is why your script is not "sudoing" - it's waiting for the ssh to end.
So if your point is to run a command on a remote server then put the command as a parameter into the same line as ssh connection. In your case:
ssh user#server sudo su - root
But this will not be of satisfaction for you. I suggest you create a script of what you want to execute on the remote server and then execute the script.
ssh user#server scriptName
The sudo thing here is very tricky because again your script might get stuck in the interactive mode waiting for a password to be inserted so I suggest you think again on the basis of the script.
mb47!
You want to run the script on the remote computer, correct?
On the remote machine, create a file containing the commands you would like to execute.
Then, on the other machine, run ssh user#machine /path/to/script/you/created/earlier
I hope this helps!
ALinuxLover

SSH : Remotely run a script and stay there

I wish to run a script on the remote system and then wish to stay there.
Running following script:-
ssh user#remote logs.sh
This do run the script but after that I am back to my host system. i need to stay on remote one. I tried with..
ssh user#remote logs.sh;bash -l
somehow it solves the problem but still not working exactly as a fresh login as the command:-
ssh user#remote
Or it will be better if i could include something in my script that would open the bash terminal in the same directory where the script was running. Please suggest.
Try this:
ssh -t user#remote 'logs.sh; bash -l'
The quotes are needed to pass both commands to ssh. The -t option forces a pseudo-tty allocation.
Discussion
Consider:
ssh user#remote logs.sh;bash -l
When the shell parses this line, it splits it into two commands. The first is:
ssh user#remote logs.sh
This runs logs.sh on the remote machine. The second command is:
bash -l
This opens a login shell on the local machine.
The quotes were added above to prevent the shell from splitting up the commands this way.

Resources