2

I have a script that asks for input from the user. When I run this locally it does what I want however I'd really like to run it over ssh.

I've tried the regular ways I'd run a script:

ssh someaccount@somemachine 'mysscript.sh'

ssh someaccount@somemachine 'bash -s' < myscript.sh'

but when it runs it does not wait for any user input and doesn't allow you to chose from the menu.

#!/bin/bash
# Bash Menu Script Example

echo -n "What machine is sick ?"
  read machine

PS3='Please enter your choice: '
options=("Ping $machine" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Ping $machine")
            echo "you chose to ping $machine"
                        ping -c1 $machine
            ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

Thanks

  • select takes numbered options. – mikeserv Feb 20 '15 at 4:18
2

To run an interactive command via the ssh command line you need to tell ssh to allocate a tty on the remote end. (Usually it doesn't bother, and most of the time this is a good assumption.) Add the -t flag to allocate a tty:

ssh -t someaccount@somemachine mysscript.sh
  • No thats not working either. I set up to default redhat machines. Please take a look at the screen shot to see what I'm seeing picpaste.com/pics/… Thanks. – user610209 Feb 21 '15 at 1:30
  • This fixed it for me unix.stackexchange.com/questions/100652/… ssh -t root@redhat1 "$(<test2.sh)" – user610209 Feb 21 '15 at 1:56
  • @user610209 so your script isn't on the remote server? Why didn't you say so, and why didn't you report the error you would have received when trying to run a script on the remote server that didn't exist? – roaima Feb 21 '15 at 8:17
  • From initial question - "When I run this locally it does what I want however I'd really like to run it over ssh." Run over ssh i.e. run it remotely. Thanks for your help. – user610209 Feb 21 '15 at 16:25
  • @user610209, OK. I'd assumed from your first example that you had the script on the remote machine. Either way, though, I'm pleased you're sorted with a solution. – roaima Feb 21 '15 at 16:36
0

You have to do :

root@debian:/home/mohsen# ssh mohsen@192.168.1.10 "~/test.sh"
mohsen@192.168.1.10's password: 
What machine is sick ?192.168.1.4
1) Ping 192.168.1.4  3) Option 3
2) Option 2      4) Quit
Please enter your choice: 1
you chose to ping 192.168.1.4
PING 192.168.1.4 (192.168.1.4) 56(84) bytes of data.
64 bytes from 192.168.1.4: icmp_req=1 ttl=64 time=0.370 ms

--- 192.168.1.4 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.370/0.370/0.370/0.000 ms
Please enter your choice: 

Syntax of remote run is :

ssh account@remote_machin  "command_for_run_on_remote_machine"

Above syntax have been used to backup for sysadmins.

  • Thanks for the reply but that doesn't seem to be working for me either. It behaves exactly the same as if I used the 2 methods mentioned in my question. Any other suggestions ? – user610209 Feb 20 '15 at 5:50
  • I think it's related to sshd_config – PersianGulf Feb 20 '15 at 13:06
  • You can do it : ssh -v mohsen@192.168.1.10 "~/test.sh" – PersianGulf Feb 20 '15 at 13:14

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.