Operating System - HP-UX
1845947 Members
2807 Online
110250 Solutions
New Discussion

How to write shell script for interact with binary application(tty problem)?

 
SOLVED
Go to solution
gto481
Occasional Advisor

How to write shell script for interact with binary application(tty problem)?

I would like to write a shell script to interact with binary application reside on unix ssytem. But when I run my script, I got "Not a tty" error message. I have the same problem with this thread http://forums11.itrc.hp.com/service/forums/questionanswer.do?admit=109447626+1239893865321+28353475&threadId=134688 . They suggested to use expect to interact with binary application. But I can not use expect because I don't have permission to install it into unix machine due to security issue. Could you guy have another method or solution for this issue?
19 REPLIES 19
Laurent Menase
Honored Contributor
Solution

Re: How to write shell script for interact with binary application(tty problem)?

What is your application? Does it open /dev/tty and get its input from that device?


Else the more easy way to interact with a program is to use coprocess

example:

#!/usr/bin/ksh

telnet localhost |&

exec 3>&p 4<&p

sleep 1
echo laurent >&3
sleep 1
echo mypasswd >&3
echo echo debut>&3

while read a
do
if [ "$a" = "begin" ]
then
echo ls >&3
echo exit >&3
cat &
break
fi
done
done <&4

but if your application is using /dev/tty
- like more- , then you can't do that, but you must use
a telnet localhost to have a tty, then being able to echo and read from it.

#!/usr/bin/ksh

telnet localhost |&

exec 3>&p 4<&p

sleep 1
echo laurent >&3
sleep 1
echo mypasswd >&3
echo echo debut>&3

while read a
do
if [ "$a" = "begin" ]
then
echo more /var/adm/syslog/syslog.log >&3
cat &
sleep 2

echo >&3
echo >&3
echo >&3
break
fi
done
done <&4


Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

>when I run my script, I got "Not a tty" error message.

Where are you getting it? From /etc/profile or ~/.profile? If so, you need to fix them.
Hakki Aydin Ucar
Honored Contributor

Re: How to write shell script for interact with binary application(tty problem)?

if you connect to another server and interact with it, shell is not good;
But yes, you can use expect if you have it installed in your environment. Actually Expect somehow a component or extension of TCL language;

http://en.wikipedia.org/wiki/Expect

you can use Expect it is very suitable for interact and most cases it is similar to TCL and user friendly.
http://en.wikipedia.org/wiki/Tcl
gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

Laurent, This application is from vendor so I don't know it open /dev/tty or not because I believe it has internal algorithm to check whether user run it via shell script or not.
Laurent Menase
Honored Contributor

Re: How to write shell script for interact with binary application(tty problem)?

you could try a tusc -v -f -p of your script, and you will be able to find out where that message comes from

are you starting your script from a console, a telnet session? a ssh session a rlogin session?

or a ssh remote command remsh remote command?


else if the application need to run in a pty with a pty as stdin/stdout/stderr, the more easy to do it is using telnet coprocess

because you can pipe to/from telnet process and it will be transmitted to your application through a pty.

so
#!/usr/bin/ksh
telnet localhost |&

exec 3>&p 4<&p

sleep 1
echo myuser >&3
sleep 1
echo mypass >&3
sleep 2
echo mycmd >&3

=======
then when you read from 4 ( read -p 4 a )
and and pass answers using echo "answers" >&3

gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

Laurent, How can I write it in loop for my script as below? Because I want to terminate many users from the list.

for z in `cat terminate_list.txt`
do
VENDER_tool << EOT
logoff user=$z
quit
EOT
done
Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

>How can I write it in loop for my script as below?

What happens when you use your script? You get that "Not a tty" every time through the loop?

Can your VENDER_tool take multiple lines with:
logoff user=User1
logoff user=User2

If so, you should at least batch them up so you only invoke it once:
(
for z in $(< terminate_list.txt); do
echo "logoff user=$z"
done
echo quit
) > tool_input
gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

Dennis, Did you mean to write shell script like this? I've tried to execute it but still no luck. I got no error message but users still not logoff. Are there anything wrong with script below?

#!/usr/bin/ksh

telnet localhost |&

exec 3>&p 4<&p

sleep 1
echo username >&3
sleep 1
echo password >&3
sleep 2
echo >&3
sleep 1
echo >&3
sleep 1
echo >&3

while read a
do
(
for z in $(< terminate_list.txt); do
echo "logoff user=$z"
done
echo quit
) > VENDER_tool
cat &
sleep 2

echo >&3
echo >&3
echo >&3
break
done <&4
Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

>Did you mean to write shell script like this?

Not exactly. ;-)

Before your while:

# here you should read all of the input from your login. This decades old trick may work:

echo "echo EOF1" >&3
while read A; do
if [ "$A" = EOF1 ]; then break; fi
echo "$A"
done <&4

echo VENDER_tool >&3

Replace your while:

(
for z in $(< terminate_list.txt); do
echo "logoff user=$z"
done
echo quit
) >&3

