Script as login shell (passing args to login shell)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script as login shell (passing args to login shell)
# 8  
Old 03-31-2014
Its a simple script that take an IP and NAME_RECORD and update a dns file. Relaunches named after the change.

Thing is the user1 CANNOT have a valid login shell so the remote script acts as a login shell

Code:
usage () {
        cat >&2 <<EOF

Insert/delete IP addresses and name records
in hosts DNS.
OS: RHEL 5.5

Usage: ${0##*/} -i \$IP_ADDRESS -n \$NAME_RECORD [ -D optional ]

- Default is to Insert
- D switch to Delete

EOF

Sorry for the confusion but i took the name x.sh from blackrageous to simplify the example. the real script name is insert_host_ip_named.bash
# 9  
Old 03-31-2014
I don't understand how that script could possibly do anything, at all, ever. It's nothing but a here-document.

Less jokingly, post the complete script please, my crystal ball is not functioning today Smilie
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 03-31-2014
Ok you asked for it Smilie

Code:
#!/bin/bash

#  Name: Eric
#  Version: 1.0
#  Date: 17/02/2014

CONFIG_FILE=/home/user1/hosts.unix-int
BACKUP_DIR=/home/user1/backup
LOG_PATH=/home/user1/log
LOG_FILE="user1_dns_${DATE}.log"
DATE=$(date +%Y%m%d)

# Default is to add record
REMOVE=N

# Usage
usage () {
        cat >&2 <<EOF

Insert/delete IP addresses and name records
in hosts DNS.
OS: RHEL 5.5

Usage: ${0##*/} -i \$IP_ADDRESS -n \$NAME_RECORD [ -D optional ]

- Default is to Insert
- D switch to Delete

EOF
}

# Manage error
error_log () {
        printf "[ $1 ]: %-30s\n" "$2" | tee -a $LOG_PATH/$LOG_FILE
}

# Validate if param are ok
validate_params () {
        results=$(echo $IP_ADDRESS | awk --re-interval -F"." '$0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 {print}')
}

# Validate if inside cloud
validate_cloud () {
        awk '/####START/,/####FIN_CLOUD/{print}' $CONFIG_FILE | egrep -q '${IP_ADDRESS}|${NAME_RECORD}'
}

# Search for existing ip in configuration file
search_existant_ip () {
        grep -q "^${IP_ADDRESS}" $CONFIG_FILE
}

# Search for existing host in configuration file
search_existant_host () {
        grep -q "${NAME_RECORD}" $CONFIG_FILE
}

# Manage backups of host file
backup_host_file () {
        if [ ! -d $BACKUP_DIR ]; then
                mkdir $BACKUP_DIR
        fi
        cp -p $CONFIG_FILE "${CONFIG_FILE}.$DATE"
}

# Do the modification to the host file
add_host () {
        sed -i "/####FIN_CLOUD/i $IP_ADDRESS\t$NAME_RECORD" $CONFIG_FILE
        search_existant_ip
        if [[ $? -eq 1 ]]; then
                error_log "ERROR" "IP not found after ADDING $IP_ADDRESS"
                exit 1
        fi
        search_existant_host
        if [[ $? -eq 1 ]]; then
                error_log "ERROR" "HOST not found after ADDING $NAME_RECORD"
                exit 1
        fi
        error_log "ADD" "$IP_ADDRESS - $NAME_RECORD"
}

remove_host () {
        validate_cloud
        if [[ $? -eq 1 ]]; then
                error_log "ERROR" "IP/HOST Found but outside cloud"
                exit 1
        fi
        sed -i "/^$IP_ADDRESS\t$NAME_RECORD/d" $CONFIG_FILE
        search_existant_ip
        if [[ $? -eq 0 ]]; then
                error_log "ERROR" "IP found after REMOVING $IP_ADDRESS"
                exit 1
        fi
        search_existant_host
                if [[ $? -eq 0 ]]; then
                error_log "ERROR" "HOST found after REMOVING $NAME_RECORD"
                exit 1
        fi
        error_log "REMOVE" "$IP_ADDRESS - $NAME_RECORD"
}

# Main

# Check number of arguments
case $# in
        4|5) ;;
        *) usage; exit 1 ;;
esac

# Parse the arguments
while getopts i:n:R OPTION; do
  case "$OPTION" in
        D) REMOVE=Y
        ;;
        i) IP_ADDRESS=$OPTARG
        ;;
        n) NAME_RECORD=$OPTARG
        ;;
        :) usage ; exit 5
        ;;
        \?) usage ; exit 5
        ;;
  esac
done

shift $((OPTIND-1))

# Validate params

validate_params $IP_ADDRESS
if [[ -z $results ]]; then
        error_log "ERROR" "Incorrect IP parameters"
        exit 5
fi

search_existant_ip
if [[ $? -eq '0' ]] && [[ $REMOVE == 'N' ]]; then
        error_log "ERROR" "Ip Address already exists in host file"
        exit 1
fi

search_existant_host
if [[ $? -eq '0' ]] && [[ $REMOVE == 'N' ]]; then
        error_log "ERROR" "Name record already exists in host file"
        exit 1
fi

backup_host_file
if [[ $? -eq '1' ]]; then
        error_log "ERROR" "Problem with backup copy"
        exit 1
