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

2 Answers2

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
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 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 http://picpaste.com/pics/Screen_Shot_2015-02-20_at_6.28.46_PM-b4o1Tpqf.1424482205.png Thanks. – user610209 Feb 21 '15 at 01:30
  • This fixed it for me http://unix.stackexchange.com/questions/100652/bash-interactive-remote-prompt ssh -t root@redhat1 "$(<test2.sh)" – user610209 Feb 21 '15 at 01: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? – Chris Davies Feb 21 '15 at 08: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. – Chris Davies 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.

PersianGulf
  • 10,850