0

I am logging into remote machine through shell script (by placing ssh command in script). After ssh command ,The remaining lines of the script are getting executed on the current machine rather than remote machine. How to make the rest of shell script lines execute on remote machine.?

Lets say this is my script

ssh username@ip-address 
ls
whoami
----

The rest of lines after ssh should execute on remote machine rather than the current machine. How to achieve this?

Stunner
  • 961
  • 2
  • 19
  • 39
  • possible duplicate of [How to ssh from within a bash script?](http://stackoverflow.com/questions/1895185/how-to-ssh-from-within-a-bash-script) – Denys Séguret Oct 09 '13 at 07:12

4 Answers4

2

One possible solution would be to use a heredoc as in the following example:

$ ssh example.foo.com -- <<@@
> ls /etc/
> cat /etc/passwd
> @@

Basically everything between the @@ on the first line and the last line will be executed on the remote machine.

You could also use the contents of a file by either reading the contents of the file into a variable:

$ MYVAR=`cat ~/foo.txt`
$ ssh example.foo.com -- <<@@
> $MYVAR
> @@

or by simply performing the same action inside the heredoc:

$ ssh example.foo.com -- <<@@
> `cat ~/foo.txt`
> @@
zzzirk
  • 1,582
  • 2
  • 14
  • 18
  • Lets say , I have some 120-130 lines that need to be executed on remote machine. Then how can i pass those lines as argument to ssh – Stunner Oct 09 '13 at 07:21
  • A heredoc could still work. If those lines are in another file it could be read into a variable and the variable could be expanded inside the heredoc. It may be a better option to look at issuing multiple commands, the first being to push the file up as a script in /tmp and the second being to execute it. – zzzirk Oct 09 '13 at 07:25
1

Is your login passwordless.

If yes, you can just use pipe to execute the statement on the remote machine

like:

cat myshellscript.sh | ssh blah@blah.com -q
kailash19
  • 1,771
  • 3
  • 22
  • 39
  • I read that it is not possible to pass password as argument to ssh. Is that true?... – Stunner Oct 09 '13 at 07:24
  • Is creating a pair of keys out of the question? Having to store the password in a script is generally a really bad idea. – zzzirk Oct 09 '13 at 07:28
  • You dint read my question correctly.What you told works when I have a myshellscript.sh as a external file. But what i have is some 100 lines of code after ssh command in the shell scrip and i need to execute those lines after logging into remote machine – Stunner Oct 09 '13 at 07:40
  • You could try making the password the first line of the heredoc in my answer above and see if that works. I'm not certain if it will, but it could. – zzzirk Oct 09 '13 at 07:49
  • You cant create keys??, even if not, what about expect?? – kailash19 Oct 09 '13 at 07:52
0

Use ssh command with -t options. For example:

ssh -t myremotehost 'uptime'
name@myremotehost's password: 
10:14:14 up 91 days, 21:20,  5 users,  load average: 0.20, 0.35, 0.36
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
0

ssh -t user@remotehost 'uptime' user@remotehost's password: 23:35:33 up 2:05, 3 users, load average: 0.79, 0.52, 0.60 Connection to remote closed.

When you specify -t, it opens the terminal in remote machine and execute the command.