fi
if [[ $REMOVE == 'Y' ]]; then
        search_existant_ip
        if [[ $? -eq '1' ]]; then
                error_log "ERROR" "Ip Address not in host file"
                exit 1
        fi
        search_existant_host
        if [[ $? -eq '1' ]]; then
                error_log "ERROR" "Host not in host file"
                exit 1
        fi
        remove_host
else
        add_host
fi

# Launch restart_named.sh

#service restart restart_named.sh
#/root/Restart_named.sh

exit 0

---------- Post updated at 06:07 PM ---------- Previous update was at 06:03 PM ----------

Food for thoughts....

I could probably skip the -n and -i cause these will be launched via a php page. So i could assume these will always be the same.

Then could i take $2 and put everything into an array and play with that to achieve the desire result?
# 11  
Old 03-31-2014
I take it you're able to modify the contents of x.sh to your pleasing?

I automated a PHP page which grabbed data via ssh by reading text from stdin via read whenever an argument wasn't present. It worked like this:

Code:
if [ -z "$KEYNAME" ]
then
        echo "Input key name" >&2
        read KEYNAME
fi

# Lots and lots of checking code for a valid value of KEYNAME

This would let you drop the minimum-4-arguments requirement.

The >&2 was to print text to stderr and data to stdout, so the output of this script could be raw data.
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 03-31-2014
The script insert_host_ip_named.bash is mine but the webpage php code is not. I can give them what i want triggered but cannot modify the this flow:

User input info in php web page ----> that triggers from the http server the ssh to the named server which hosts the above script that modifies the dns files and relaunches named.

That is the full plan.
# 13  
Old 03-31-2014
So the PHP page would not be triggering it via ssh?
# 14  
Old 03-31-2014
Exactly the opposite .... the php page will trigger the ssh to the remote server.

You put in the info on the php page (those become args for my script).

The php code will trigger the ssh with the args

ServerA (php code, user input) --- launches the ssh with args --- ServerB receive the ssh with args and trigger upon login my above script with the correct args.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for login user and email

Guys please help me I have a linux class and I want to write a shell script who shows which user loged in and show the process that are active in his/her shell in another text file and email that file to root just when the user loged out Thanks every bod (1 Reply)
Discussion started by: hamedk1122
1 Replies

2. Shell Programming and Scripting

How to login as a different user inside a shell script?

hi, i want to login as a different user inside a shell script and then call another shell script from that script. how to do that? original script : script_A.sh so when the script_A.sh is called , i want to login as a different user and then call another shell script(script_B.sh) from... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

passing login details to htaccess login prompt

Hi, How i can pass the login details to the URL which is password protected with the htaccess using command line or script (perl,or shell,or php). Any help or hint appreciated. Thanks, SJ (4 Replies)
Discussion started by: SilvesterJ
4 Replies

4. Shell Programming and Scripting

Help with Unix bash shell script login

Hi, I am a complete Unix novice and need some help with creating a login shell script. I have created a file with user details i.e. PIN, name etc and require help in recalling the specified details from the file and being prompted for a password on login. Any help would be very much appreciated.... (0 Replies)
Discussion started by: tdsrogers
0 Replies

5. Shell Programming and Scripting

SSH - Passing Unix login passwords through shell scripts

Hi All , I need to call a script runscript_B.sh on server A, the runscript_B.sh script locating in server B. The runscript_B.sh in calls another script runscript_A on server A itself. it seend, i need to be connect from Server A to Server B using ssh. I have tryed like this in... (3 Replies)
Discussion started by: koti_rama
3 Replies

6. Shell Programming and Scripting

Login with Shell Script.

Dear All, I need to create a shell script which will login to a unix system with user root. I also need to supply the password for root through script only instead of entering it manually. After i am logged in to the system i need to excute all the necessary commands. so far i have done... (7 Replies)
Discussion started by: Siddheshk
7 Replies

7. Shell Programming and Scripting

login from a shell script?????

Any help on this ..... its a bit urgent !!!! Hi Can anybody provide info about the following??? i want to issue su (switch user) command from within a shell script how to take the password without user intervention from the shell script only???? i.e using apssword which is already... (2 Replies)
Discussion started by: skyineyes
2 Replies

8. HP-UX

cannot login after changing login shell

Hello Everyone, I am a newbie in unix. I was practicing shell scripts on hp unix machine. I changed my current login shell (Korn) to Bourne shell giving the following command. $ chsh username /usr/bash I am using secure shell client for accessing the hp ux server. After which i... (4 Replies)
Discussion started by: hardesh
4 Replies

9. Shell Programming and Scripting

remote-login via Shell-Script

Hello all, I would like to login from one unix-system with a (tcsh)-script to an other unix-system.The login-procedure and the transmission of username, resp. password runs via ssh. The problem is after logging onto the remote server once "Enter" has to be pressed, before one gets to the... (1 Reply)
Discussion started by: Juergen Paepke
1 Replies

10. UNIX for Dummies Questions & Answers

remote login via shell script

is it possible for me to have a shell script log me in to a telnet session? i have in mind something along the lines of % telnet host < script, where the first two lines of script will be username and pass followed by a list of commands to execute on the remote host. this doesn t work as... (4 Replies)
Discussion started by: lethe
4 Replies
Login or Register to Ask a Question