LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2013, 07:25 PM   #16
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783

Quote:
Originally Posted by Paul R View Post
Well, I logged in as root (using sudo), ran strace, and it no longer gave me the previous error... However, now I get a different error...

# strace ./tst 2> tst.out2
# cat tst.out2
ERROR: tracer already exists
I'm lost with that one...

shame as
write(1, "1234\n"
is what you need from strace
 
Old 07-15-2013, 07:28 PM   #17
Paul R
LQ Newbie
 
Registered: Jul 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
Cool output from strace

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.

Quote:
Originally Posted by Firerat View Post
strace, on tst ( with shabang )

Code:
open("./tst", O_RDONLY)                 = 3
fcntl(3, F_DUPFD, 10)                   = 10
close(3)                                = 0
fcntl(10, F_SETFD, FD_CLOEXEC)          = 0
rt_sigaction(SIGINT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGINT, {0x40f270, ~[RTMIN RT_1], SA_RESTORER, 0x7f25d83b6260}, NULL, 8) = 0
rt_sigaction(SIGQUIT, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_DFL, ~[RTMIN RT_1], SA_RESTORER, 0x7f25d83b6260}, NULL, 8) = 0
rt_sigaction(SIGTERM, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGTERM, {SIG_DFL, ~[RTMIN RT_1], SA_RESTORER, 0x7f25d83b6260}, NULL, 8) = 0
read(10, "#!/bin/sh\necho 1234 > /dev/tty\n\n", 8192) = 32
open("/dev/tty", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
fcntl(1, F_DUPFD, 10)                   = 11
close(1)                                = 0
fcntl(11, F_SETFD, FD_CLOEXEC)          = 0
dup2(3, 1)                              = 1
close(3)                                = 0
write(1, "1234\n", 51234
)                   = 5
dup2(11, 1)                             = 1
close(11)                               = 0
read(10, "", 8192)                      = 0
exit_group(0)                           = ?
 
Old 07-15-2013, 07:53 PM   #18
Paul R
LQ Newbie
 
Registered: Jul 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
Well... I got truss to work, but the "write" line is not as elegant... it puts a bunch of stupid spaces in...

fcntl(3, F_DUPFD, 0x00000001) = 1
close(3) = 0
write(1, " 1 2 3 4 5 6 7 8 9\n", 12) = 12
fcntl(1, F_GETFD, 0x00000000) = 0
close(1) = 0
fcntl(10, F_DUPFD, 0x00000001) = 1


But at least I have something to work with now. --Paul.

Quote:
Originally Posted by Paul R View Post
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.
 
Old 07-15-2013, 07:59 PM   #19
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by Paul R View Post
Well... I got truss to work, but the "write" line is not as elegant... it puts a bunch of stupid spaces in...
Code:
fcntl(3, F_DUPFD, 0x00000001)                   = 1
close(3)                                        = 0
write(1, " 1 2 3 4   5 6 7 8   9\n", 12)        = 12
fcntl(1, F_GETFD, 0x00000000)                   = 0
close(1)                                        = 0
fcntl(10, F_DUPFD, 0x00000001)                  = 1
But at least I have something to work with now. --Paul.
I was just about to come back with truss, ( after startpaging the error you were getting )

You just need to get out the ol' awk now
 
Old 07-16-2013, 02:18 AM   #20
Paul R
LQ Newbie
 
Registered: Jul 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
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 View Post
I was just about to come back with truss, ( after startpaging the error you were getting )

You just need to get out the ol' awk now
 
Old 07-16-2013, 02:38 AM   #21
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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:

Code:
script -q -c "echo 'Hi'>/dev/tty" ttyoutput.txt >/dev/null
 
Old 07-16-2013, 03:10 AM   #22
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
But script captures terminal sessions and the problem binary is not running a terminal session.

AFAIK the OP has been completely consistent in wanting to find a way to capture what is being written to the virtual device /dev/tty.

The screen command should be good for that. I have never used screen so cannot advise how.

Last edited by catkin; 07-16-2013 at 03:47 AM. Reason: dyslexia rules KO
 
1 members found this post helpful.
Old 07-16-2013, 03:35 AM   #23
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by Paul R View Post
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.
[ c o d e ]
Preserves
indents and the like
[ / c o d e ]
http://www.linuxquestions.org/questi....php?do=bbcode
Code:
Preserves
    indents and the like
Anyway, they spaces are not a problem,,

This is crude, but should work

Code:
awk -F\" '/write/{gsub(/  /,"_");gsub(/ /,"");gsub(/_/," ");print $2}' Input.file
replacing all double spaces with _
deleting all single spaces
then replacing all _ with space.

Probably better ways to do it, but it works well for now

BUT, I don't see the numbers you want...

http://www.idevelopment.info/data/Un...nSolaris.shtml
Code:
 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
 
Old 07-16-2013, 03:51 AM   #24
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> But 'script' captures terminal sessions and the problem binary is not running a terminal session.

Methinks if it weren't running in a terminal session, writing on '/dev/tty' wouldn't work.
 
Old 07-16-2013, 07:43 AM   #25
bonnydeal
Member
 
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
You can use 'expect' for this.
It may already be on your system.

If not get it here:
http://expect.sourceforge.net
 
Old 07-16-2013, 10:35 AM   #26
Paul R
LQ Newbie
 
Registered: Jul 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
Duh... I had been using the -w!all option instead of -wall. Thanks! :-)

Quote:
Originally Posted by Firerat View Post
BUT, I don't see the numbers you want...

http://www.idevelopment.info/data/Un...nSolaris.shtml
Code:
 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
 
Old 07-16-2013, 10:41 AM   #27
Paul R
LQ Newbie
 
Registered: Jul 2013
Posts: 13

Original Poster
Rep: Reputation: Disabled
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 View Post
You can use 'expect' for this.
It may already be on your system.

If not get it here:
http://expect.sourceforge.net
 
Old 07-16-2013, 12:23 PM   #28
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by Paul R View Post
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.
 
Old 07-16-2013, 01:21 PM   #29
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by Paul R View Post
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
and your cron ( every 15min )
Code:
*/15 * * * * /path/to/LicenseCheckLogger.sh

Now, if truss is what works

Code:
#!/bin/sh
# LicenseCheckLogger.sh

truss -wall /path/to/TheBin 2>&1 | \
awk -F\" 'BEGIN{"date +%Y%m%d-%R"|getline Date};/write/&&/" 6/{gsub(/  /,"_");gsub(/ /,"");gsub(/_/," ");print Date" "$2}' >> /path/to/logfile
I added a little to the awk, each line will get the date/time ( awk was started )
also limiting to 'needed' info ( starting " 6 )
 
2 members found this post helpful.
Old 07-17-2013, 07:18 AM   #30
bonnydeal
Member
 
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
Quote:
Originally Posted by Paul R View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird question | Grabbing tty output from remote machine the_gripmaster Linux - General 6 04-03-2011 08:02 PM
lpr and /dev/usb/lp0 ---- simple question v333k Linux - Software 19 11-01-2009 12:23 PM
wxPython GUI input/output simple question Hal Programming 0 04-14-2005 03:17 PM
please help, simple question about redirect output kool Linux - General 4 03-18-2002 09:34 AM
Capturing tty data to a file svaujin Linux - Newbie 2 04-03-2001 09:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration