1

How do I write a bash script that

first: login in to Ubuntu virtual machine

second: open terminal in VM and pass command in it?

Is there any way to do so?

Thanks

  • 3
    Use ssh just like connecting to any other computer. – glenn jackman Oct 9 '15 at 0:38
  • 1
    Set up port forwarding in virtual machine options, then ssh to it as glenn suggests – Sergiy Kolodyazhnyy Oct 9 '15 at 0:53
  • 1
    Openssh server will need to be installed on VM by the way – Sergiy Kolodyazhnyy Oct 9 '15 at 0:54
  • Is there any way I can do it using script/programming? – Dana Oct 9 '15 at 2:19
  • Yes, SSH will give you a terminal just like any other one. Whatever you can do from a Terminal you can do over SSH. – Daniel Oct 9 '15 at 2:46
0

I assume that you have open a terminal window and you can login on you VM via ssh.

Create a file ~/hello.sh with the next content:

#!/bin/bash
mkdir ~/testdir
cd ~/testdir
echo "Hello" > hello.txt

Next run the follow command

ssh user@host < hello.sh

You will be asked for password and the script will be run.

If you use certificate (recommended) change the command with

ssh -i path_to_sert user@host < hello.sh

Log via ssh and check what you have

cat ~/testdir/hello.txt

| improve this answer | |
0

By your question it seems that you want to log in to Ubuntu via GUI, then open a window with a terminal in it and execute some commands there. I may have misunderstood your question though. Please let me know if that's the case.

That's not a practical way to do it, and normally we'd rather use ssh to connect directly to the VM using the terminal on your physical computer. This means you can execute commands on your VM, but the output will be sent to the terminal on your physical computer - they won't show up on the VM's GUI screen with a terminal window open.

For this to work you must know your VM's IP address and install sshd (sudo apt-get install openssh-server) on the VM, then in your physical computer's terminal, you can execute:

ssh [VM's ip address]

to simply log in to its shell, or

ssh [VM's ip address] command

to execute [command] on the VM. If [command] is a shell script, it needs to be a shell script on the VM. You can also execute commands directly, for example:

ssh 192.168.0.100 ls -la
| improve this answer | |
  • Thanks for your response. Its really helpful. Just wondering, when I type ssh 192.168.0.100 ls -la in the terminal, it lists all the files and exits the ssh.Is this normal? What if I don't want to exit the ssh connection. Secondly, If I want to go to a directory and execute a command..? How to do this with single ssh command?(FYI: I tried doing this: ssh 192.168.0.100 cd /Desktop/folder ls -a ...But it only build an ssh connection and doesn't execute the remaining cd /Desktop ... command.) – Dana Oct 15 '15 at 23:13
  • Yes, when you execute ssh 192.168.0.100 ls -la it is supposed to exit afterwards. To go to a directory, then execute a command you can use semicolon to separate commands, or && if you want to execute the next command only if the previous one was successful. It's important that you enclose the commands in double quotes though, otherwise the second command will be executed locally. For example: ssh 192.168.0.100 "cd some-dir; ls -la" – Ronny Ager-Wick Oct 18 '15 at 18:18
  • Better yet, in order to stop executing commands should any of the commands return an error, use && instead of ; like this: ssh 192.168.0.100 "cd some-useless-dir && rm -rf ." . Obviously, you need to be careful with rm -rf ., but it's just a worst case scenario example. That command would wipe your home directory should some-useless-dir not exist had you used semicolon instead of &&. && is like saying "and if that went well, ...". The opposite is ||, which can be read "or if that didn't work, try ...". – Ronny Ager-Wick Oct 18 '15 at 18:20
  • I don't know of a way to execute a remote command, then stay in the remote console. As far as I know, you have to choose either to interactive or not. – Ronny Ager-Wick Oct 18 '15 at 18:29
  • @Dana does this answer your question? – Ronny Ager-Wick Oct 21 '15 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.