Click to See Complete Forum and Search --> : Shell Scrips, user inputs and passwords


Cerf
02-12-2003, 05:46 PM
Yo yo yo,

Ive just started to mess around with shell scripting because I have a whole bunch of commands that I have to type when I do a few things so the script felt like a smart thing to do.

When I run my script (this one to mount 3 filesystems) I need to type in a password. Is there a way where I can run the script without entering my password, or have the password within the script then prevent other users from reading/writing to the script (only execuiting).??

And how can I get usering input. For instance when I type "mount" I need a whole bunch more variable like "-o umask=777" how can I do things like that in my scripts??

Thanks for all your help

hlrguy
02-12-2003, 06:06 PM
I use expect to create interactive scripts that communicate with screen prompts. I am sure there are other ways. (Note: autoexpect can automate the script create). Here is a simple example of ssh'ing into a remote machine.

#!/usr/bin/expect

spawn ssh <hostname> -l <username> -F /root/.ssh/ssh_config

expect {
-re "password: " { send "<password>\r"}
}

interact {
}

As you can see, the password is in the file as plain text. If you were to make this one as permission 700 (only root can read/write/execute). I don't know of any way to automate password scripting where it isn't visible to whomever is allowed to run it. You need to have read permission to be able to execute. i.e. chmod 744 isn't going to work.

hlrguy

On the input side...pretty easy. Note syntax is for KSH, what is used at work. Every shell has their own unique shell quirks. You will have to search out examples, heck look at 25% of of the files in /usr/bin are scripts, not executables.

#!/bin/ksh -f

if [[ $1 != "T1" ]] && [[ $1 != "V35" ]]
then
echo "Please specify if links are T1 or V35"
echo "usage: setup {T1/V35}"
exit
fi

You can do this for as many variables ($1, $2, $3...) as you like

Cerf
02-12-2003, 06:30 PM
Originally posted by hlrguy


#!/usr/bin/expect

spawn ssh <hostname> -l <username> -F /root/.ssh/ssh_config

expect {
-re "password: " { send "<password>\r"}
}

interact {
}



didnt work, I am a newbie to this so.....
is there another way you can rewrite it for me
and whats ssh??

hlrguy
02-12-2003, 07:09 PM
using expect is tricky it is an exactly science. The script above was purely for example. ssh is how I log into to my work/lab machines from home. This script automates the process.

Give autoexpect a try. After you start it, it records EVERY thing you type and records all the screen prompts. Then, when you exit, it will have the entire session scripted in expect.

hlrguy

man expect
or
http://nodevice.com/sections/ManIndex/man0334.html

man autoexpect
or
http://nodevice.com/sections/ManIndex/man0072.html

Cerf
02-13-2003, 05:35 PM
OK

I need to run a command as root
how would I do this

Can you make an example for me useing "su" as the username, "psswd" as the password, and "mount" as the command??

hlrguy
02-13-2003, 07:04 PM
I have attached one that works on my system EXCEPT, replace your root password with your root password. I replaced mine with ???????

Also, when you
su - root

make sure that the left prompt looks like mine does (i.e. root#) if not, replace that with what yours looks like. For example, root%, etc)

I stuck the sleep in there just so that you can see more examples. It is really easy.

Spawn ANY command,
expect something, send something, expect something, send something.

If you want to see what it is doing, or debug a new script, just put
<space>-d

after expect in the first line of the script.

hlrguy

Cerf
02-13-2003, 07:48 PM
Didnt work

I tried to get the script to run "ls /root" but when I run the script I got
"spawn su - root
Password: (my password)
ls /root


"

and there I stoped it useing Ctrl+Z
then I tried the script again and I got
"spawn su - root
Password: (my password)
ls /root"

again but then I typed "ls /root" and nothing happened

hlrguy
02-13-2003, 07:59 PM
Add the
-d
to show all the debug info. Also, did you make sure to at the '\r' to the password?

hlrguy

Cerf
02-13-2003, 10:48 PM
Now with the -d im getting an error
"spawn -d su root
couldn't execute "-d": no such file or directory
while executing
"spawn -d su - root"
(file "./runroot.sh" line 4)"

bwkaz
02-13-2003, 11:48 PM
The -d doesn't go after spawn. ;)

It goes after expect, on the first line of the script. The first line should look something like #!/usr/bin/expect now (the path may be different, depending on where expect installed itself); add a space and a -d to the end of that.

hlrguy
02-17-2003, 05:24 PM
Don't know if you gave up on this, however, I took autoexpect for the test drive of it's life.

autoexpect
<Do EVERYTHING you want>
exit

creates a file called script.exp

expect script.exp
and it repeats what you just did.

I tried it to do all the following. ftp into a test box, upload 1200 messages from dir a, upload 400 testcases from dir b, upload 10 batch files to run the above. I then turn around and download all of the above to the other 12 test boxes to keep them in sync. I was getting tired of doing it manually. (note in ftp, enter verbose to turn off status). It took 3 1/2 hours to execute. After creating the expect script, I deleted ALL the downloaded test tools, ran autoexpect script.exp and poof done.

It even handled the errors. It dutifully entered the wrong dir as I had typed it the first time, etc. Didn't matter, perfect result.

hlrguy