# I'm assuming the buffers are large enough so you don't have to read each reply?

# Here you should read all of those replies. except you don't have an idea when to quit.

echo "echo EOF2" >&3
while read A; do
if [ "$A" = EOF2 ]; then break; fi
echo "$A"
done <&4

# now logoff
echo "exit" >&3

# terminate telnet (You'll need control V to enter that control char.)
echo "^]" > &3
echo "quit" >&3
gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

Dennis, Thank you for your help but I still having a problem when I enter the following value before my "while" as your comment.

echo "echo EOF1" >&3
while read A; do
if [ "$A" = EOF1 ]; then break; fi
echo "$A"
done <&4

It struck at EOF1 and not going to run into next step.

> echo EOF1
EOF1
Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

>when I enter the following value before my "while" as your comment.

(I tossed your while out.)

echo "echo EOF1" >&3
while read A; do
if [ "$A" = EOF1 ]; then break; fi
echo "LOOP1: $A"
done <&4

I just added "LOOP1:" above so you know which is which.

>It's struck at EOF1 and not going to run into next step.

Hmm. What stuff is echoed there? The intention is that the "remote" shell is going to see that echo and echo EOF1. Then the script will break out of the loop.
gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

It works. Thank you Dennis.
Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

>It works.

What was it you fixed that made it work?
gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

I changed if to case as below.

echo "echo EOT2" >&3
while read a
do
case $a in
*EOT2*) break ;;
esac
echo "$a"
done <&4
Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

> *EOT2*) break ;;

I was wondering if there were extra stuff on the lines. If you actually echo $a, you might find out what's extra. The prompt?
gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

I got >EOT2 after echo "echo EOT2" >&3 and script stuck on > prompt. I'm not sure why script did terminate after got EOT2. I think it would be some space or another char that I didn't see before or after EOT2. So I use wildcard for fix this problem. Another thing is after script terminated, I saw telnet localhost process still exist. How can I terminate it after script terminated? Below is my final script.

#!/bin/ksh

telnet localhost |&

exec 3>&p 4<&p

sleep 1
echo myuser >&3
sleep 1
echo mypass >&3
sleep 3
echo >&3
sleep 1
echo >&3
sleep 1
echo >&3
sleep 1

echo VENDER_tool >&3
sleep 1
(
for z in $(< terminate_list.txt); do
echo "logoff user=$z"
done
sleep 2
echo quit
) >&3

cat &
sleep 2

echo >&3
echo >&3
echo >&3

echo "echo EOT2" >&3
while read a
do
case $a in
*EOT2*) break ;;
esac
echo "$a"
done <&4

echo "exit" >&3

It seems after script terminated by got EOT2 and break. It will not process on echo "exit" >&3
Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

>I saw telnet localhost process still exist. How can I terminate it after script terminated?

You didn't put all of this at the end of your script:
# now logoff
echo "exit" >&3

# terminate telnet (You'll need control V to enter that control char.)
echo "^]" > &3
echo "quit" >&3

>It will not process on echo "exit" >&3

Do you ever get to this echo?
What happened to my EOF1 loop before you start VENDER_tool?
gto481
Occasional Advisor

Re: How to write shell script for interact with binary application(tty problem)?

Sorry Dennis for my late reply. I modified my script as your comment as below and still I never got "echo exit" on my screen after run this script. The script terminated after it got "echo EOT2" thus telnet localhost process still not terminate. What should I do?

#!/bin/ksh

telnet localhost |&

exec 3>&p 4<&p

sleep 1
echo myuser >&3
sleep 1
echo mypass >&3
sleep 3
echo >&3
sleep 1
echo >&3
sleep 1
echo >&3
sleep 1

echo "echo EOF1" >&3
while read a
do
case $a in
*EOF1*) break ;;
esac
echo "$a"
done <&4

echo VENDER_tool >&3
sleep 1
(
for z in $(< terminate_list.txt); do
echo "logoff user=$z"
done
sleep 2
echo quit
) >&3

cat &
sleep 2

echo >&3
echo >&3
echo >&3

echo "echo EOT2" >&3
while read a
do
case $a in
*EOT2*) break ;;
esac
echo "$a"
done <&4

echo "exit" >&3
echo "^]"&3
echo "quit" >&3
Dennis Handly
Acclaimed Contributor

Re: How to write shell script for interact with binary application(tty problem)?

>still I never got "echo exit" on my screen after run this script. The script terminated after it got "echo EOT2" thus telnet localhost process still not terminate. What should I do?

I was never sure what this did?
cat &

It seems like it hangs waiting for input?

Some typos here:
echo "exit" >&3 # this should exit the remote shell and not be seen
# Need to use control-] on the next line:
echo "^]" >&3 # this should escape from telnet
echo "quit" >&3 # this should quit telnet

I'm assuming we don't have to read from 4 here and the buffer is large enough?

You might want to remove the VENDER_tool loop to make sure the rest work.