Multiple interpreter declarations

Hi,
I am writing a shell script that connects to a remote server and performs some tasks on the server and exits.
Since i am using a ssh connection, i am using a "expect" utility to supply the password automatically (which is present within the script).

In order to use this utility, i need to declare the executable path at the beginning of the script using interpreter declaration.
something like this: #!/usr/bin/expect

This works fine so far.
However, i am using this script as part of CGI-programming too.
The input/output of this script is available via web browser with the help of cgi-programming.

But, the real problem is , in order to achieve cgi-programming with this script, i need to have a interpreter declaration about the path of the shell.
something lik this: #!/bin/bash

Now,, the problem is , i cant have both interpreter declarations in the script.
It throws error if i try.
So, it leaves me with an option to use any one of the interpreter declarations.

is there any way or a workaround to get both the features.
What i mean is - is there any way to declare both the interpreters ( #!/usr/bin/expect and #!/bin/bash) within my shell script.

Appreciate your help.
Thanks.

Have you considered using an rsa key pair and authorized_keys file?

On the machine you are logging in from, cd to the home directory of the user you will be logging into the server as. Once you are there cd to .ssh
If .ssh does not exist, create it. Run the command:

ssh-keygen -t rsa

hit enter on all three questions to accept the defaults
copy the id_rsa.pub to the server you are logging into
On the server go to the user's home directory you will be logging in as.
cd to .ssh
append the contents of the id_rsa.pub you just copied to the server to the authorized_keys file

now you should be able to log in with no password, just make sure you are running the script as the user you will be logging in as.

1 Like

Thanks ilikecows for a very useful solution.
For a moment i thought this would be the best solution that you provided.
However, i can see another problem.
The problem is that this script connects to any server (specified by the user) in the network.
It means that there might be a new server coming up in the network all of a sudden and when a user runs this script, it should be able to login automatically without user intervention.

If i follow your solution, i would have to login into each and every server (that the script is goin to login ) and copy the id_rsa.pub there, which again doesn't help me as i have to give this script to the users and it wont be good to ask them to do the above tasks. :frowning:

You could always write a wrapper script in ksh or bash that your web server runs that calls the expect script.