Run yes/no script remotely


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run yes/no script remotely
# 1  
Old 02-19-2015
Run yes/no script remotely

I have this script in server2

Code:
[root@server2 ~]# cat /root/yesno.sh
#!/bin/bash

read -p "are you sure?" -n 1 -r

if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo ""
        echo "YES"
else
        echo "NO"
fi


[root@server2 ~]# sh  /root/yesno.sh
are you sure?y
YES

It works fine. I have installed ssh keys between server1 and server2. When I run the script from server1 as follows, it does not prompt the question. However, if I press Y, it gives correct output. How can I get the question printed into the shell?


Code:
[root@server 1~]# ssh root@11.22.33.44   sh  /root/yesno.sh
y

YES

# 2  
Old 02-19-2015
You are using bash features, but running it in sh.

To run it in bash, use bash.
# 3  
Old 02-19-2015
There's a shebang #!/bin/bash in the script, but running it with
sh scriptname bypasses the shebang's effect. The reote script needs to be executable. And run that way.

Code:
# one time only
ssh root@11.22.33.44 ' chmod +x /root/yesno.sh'

ssh root@11.22.33.44 ' /root/yesno.sh'

Also try to keep the remote command surrounded by ' or by " or you will get unexpected results - in general. See the examples above.

PS: root access by ssh is usually a bad idea. If this is a home network, maybe. For business, no. Would not pass a reasonable security audit.
# 4  
Old 02-20-2015
I have modified to /bin/sh and changed script to 755. Still same result. I am using ssh keys between servers.

Code:
# cat /root/yesno.sh
#!/bin/sh


-rwxr-xr-x 1 root root 116 Feb 20 05:17 /root/yesno.sh


#  ssh root@IP /root/yesno.sh
y

YES


#  ssh root@IP   '/root/yesno.sh'
y

YES


# ssh root@IP  "/root/yesno.sh"
y

YES

# 5  
Old 02-20-2015
Try
Code:
ssh -t root@10.1.1.3 ./yesno.sh
are you sure?

# 6  
Old 02-20-2015
Quote:
Originally Posted by anil510
I have modified to /bin/sh and changed script to 755. Still same result.
Before, the problem was that you were accidentally running it in sh when you should have been using bash.

Now, you are intentionally running it in sh when you should be using bash.

If you want bash features like read -p, use bash!
These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[perl] execute remotely script

Hello Can some help with write part of perl script I need something like this in perl SSH="/bin/ssh -o BatchMode=yes -o" USER="test" SRV="server" SCRIPT_TO_EXEC="/tmp/test.sh" -> shell script OUT=/tmp/out.file ${SSH} -l ${USER} ${SRV} 'sudo /usr/bin/ksh -s' < ${SCRIPT_TO_EXEC} >> ${OUT}... (1 Reply)
Discussion started by: vikus
1 Replies

2. Shell Programming and Scripting

Find not finding stuff if run remotely

Hello I'm working on script to find tomcat on all my servers. Then find out what version of tomcat is installed. Basically I want to check and see if the latest version is installed. I'm testing the script on Solaris 10. I'm also going to need it to work on RHEL and SLES. If I run the following... (7 Replies)
Discussion started by: bitlord
7 Replies

3. Shell Programming and Scripting

Then error while running script remotely

facing issue with then error while running a local script aginst a remote server. i facing the same issue in multiple scripts. So what i am missing here or what is needed. #!/bin/ksh echo "enter the filename" read file if then echo "file exists" else echo "file does not exists" fi ... (0 Replies)
Discussion started by: NarayanaPrakash
0 Replies

4. Shell Programming and Scripting

Remotely Execute a script

Hi, What is the best way to remotely execute a script? Scenario: 1 Unix box creates a file and moves it to a 2nd unix box 2nd unix box must then move the file to a windows server How can i execute the ftp script on the 2nd server, I need to schedule the command to automate it!? I... (3 Replies)
Discussion started by: mcclunyboy
3 Replies

5. Shell Programming and Scripting

Create a list of commands in a central location, and then run them all remotely through a ssh here

I'm trying to write a script that in the end will from one central location hop to a bunch of servers and then run a series of ping tests. The thing is, the list of devices that needs to be pinged is going to be different for each server. So what I want to do is be able to do is read through the... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

6. Shell Programming and Scripting

howto run remotely call function from within script

Hi I have the following script : #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` } ####################################################################### # # print... (2 Replies)
Discussion started by: presul
2 Replies

7. Shell Programming and Scripting

help needed. run shell scipt remotely

Dear all , I have a script. this script called get.sh and can get some solaris infomation and save the result as result.tar.gz. the problem is : we have 12 servers. every time. I need to login 12 server and do the same job 12 times.:mad: master server ... (2 Replies)
Discussion started by: chinesefish
2 Replies

8. Shell Programming and Scripting

how to avoid space to run remotely

If I run the following command remotely after ssh than it works fine su - oracle -c "/oracle/product/102/db/bin/dbshut" But If I run the following command it doesn't work su - oracle -c "/oracle/product/102/db/bin/lsnrctl stop" Because I think there is a space is present between lsnrctl and... (1 Reply)
Discussion started by: madhusmita
1 Replies

9. UNIX for Dummies Questions & Answers

Exit from telnet when run Remotely

ssh user@host -q -n 'grep `hostname` /etc/hosts; telnet 10.100.23.45 1234;' When i run this command remotely it is hanging and not giving me the prompt, Can anyone tell me how can I exit a telnet command remotely please. Thanks. (10 Replies)
Discussion started by: venu_nbk
10 Replies

10. Shell Programming and Scripting

Executing remotely the script

Hi All, I have a script to be executed in another machine. I connect to that machine from another server as a root ( this is the only configured access, as i cannot log in as a normal user). After that I have to switch to a normal user and that I can be able to executge that script. But all this... (12 Replies)
Discussion started by: elthox
12 Replies
Login or Register to Ask a Question