Switching user


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Switching user
# 1  
Switching user

I need to do a switch user in an automated mode and do a ftp using that switched id.

Scenario:
initial login xx.
switch to user-yy without manually entering the password.
ftp some files from user yy to another user zz - automated mode.

Can any unix experts can help me for my above query?
# 2  
https://www.unix.com/shell-programmin...x-solaris.html <--- will this be ok for you to give you some kind of idea?
# 3  
#!/bin/sh
HOST='something.else.com'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0

Linux, Unix and BSD users have the alternative of using a .netrc file. The ftp man page documents the format of .netrc. To accomplish the task of using ftp in a shell script you would have to fill out a .netrc file something like this:


machine something.else.com
login myid
password mypassword

ftp demands that .netrc not have group or world read or write permissions:


$ ls -l .netrc
-rw------- 1 bediger users 51 Dec 16 13:30 .netrc

Using a .netrc file has a few problems that may or may not prevent you from using it.

A shell scripkt that does FTP using .netrc is no longer self-contained. You have to keep track of two files, which means that bugs can be less than obvious.
ftp reads it's user ID's .netrc. If you develop your script under a given user ID, then put it in production under a second user ID, you have to coordinate .netrc file contents between those two user IDs.
# 4  
Switching user

This will help me in ftping. but how do i do a automated switch user?
# 5  
Code:
su - myotheruser -c "thatftpscript.sh"

# 6  
switching user

thanks zaxxon but it still asks for the myotheruserid password. how do i avoid or automate it?
# 7  
the .netrc file is the way to go - note the permissions on the .netrc file (contained in the home directory of the user executing the ftp) must be 00 to group and world... the user executing the ftp call is *usually* not the same user logging in and executing the ftp commands.

the call is in your script:
ftp -iv ftp.somesite.net 1>>$COMMDIR.out 2>>$COMMDIR.err

that call uses the .netrc which looks like this:
machine ftp.somesite.net login OTHERUSER password 53MB3e
macdef init
binary
cd incoming
put AMR.dat
cd ../outgoing
mget *Report.txt
mdelete *Report.txt
mget *.xml
mdelete *.xml
quit
#####END SOMESITE#####

Also you have to have at least one empty line following the macdef init entry in the .netrc file for it to be read.

Last edited by kittielemieux; 07-17-2008 at 08:53 AM..
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #428
Difficulty: Medium
JavaScript supports regular expressions in a manner similar to Perl.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switching to user to stop db

Hi all, I have a script that I will need to run occasionally to stop my db2 instance- stopDB2.sh su -l -c "db2 force application all" su -l -c "db2 terminate" su -l -c "db2 stop" su -l -c "db2licd -end" This works when I su to the instance owner (archive), and run each line. I need to... (5 Replies)
Discussion started by: jeffs42885
5 Replies

2. UNIX for Dummies Questions & Answers

Switching from root to normal user takes me to user's home dir

Whenever i switch from root to another user, by doing su - user, it takes me to home directory of user. This is very annoying as i want to be in same dir to run different commands as root sometimes and sometimes as normal user. How to fix this? (1 Reply)
Discussion started by: syncmaster
1 Replies

3. UNIX for Advanced & Expert Users

Switching user in AIX 5

I need to do a switch user in an automated mode and do a ftp using that switched id. Scenario: initial login xx. switch to user-yy without manually entering the password. ftp some files from user yy to another user zz - automated mode. Can any unix experts can help me for my above query? (1 Reply)
Discussion started by: mjdarm
1 Replies

4. Shell Programming and Scripting

su (switching to other user)

Hi, what is the use of the double quotes and !! in the following code segment: su - user1 << ""!! > /dev/null 2>&1 echo "welcome user1" EOF !! also what is the difference between below: su - user1 << ""!! > /dev/null 2>&1 and su - $USER << ""!!!> /dev/null 2>&1. Note: $USER =... (2 Replies)
Discussion started by: bjagadeesh
2 Replies

5. Shell Programming and Scripting

su (switching to other user)

Hi, what is the use of the double quotes and !! in the following code segment: su - user1 << ""!! > /dev/null 2>&1 echo "welcome user1" EOF !! also what is the difference between below: su - user1 << ""!! > /dev/null 2>&1 and su - $USER << ""!!!> /dev/null 2>&1. Note: $USER =... (1 Reply)
Discussion started by: bjagadeesh
1 Replies

6. Shell Programming and Scripting

switching to another user in shell script...

Hi, I have a shell script in which I need to switch to another user and execute some commands and then come back to the original user. To make it more clear - I have to log in as user root then 'su' to jag - execute a script called backup.sh and then logout and come back to root again.. ... (1 Reply)
Discussion started by: bjagadeesh
1 Replies

7. Shell Programming and Scripting

switching user from root to ordinary user

Good day Guys!!! I am currently making a script in AIX, the script runs a SAS job, the owner of the script is the root, but the SAS jobs cannot be run by the root, as it should be run by a user 'sasia'. But inside the script, root creates a logfile, so what I need is just to su to sasia for the... (3 Replies)
Discussion started by: sasia
3 Replies

8. UNIX for Dummies Questions & Answers

FTP - switching user syntax

Running the following shell script, #!/usr/bin/ksh set -x swdofile=/opt/SWDO_IN1V01P001_1.csv USER='myusername' PASSWD='mypassword' HOST='myhostname' ftp -n $HOST << SCRIPT quote USER $USER quote PASS $PASSWD su - BRA -c put $swdofile quit SCRIPT exit 0 but not managing to get the... (1 Reply)
Discussion started by: daveaasmith
1 Replies

9. Shell Programming and Scripting

switching between root and a normal user

I am writing a script that has some tasks that must be run as root, then set of tasks to be run as normal user, then again as root. is there a way to switch between users in a script? any other alternatives? thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

10. UNIX for Dummies Questions & Answers

Switching to single-user mode

Hello everyone, I need to make a OS full backup. I am using the vdump command but first, I must to switch to the single-user mode. I am working on a Compaq Tru64 Unix V4.0G. Please, could somebody tell me which is/are the commands to do it? I appreciate your help Gastn (1 Reply)
Discussion started by: gmoyano
1 Replies

Featured Tech Videos