Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to respond to a prompt in a script

Status
Not open for further replies.

squash

MIS
May 21, 2001
99
US
Hello,
I am an intermediate scripter. Mostly for tools as an admin. Recently I have written a script that uses secure copy to copy several files from one box to another. after each copy takes place a prompt appears for the scp password. There must be some way in my script I can input this once and hold as a variable that responds to the question each time.

example of my script:

while :
do
echo "type in julian date"
read x

scp /logs/access.$x getlogs@111.111.1.122:/logs/access.$x
done

When this script is run I then just type in the 3 digit code for the julian date i need. Usually I must do 10 files at a time and each time it prompts for the scp password.

Any thoughts?

And we thank you in advance for your support.
 
You could try setting your password as a variable:

PASSWORD=<password>;export PASSWORD

then use this as an input to your scp line:

scp /logs/access.$x getlogs@111.111.1.122:/logs/access.$x <<!
echo $PASSWORD
!

I haven't tested this, so it might not work (possibly needing a carriage return on the end of the echo?). However, I'm sure others might be able to provide a solution if not. Be aware that hard coding passwords like this is a security risk - presumably one you're willing to take? HTH.

 
Well That may work, I am not comfortable with hardcoding a password. So I would make it a read variable, something like

echo &quot;enter password&quot;
read psswd

I will however try your redirrect idea with that in mind.
 
Ok I have tried this redirect with the following results

scp ..... <<echo $password

error unmatched <<

then i tried

scp ... <<echo &quot;$password&quot;

same thing

then

scp ... <echo $password
Still no good.

It was a good thought, just not the right one i guess.
Any other thoughts?

and we thank you for you support
 
You should be able to just replace my PASSWORD=etc with something like:

echo &quot;Enter password: \c&quot;
read passw
PASSWORD=passw;export PASSWORD

then use the <<! notation as in my first post. This tells the script to take everything between <<! and ! as input (so the echo might be superfluous). Don't know if it will work, but give it a go. Regards.
 
Oops. Forgot about the mighty dollar! The line:

PASSWORD=passw;export PASSWORD

should read:

PASSWORD=$passw;export PASSWORD

Apologies!
 
Hi,
Try PIPING the echo.

echo $PASSWORD | scp /logs/access.$x getlogs@111.111.1.122:/logs/access.$x


Use the code from the above posts to obtain $PASSWORD. Also if you are inside a script I don't think there is any reason to

export PASSWORD

but I could be wrong.

hope this helps.
 
Hmm,
ok I have tried

scp ...$x <<!$passwd!

syntax error at line 40 `<<` unmatched

Then I tried piping the $passwd

echo $passwd |scp .... $x

This just seemed to ignore the echo and still prompeted me
for the scp password.

Seems there should be some way for my script to look for a response from the scp program, similar to an errorout. Then when it sees this password prompt it can feed my variable to it.

Well If you have any thoughts this will be cool to complete.

And we thankyou for you support
 
Hi,
This may sound like a silly question but are we sure that SCP is issuing the read through STDIN?

Maybe since it is secure it is issuing it through its own file descriptor.

This could be why simple < and Pipe aren't working.

I did a search on the WEB for Secure Copy and found 6 or 7 different versions ranging from UNIX to Windows.

Whose version of SCP are you using?

 
scp -V displays

scp: SSH Secure Shell 2.3.0 (non-commercial version) on sparc-sun-solaris2.6

hope that helps you help me.
Thankx

 
squash,

you're missing the ! in the example, it's called a heredocument and, the first time you come across it, it's a bit weird.

this:

some_command <<!
1
2
3
!

Could be translated as:

run some_command and feed it with the next lines up to a line containing *just* the string after << (that's ! in this example)

As I said, weird but useful when you get used to it. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
This is one I have not come across, so let see if I understand correctly.

in my example, <<!command! , not correct.

Should be,

<<!
command
!

Did I get that.
Carraige return import here?
I will be back on Saturday night to try that.
Thankx for you patiance.
 
Hi,
Being a CSH programmer I have never seen this

<<!COMMAND!

CSH has a HEREIS input using the << but I was never aware you could actually put commands and or variables in this section.

But whether it is

<<!
command
!

or <<!Command!

or
<< 'hereis'
PASSWORD
'hereis'
( again hardcoding the password here is BAD )

IS irrelevent to the problem at hand. For whatever reason SCP is not reading from STDIN in the script It appears to be reading from its own File descriptor assigned to something like /dev/tty. the other curious thing is it appears SCP does this when it starts up so it is hard to echo something to /dev/tty and have SCP read it from the Parent script.

This is why none of these pipes and echo's seem to be working.

I wonder if '(' ')' might help.

echo $PASSWORD | ( scp .... )

or

set file = /tmp/myfile.$$
echo $PASSWD > $file
( scp ... ) < $file
rm -f $file


 
scp calls ssh which does not use stdin for password requests so you can not pipe the password to scp !!!!!!!
If your ssh server accepts using keys.
generate a private key ssh-keygen
scp the public key to other host.
scp $HOME/.shh/.identity.pub $USER@HOST:$HOME/.ssh.identity.pub
load shh key agent eval `ssh-agent`
Add your private key to your agent with
ssh-add It will ask for passphrase
passphrases no longer needed for scp


Tony ... aka chgwhat

When in doubt,,, Power out...
 
Well Sorry, but that last response is way over my head. or maybe its just working these night shifts.

I don;t know that much about scp or ssh server.

I had hoped for a simple solution. as in some way in a script to look for a specific text. like

password:

then feed a variable to the command line.

I do appreiciate your persistance though.

Thankx hope I can help you sometime.
 
squash,

Have a closer look at what &quot;Tony ... aka chgwhat&quot; suggests, it seems to me that it might be the right way to go. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top