Solved

Simple command script:

Posted on 2001-07-26
17
317 Views
Last Modified: 2013-12-27
Can anyone tell me what went wrong with the following simple command script (which should take a command
file and blast it to the program my_command_line_parser):

#!/bin/csh
cat $1 | awk '{ printf("sleep 1\necho %s\n", $0) } END { print "sleep 5" }' | csh | ./my_command_line_parser


I'm getting the following error messages:

main: cannot Open /dev/ttyp0 again
dup2 on ttyfd failed
stty: TIOCGETD: Operation not supported
0
Comment
Question by:tflai
  • 8
  • 4
  • 3
  • +1
17 Comments
 
LVL 3

Expert Comment

by:interiot
ID: 6324173
What's $1 in the script?  The lines of that are passed unquoted as arguments to echo.  If that contains shell characters (eg. backticks, parenthesis), the command could execute differently.

Also, does it exhibit the same behavior if you take ./my_command_line_parser off the end?
0
 
LVL 4

Author Comment

by:tflai
ID: 6324275
interiot,

The $1 is to take an argument as the command file.  So, what I want to do is to take a text file, supposely look like this "test_command1\ntest_command2\ntest_command3...", and pipe those commands thru that script into the program "my_command_line_parser", which would interpret test_command1, followed by test_command2, etc.
0
 
LVL 3

Expert Comment

by:interiot
ID: 6324419
I was just wondering what the typical contents of command files are, because the commands will be partially interpretted a second time as arguments to echo.  Doing this would prevent the extra interprettation, but might not fix the problem:

perl -ple 'sleep 1; END { sleep 5}' $1 | csh | ./my_command_line_parser

Also, could you post the contents of the command file?  The error messages could be coming from those commands, or from my_command_line_parser.
0
 
LVL 4

Author Comment

by:tflai
ID: 6324488
interiot,

You're right.  Those error messages seem to have came from the application.  Actually I'm trying to run this script on a network router thru telnet.  The same script works perfectly when I tried it to another UNIX box.  So, I guess the problem must be with the router's telnet server.  But I don't know what it is!?
0
 
LVL 4

Author Comment

by:tflai
ID: 6324489
interiot,

You're right.  Those error messages seem to have came from the application.  Actually I'm trying to run this script on a network router thru telnet.  The same script works perfectly when I tried it to another UNIX box.  So, I guess the problem must be with the router's telnet server.  But I don't know what it is!?
0
 
LVL 4

Author Comment

by:tflai
ID: 6324544
interiot,

The $1 is to take an argument as the command file.  So, what I want to do is to take a text file, supposely look like this "test_command1\ntest_command2\ntest_command3...", and pipe those commands thru that script into the program "my_command_line_parser", which would interpret test_command1, followed by test_command2, etc.
0
 
LVL 3

Expert Comment

by:interiot
ID: 6324907
Isn't csh running those commands, and my_command_line_parser is interpretting the output of test_command1, test_command2, etc...?

The problem could be with the environment (shell vars, config files, installation, etc...) on the router?
0
 
LVL 4

Author Comment

by:tflai
ID: 6324926
Thanks.  It could be.  I'll dig into it some more...
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Author Comment

by:tflai
ID: 6324966
interiot,

By the way, I get the following message when the script failed.  (It's funny though, the script DO WORK about 1 out of 10 times)...

stty: TIOCGETD: Connection closed by foreign host.
Operation not supported
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6326269
cat $1 | awk '{ printf("sleep 1;echo '"'"'%s\n'"'"'", $0) } END { print "sleep 5" }' | csh | ./my_command_line_parser


BTW, not shure what   |csh  should do in your example
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6326277
oops, missed a semicolon, sorry

cat $1 | awk '{ printf("sleep 1;echo '"'"'%s;'"'"'", $0) } END { print "sleep 5" }' | csh |
                     ./my_command_line_parser
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6326886
Hmm, I swear I posted to this already, but don't see it here.
Anyway, this whole approach looks too complicated to me.  Here's a simpler solution


#/usr/bin/ksh -
while read -r line
do  sleep 1
      print -r -- "$line
done < "$1" | ./my_command_line_parser
0
 
LVL 3

Expert Comment

by:interiot
ID: 6327108
The given perl alternative is the shortest of them all.  :)

But yeah, the csh in there looks suspicious because, as I said, it's actually running the commands, and giving the output of each of them to my_command_line_parser.  It sounded like you wanted your parser to read in the commands directly, so you may not want the |csh| in there.
0
 
LVL 4

Author Comment

by:tflai
ID: 6327237
It's sad to say that none of the approach worked too well.  Most often I get the following error message:

I tried all the approaches (awk, perl, ksh).  Must be something wrong with the router...

But the funny thing is that there is absolutely no problem if I manually telnet to the box.

=== cli.sh ===
#!/bin/csh
cat $1 | awk '{ printf("sleep 1; echo '"'"'%s;'"'"'", $0) } END { print "sleep 5
" }' | csh | telnet $2


#./cli.sh test.cli 192.168.163.71
Trying 192.168.163.71...
stty: TIOCGETD: Operation not supported
Connected to router1.com.
Escape character is '^]'.


XYZ ROUTER (1)

Connection closed by foreign host.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6327615
Ah, well there's your problem.  You can't use telnet this way because it needs stdin connected to a terminal in order to do certain operations.  Instead pick up a copy of netcat.  You can find it easily by searching for it on google or some such.
0
 
LVL 4

Author Comment

by:tflai
ID: 6327856
Okay, I've downloaded NETCAT and compiled it.  How do I create a script that use NETCAT to do the same thing?  (It's Friday, I'm too lazy to read the instruction...)
0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 100 total points
ID: 6327884
Just replace 'telnet' with 'nc'.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now