Simple question about capturing output to /dev/tty
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, that's pretty cool output from strace. From there, it would be a simply matter of parsing out the "write" lines, which contain the data that is written to /dev/tty. It's a great idea. I only wish strace worked on my system! I can try to touch base with my UNIX admin to see why it's not working, but... it's always real hard to get ahold of him. Any OTHER suggestions??? --Paul.
But at least I have something to work with now. --Paul.
Quote:
Originally Posted by Paul R
Well, that's pretty cool output from strace. From there, it would be a simply matter of parsing out the "write" lines, which contain the data that is written to /dev/tty. It's a great idea. I only wish strace worked on my system! I can try to touch base with my UNIX admin to see why it's not working, but... it's always real hard to get ahold of him. Any OTHER suggestions??? --Paul.
Well... I spoke too soon, because I tested too few characters with truss. Here is what the actual output looks like from the license tool:
License activity on ihgp:
Feature in use total occupancy
----------------- ------ ----- ---------
6000 24 97 25%
6001 0 3 0%
6005 0 1 0%
And here is what the truss output looks like:
11486: write(1, "\n L i c e n s e a c t".., 28) = 28
11486: write(1, " F e a t u r e ".., 45) = 45
11486: write(1, " - - - - - - - - - - - -".., 45) = 45
11486: write(1, " 6 0 0 0 ".., 43) = 43
11486: write(1, " 6 0 0 1 ".., 43) = 43
11486: write(1, " 6 0 0 5 ".., 43) = 43
As you can see, major truncation... to the point where the output is basically useless (there are lots of spaces in the output, but this forum software is scrunching them down to one space). I searched the man page and internet for a solution, but did not find any. Any ideas? --Paul.
Quote:
Originally Posted by Firerat
I was just about to come back with truss, ( after startpaging the error you were getting )
It is not easy to find out what the actual question is... the OP seems to keep changing his or her question constantly. If the (current) question is about redirecting a program's output from /dev/tty into file, that would be something like this:
Well... I spoke too soon, because I tested too few characters with truss. Here is what the actual output looks like from the license tool:
Code:
License activity on ihgp:
Feature in use total occupancy
----------------- ------ ----- ---------
6000 24 97 25%
6001 0 3 0%
6005 0 1 0%
And here is what the truss output looks like:
Code:
11486: write(1, "\n L i c e n s e a c t".., 28) = 28
11486: write(1, " F e a t u r e ".., 45) = 45
11486: write(1, " - - - - - - - - - - - -".., 45) = 45
11486: write(1, " 6 0 0 0 ".., 43) = 43
11486: write(1, " 6 0 0 1 ".., 43) = 43
11486: write(1, " 6 0 0 5 ".., 43) = 43
As you can see, major truncation... to the point where the output is basically useless (there are lots of spaces in the output, but this forum software is scrunching them down to one space). I searched the man page and internet for a solution, but did not find any. Any ideas? --Paul.
Using truss
Truss is used to trace the system/library calls (not user calls) and signals made/received by a new or existing process. It sends the output to stderr.
NOTE: Trussing a process throttles that process to your display speed. Use -wall and -rall sparingly.
Truss usage
truss -a -e -f -rall -wall -p
truss -a -e -f -rall -wall
-a Show arguments passed to the exec system calls
-e Show environment variables passed to the exec system calls
-f Show forked processes
(they will have a different pid: in column 1)
-rall Show all read data (default is 32 bytes)
-wall Show all written data (default is 32 bytes)
-p Hook to an existing process (must be owner or root)
<program> Specify a program to run
so it looks like if you add -wall to the truss you will get all the write, not just first 32 bytes
Using truss
Truss is used to trace the system/library calls (not user calls) and signals made/received by a new or existing process. It sends the output to stderr.
NOTE: Trussing a process throttles that process to your display speed. Use -wall and -rall sparingly.
Truss usage
truss -a -e -f -rall -wall -p
truss -a -e -f -rall -wall
-a Show arguments passed to the exec system calls
-e Show environment variables passed to the exec system calls
-f Show forked processes
(they will have a different pid: in column 1)
-rall Show all read data (default is 32 bytes)
-wall Show all written data (default is 32 bytes)
-p Hook to an existing process (must be owner or root)
<program> Specify a program to run
so it looks like if you add -wall to the truss you will get all the write, not just first 32 bytes
I have "expect" on my system, and read similar suggestions on the internet for others who had the same problem as me, but... I'm not sure I understand it, and have never seen any coding examples. So... If YOU were dealing with someone elses one-line script that wrote to /dev/tty (i.e. echo "1234" > /dev/tty), then how would YOU capture the "1234" output using expect? I'm open to any/all suggestions/solutions, because even though truss seems to work, it may NOT work in a cronjob (which I haven't tested yet). If need be, I can run a script from the UNIX prompt, and append "&" to run it in the background (indefinitely), but I would prefer running a cron job periodically instead. -Paul.
Quote:
Originally Posted by bonnydeal
You can use 'expect' for this.
It may already be on your system.
I have "expect" on my system, and read similar suggestions on the internet for others who had the same problem as me, but... I'm not sure I understand it, and have never seen any coding examples. So... If YOU were dealing with someone elses one-line script that wrote to /dev/tty (i.e. echo "1234" > /dev/tty), then how would YOU capture the "1234" output using expect? I'm open to any/all suggestions/solutions, because even though truss seems to work, it may NOT work in a cronjob (which I haven't tested yet). If need be, I can run a script from the UNIX prompt, and append "&" to run it in the background (indefinitely), but I would prefer running a cron job periodically instead. -Paul.
From what I've seen you still haven attempted the command substitution which I have tested and verified works. I have no idea how expect would be useful here, I use expect to enter passwords in ssh for loops and so on.
Here is an example:
Code:
#!/usr/bin/expect
set logfile [open /tmp/iplist]
set ipaddrs [read $logfile]
set timeout 45
foreach ip [split $ipaddrs \n] {
set pass "PASSWORDHERE"
spawn ssh root@$ip "yum install -y openldap-clients nss_ldap"
expect {
password: {send "$pass\r"; exp_continue}
}
}
That does an ssh loop and sends a password which installed nss_ldap in my environment. Now unless I'm missing something here, expect would be of no use to you for this situation.
I have "expect" on my system, and read similar suggestions on the internet for others who had the same problem as me, but... I'm not sure I understand it, and have never seen any coding examples. So... If YOU were dealing with someone elses one-line script that wrote to /dev/tty (i.e. echo "1234" > /dev/tty), then how would YOU capture the "1234" output using expect? I'm open to any/all suggestions/solutions, because even though truss seems to work, it may NOT work in a cronjob (which I haven't tested yet). If need be, I can run a script from the UNIX prompt, and append "&" to run it in the background (indefinitely), but I would prefer running a cron job periodically instead. -Paul.
I think NevemTeve's solution is the cleanest, it certainly seems to work with the "tst" script
the cronjob should simply be periodically executing a shell script, one which appends to a log.
Assuming NevemTeve's Script solution works
Code:
#!/bin/sh
# LicenseCheckLogger.sh
script -q -c /path/to/TheBin -a /path/to/logfile >/dev/null
I have "expect" on my system, and read similar suggestions on the internet for others who had the same problem as me, but... I'm not sure I understand it, and have never seen any coding examples. So... If YOU were dealing with someone elses one-line script that wrote to /dev/tty (i.e. echo "1234" > /dev/tty), then how would YOU capture the "1234" output using expect? I'm open to any/all suggestions/solutions, because even though truss seems to work, it may NOT work in a cronjob (which I haven't tested yet). If need be, I can run a script from the UNIX prompt, and append "&" to run it in the background (indefinitely), but I would prefer running a cron job periodically instead. -Paul.
I understand you have a script to write to tty (ttyout.sh)
Code:
#!/usr/bin/bash
#
echo 1234 > /dev/tty
Sample expect script to capture tty (ttycap.exp)
Code:
#!/usr/bin/expect -f
#spawn the command that outputs to tty here.
# This is just the echo 1234 in your example.
spawn ./ttyout.sh
expect {
eof {exit}
}
Invocation
Code:
MYNUMBER=$(./ttycap.exp | tail -1)
echo $MYNUMBER
Expect is the best way to do this, imho, because it is specifically designed to work with tty output.
You can do it with a few lines of expect then parse the output how you like.
In the example I just used tail to get the last line, you may want to do something else.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.