Operating System - HP-UX
1827892 Members
1683 Online
109969 Solutions
New Discussion

How to su thru shell scripts?

 
Mahesh_3
Occasional Contributor

How to su thru shell scripts?

How can I change user id using shell scripts. How do I supply the password thru the script , say if I have the password stored in a variable?
Also if I try to ftp from unix to my local disk (giving my machine's IPaddress) it says connection refused. Do I need to change some permissions somewhere?
Thanx
A mech engineer learning computers!!!
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: How to su thru shell scripts?

Mahesh:

With regard to the use of 'su' in a script, if you are root, of course, you can use the 'su -' form and no password is required. For non-root scripts, while you can su to another account within a script, you will not be able to use a stored password. The user of the script will have to interact through his/her terminal to provide the password. Your script can, however, check the exit status of the su call, and so determine when/whether a good transition to the new user was made.

With regard to your FTP problem, you must, of course, have an FTP client running on your PC.

...JRF...
Michael Steele_1
Occasional Advisor

Re: How to su thru shell scripts?

I don't believe you can either su or newgrp from within a script. But you can automate a login, like ftp, with a .netrc file. For example, set up a ftpuser account and modify the .netrc file, add the appropritate commands that deposit files, for instance, and exit.
Remember Calvin and Hobbs
Antoanetta Naghiu
Esteemed Contributor

Re: How to su thru shell scripts?

If your script is owned by root, it is not a problem. If you want another user (except root) to have rights to execute someone else commands, I guess, the easiest way is to setup the secondary group. e.g. user1 belongs to group1 and to execute oracle stuff, user1 has dba as a secondary group. Play a little bit with permissions and setup the correct PATH in your script, and you are all set! Do not forget for secondary group to create the link ln -s /etc/group /etc/logingroup.
Using .netrc in user's home directory could be a security hole. Depends on the environment.
Using suid or sticky bit (s or t permission) could help you as well, depends in what you try to achieve.
For ftp, well, could be lots. What machine (OS, I mean) is your topdesk? Can you ftp from your topdesk to Unix? Try nslookup on Unix side to ensure the name resolution is corectly solved. Edit fptd line in /etc/inetd.conf to log ftp ( ftpd -l); recycle inetd daemon (inetd -c). See the error logged in /var/adm/syslog/syslog.log file.
See the other posts about ftp were lots of discutions.
Good luck.
Rick Garland
Honored Contributor

Re: How to su thru shell scripts?

Running the script as root and then doing an 'su -' will achive the results you are looking for. If running the script as a user and need to run as root, I would suggest something like 'sudo' which will run as root. Fom here you could 'su -' to another user if you wanted but I would be weary of all the different 'su's in a script.
George LaSalle
Occasional Advisor

Re: How to su thru shell scripts?

I'm wondering if Expect could be used in this situation.

Re: How to su thru shell scripts?

Yes, expect is a good answer for your problem. It will allow you to send the password without even setting a variable, or from a variable too.

the syntax is something like the following:

spawn "su -"
expect "Password:"
send ""

You may have to terminate each line with a semi-colon (;) I can't get to my script to verify right now. You have to have expect installed in order for this to work, I think it is part of the TKL stuff.