Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to run a couple remote commands on a box that will not give me a normal shell (it's a device with a proprietary shell).

I'm also running the command from a device with a limited set of resources so I can't add something like Expect.

I tried the following to no avail:

#!/bin/ksh
( sleep 2
  echo username
  sleep 5
  echo password
  sleep 5
  echo show whoison ) | telnet 192.168.1.10

It successfully enters the username and fails on entering the password. "\n" didn't seem to work either.


I've tried SSH before but the shell that the device is using wouldn't let me pass commands (I tried ssh user@host 'show whoison')

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

first. do not. it's not secure.

second - only if you have to...

line below - similar to yours - works for me - but probably it's a race condition and depends on how quickly your device expects credentials.

( echo someLogin;  echo somePass; echo reboot 0;  echo reboot 0 ; sleep 3; ) | telnet 192.168.0.141

you can as well use expect

share|improve this answer
    
I see double spaces but suspect there's something else. Did you have control characters in that command line? –  Chris W. Rea Dec 9 '09 at 19:57
    
@BasicallyMoney.com - no i did not had special chars –  pQd Dec 9 '09 at 19:58
    
Had to adjust the sleeps in mine, thanks pQd. –  Jason Dec 9 '09 at 20:16
    
@Jason - nevertheless it is ugly hack and with expect you can make it a bit better. –  pQd Dec 9 '09 at 20:28
add comment

For automatic logging in with telnet, you really want to use expect

NOTE: Really and truly, you don't want to use telnet either. There are SSH alternatives available for nearly every platform now, including Cisco routers.

SSH allows you

  1. Passwordless entry via public key authentication
  2. Easier execution of remote commands
  3. Passing of return values via scripts
  4. A secure environment that is encrypted end to end
  5. Agent authentication so that passwords can be used on the keys, but the keys are stored in memory when used once.

If you have the power to use SSH, use it. If not, use expect.

share|improve this answer
    
I tried SSH too but couldn't send the commands effectively into the shell that the device provided. –  Jason Dec 9 '09 at 19:56
1  
You might want to post another question regarding that issue than, to work at it from both angles. –  Kyle Brandt Dec 9 '09 at 19:59
add comment

I clicked through to your question to recommend expect, but then saw your limitation. Sorry, but telnet can't be used the way you want. Have you tried netcat or you can't install anything?

Also, telnet is only safe for trusted local-only traffic; otherwise use SSH.

share|improve this answer
add comment

Does using the -t option to ssh help at all with the problem of the commands not working when using ssh?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.