1

I am trying to automate the healthcheck process of a router by running a centralised script from a linux server. I am so far able to access the router server without password prompt using ssh and rsa key pairs, and send in some commands from the server.

For example, I could run this automation script (to overwrite a file):

ssh -t -t  admin@$server << EOF
del output.txt
y
show port | save output.txt
exit
EOF

However, when I send a command "start shell" to initialise the bash shell in the router, i was not able to run a script saved in the router automatically. Instead, i have to type in the command to run the script manually, that is ./script.sh.

ssh -t -t  admin@$server << EOF
del output.txt
y
start shell <<EOF
./script.sh  # <-------------This doesn't get received by bash
EOF

I'm not sure what i can do. If there are any other ways i can run that script and receive the output without manually typing in the command, with or without using ssh or rsa key pairs, im all ears.

2
  • I really think you should avoid such scripting. SNMP is really good for that, it will be far easier to use and far more efficient.
    – Golgot
    Commented Jul 30, 2018 at 9:12
  • Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you can provide and accept your own answer.
    – Ron Maupin
    Commented Dec 25, 2018 at 8:51

2 Answers 2

0

This occurs generally when a script wait till next keyword like last character "#" to pass next command and finish running command result. In this case till start shell last character "#" after that last character is "$". Therefore, need to change in script for waiting command will see $ instead of #.

For example, using VB script.

Screen.Send "del output.txt" & Chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "y" & Chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send "start shell" & Chr(13)
crt.Screen.WaitForString "$"
crt.Screen.Send "./script.sh" & Chr(13)
crt.Screen.WaitForString "$"
4
  • Hi, I understand that this is in VB script, but is there a way to do it in shell script? I will be running the shell script in a shell terminal to access the router. May i know what I can search to look for this?
    – tthhss
    Commented Jul 4, 2018 at 8:21
  • cmd2 & cmd3 & cmd4 & wait $ next cmd // shell command. (See this link unix.stackexchange.com/questions/63648/…) Commented Jul 4, 2018 at 8:37
  • 1
    hmm, it still gets stuck at "start shell" in this scipt i wrote: ssh -t -t adminolp@$server << EOF enable start shell wait $ ./script.sh exit EOF
    – tthhss
    Commented Jul 4, 2018 at 8:49
  • Wait $ be a part of shell kernal instructions script; not command list. Like cmd(1) wait $ cmd (2) . Plz also check ./script path. May be it's path or directory different Commented Jul 4, 2018 at 10:46
2

I'm not sure what i can do. If there are any other ways i can run that script and receive the I/O without manually typing in the command, with or without using ssh or rsa key pairs, im all ears.

Do not attempt to drive interactive pty sessions with bash shell... it's like trying to turn a screw with the pointy-end of a hammer claw.... no worky. Use the Expect language, or Python / Ruby / Perl.

I wrote a presentation on one way to drive automated router sessions in python (with pexpect). This is only one of several techniques you could use. This presentation also includes a simple Expect script if you wanted to go that route.

If you want to use Windows, one popular option is scripting in SecureCRT

2
  • Is there any ways i can do this without using a installed language such as python or expect? Because the router is not mine and the admins do not advise that
    – tthhss
    Commented Jun 28, 2018 at 10:43
  • Would you rather write scripts in C?? Commented Jun 28, 2018 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.