Simple command script:

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
LVL 4
tflaiAsked:
Who is Participating?
 
chris_calabreseConnect With a Mentor Commented:
Just replace 'telnet' with 'nc'.
0
 
interiotCommented:
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
 
tflaiAuthor Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
interiotCommented:
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
 
tflaiAuthor Commented:
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
 
tflaiAuthor Commented:
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
 
tflaiAuthor Commented:
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
 
interiotCommented:
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
 
tflaiAuthor Commented:
Thanks.  It could be.  I'll dig into it some more...
0
 
tflaiAuthor Commented:
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
 
ahoffmannCommented:
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
 
ahoffmannCommented:
oops, missed a semicolon, sorry

cat $1 | awk '{ printf("sleep 1;echo '"'"'%s;'"'"'", $0) } END { print "sleep 5" }' | csh |
                     ./my_command_line_parser
0
 
chris_calabreseCommented:
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
 
interiotCommented:
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
 
tflaiAuthor Commented:
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
 
chris_calabreseCommented:
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
 
tflaiAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.