Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Turn off text output to screen 3

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi all

I thought this would be easy to find but its proving harder than I thought.

I'm running a bash shell script that calls and expect script, problem is, when it calls the script it displays the output to the screen which I don't want. Its a login sequence of sorts and would like it to appear transparent.

I've looked along the lines of stty echo / -echo .. tput (seems to only work on echo display though)

Any ideas?

Thanks... T
 
You can redirect what you don't want to /dev/null.

If the login piece is doing something like:
ssh somebox -l myuser "/this/important/cmd"

you can change it to:
ssh somebox -l myuser "/this/important/cmd" > /dev/null

if you need both std out and std err redirected you'd do something like:
ssh somebox -l myuser "/this/important/cmd" > /dev/null 2>&1

If there are many lines to redirect it maybe easier to do this:

exec 1 > /dev/null
exec 2 > /my/error.log
ssh somebox -l myuser "/this/important/cmd1"
ssh somebox2 -l myuser "/this/important/cmd2"
ssh somebox3 -l myuser "/this/important/cmd3"
exec 1<&1
exec 2<&2
 
Without the spaces between 1 and > (likewise for 2 and >). exec >/dev/null 2>&1 would achieve the same.

Annihilannic.
 
Thanks for the replies .. I'll give those commands a go and let you know how things work out.

Cheers... T
 
Ok, tried that ... those commands are not what I'm after. (Redirect to null leaves me at null if you know what I mean :) )

I might have not explained it properly.

The bash script gets info, like username, password, passphrase etc, it then exports that data to be used by the expect script that is called in the script.

So ideally, only parts of it needs to be transparent. So if the script runs as it is now, it throws out the following:
Code:
SSH file found ... 
 
Your Username is: user123

 
 Standby getting password for DEVICE-RTR 
 
spawn ssh DEVICE-RTR -p 22 -K /home/users/user123/.ssh2/id_dsa_2048_a
Passphrase for key "/home/users/user123/.ssh2/id_dsa_2048_a":
Authentication successful.
sh: /usr/X11R6/bin/xauth: not found
Login  : admin                                                                                           
Passwd : 
DEVICE-RTR#

This is the type of output I'm trying to achieve:
Code:
SSH file found ...
 
Your Username is: user123
 
 Standby getting password for DEVICE-RTR
 
Login  : admin                                                                                           
Passwd :
DEVICE-RTR#

Perhaps it is something in the expect script where I need to do this... or both even?

Thanks... T
 
What is your actual code(s) ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV

The code is pretty much as follows, probably not as clean as what you guys push out each day though .. but I'm getting there .. I hope ;-)

The shell part:
Code:
## Test for file ##
if [ -s /home/users/$USER/.ssh2/id_dsa_2048_a ]
 then
  echo "SSH file found ... "
 else
  echo "SSH file not found... please create manually"
  exit 1
fi

export EHOST=$1
echo " "
echo "Your Username is: $USER "
export PUser=$USER
echo ""
stty -echo
trap "stty echo; exit 1" 2

if [ -s /home/users/$USER/.ssh2/.passphrase.pwd ]
 then
  SPASS=`cat /home/users/$USER/.ssh2/.passphrase.pwd`
 else
  echo -n "Enter your passhrase for ssh key /home/users/$USER/.ssh2/id_dsa_2048_a: "; read -r SPASS
  echo ""
  echo -n "Would you like your passphrase stored to your key directory? (y/n) "; read -r YN
   if [ "$YN" = "y" ]
    then
     echo "$SPASS" > /home/users/$USER/.ssh2/.passphrase.pwd
     echo "Password saved..."
   else
    echo "Password not stored ... "
   fi
fi


export sshpass=$SPASS
echo " "
stty echo
### Get the admin password
echo " Standby getting password for $1 "
echo " "

PASS=`passwordlookup.sh $1 | head -1 | awk '{print $2}' `
export adpwd=$PASS
export admin=admin

 $TOOLPATH/passphrase.exp

fi

# ---- end of shell script ----

The expect script: passphrase.exp

Code:
#!/usr/local/bin/expect -f

set timeout 5

set host $env(EHOST)
if {[string match "" $host] == 1} {
   puts "Error.. no host entry found.\n"
   exit 1
}

set user $env(PUser)
if {[string match "" $user] == 1} {
   puts "Error.. no userid entry found.\n"
   exit 1
}
set spass $env(sshpass)
if {[string match "" $spass] == 1} {
   puts "Error.. no ssh password entry found.\n"
   exit 1
}
set adminuser $env(admin)
if {[string match "" $adminuser] == 1} {
   puts "Error.. no admin username entry found.\n"
   exit 1
}
set adminpass $env(adpwd)
if {[string match "" $adminpass] == 1} {
   puts "Error.. no enable entry found.\n"
   exit 1
}

spawn ssh $host -p 22 -K /home/users/$user/.ssh2/id_dsa_2048_a

expect -re ":"
send "$spass\r"
expect -re "Login  :"
send "$adminuser\r"
expect "Passwd :  "
send "$adminpass\r"
interact

Thanks... T
 
spawn -noecho suppresses the display of the command-line executed by expect.

Reading the man page I can't figure out how to make expect discard some of the output from the spawned process (i.e. the passphrase prompt) and then display the rest, which I find a little surprising.

Do you really, really need to use a passphrase? I haven't encountered a site yet that uses them, and they're especially awkward for any automated logins as you've discovered...

Annihilannic.
 
Thanks for that Annihilannic ... that does help a bit at least.

Unfortunately these devices have to use a key by looks of it. They're a type of optical converter ... if ssh direct to them they complain of not authentication methods available. I could look more into the documentation on them, but I suspect it would take me longer to figure that out ;-)

Cheers... T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top