LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Echo "password" to su (https://www.linuxquestions.org/questions/linux-newbie-8/echo-password-to-su-864999/)

csravi88 02-25-2011 10:19 AM

Echo "password" to su
 
Hi,

I have to run "pppd call idea" command from root shell every time to connect to internet from mobile. Now i want a script so that i just run it to connect. someting like :

#!/bin/sh
echo "password" --stdin | su -
pppd call idea

but its showing error that "standard in must be a tty".

why is this. using CentOS 5.5

Thanks,
Ravi

acid_kewpie 02-25-2011 10:24 AM

it's correct to say that. What you want to do is pretty horrible, don't do it. You can use sudoers to allow a normal user to execute a single command if necessary. see the sudo / sudoers manpage for more details.

PTrenholme 02-25-2011 11:42 AM

Also, look at the /etc/rc.d/rc.local file. That's a script that is automatically run by the init script, as "root," before your system starts the X-server. It's designed for stuff like you want to do, Here, for example is my rc.local script for this laptop:
Code:

$ sudo cat /etc/rc.d/rc.local
[sudo] password for Peter:
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
local_dir="/etc/rc.d/"
touch /var/lock/subsys/local
#-----------------------------

# Turn off all output
#exec &>/dev/null

# Load the local function definitions
__local=T
. ${local_dir}/rc.local_functions
echo
echo -----------------------------------------------------
echo Starting $0

# Connect to the TSS3 network if it's available
connect_to TSS3

# Activate and mount any available, unmounted, volume groups with mount points in /etc/fstab
vg_activate

# Mount any "noauto" devices listed in /etc/fstab that exist and are not already mounted
mount_noauto

# Mount any "noauto" cifs devices
mount_cifs

# Mount up to max_loop ISO files located in /ISO under /mnt/ISO/
mount_iso
echo -----------------------------------------------------
echo

<edit>
Oh, note that that script uses my rc.local_functions. Since you're doing network connection stuff, here's the part of that script that connects my laptop to my local network. (Note the sucmd function that let's me run the privileged stuff in the other functions if, for some reason, I need to rerun one of them during without rebooting.)
Code:

$ cat /etc/rc.d/rc.local_functions
#!/bin/bash
#############################################################################
#
# Define some terminal control codes
# See man console_codes for terminal display code descriptions.
#
#############################################################################
csi="$(echo -en \\e)[0;"
red="${csi}1;31m"
green="${csi}1;32m"
blue="${csi}34m"
cyan="${csi}36m"
white="${csi}37m"
reset="${csi}39m"

#############################################################################
#
# Return an empty string if called from rc.local (if rc.local sets __local),
# otherwise return "sudo"
#
#############################################################################
sucmd() {
  local error_code
  if [[ "${__local}" = "T" ]]
  then
    sucmd=
  else
    sucmd="sudo"
  fi
  echo -n ${blue}
  ${sucmd} $*
  error_code=$?
  echo -n ${reset}
  return ${error_code}
}

#############################################################################
#
# Connect to the specified network if it's available
#
# Arguments:
#  $1 - Access point
#  $2 - Passphrase (If blank, use contents of /root/.connect_passphrase)
#  $3 - Connection type {unprotected|wep|wpa} Default: wpa
#      Note: Only passphrase WEP and WPA encription is supported
#            by this script
#
#############################################################################
connect_to() {
  local we type pass i
# Get the list of available access points
  we=$(cnetworkmanager --we)
  if [ -z "${we}" ]
  then
    echo "${red}No wireless devices are available on this system.${reset}"
    return
  fi
  [ -z "${pass}" ] && pass=$(cat /root/.connect_passphrase)
  type=$3
  [ -z "${type}" ] && type=wpa
  case ${type,*} in
    unprotected)pass="";;
    wep) type=wep-pass;;
    *) type=wpa-pass;;
  esac
  echo -n "${blue}Connecting to ${1}"
  cnetworkmanager -C ${1} --${type} ${pass} &>/dev/null &
  pid=$!
  if [ $? -ne 0 ]
  then
    echo " ${red}Unable to connect to ${1}.${reset}"
  else
    for ((i=1; i<6; i=++i))
    do
      sleep 1
      echo -n " ."
    done
    echo
    echo "${green}Connected to ${1}${reset}"
#    sleep 5
  fi
  disown ${pid}
}
[...]



All times are GMT -5. The time now is 01:23 PM.