Changing the prompt in telnet

0

With some help from people on here, I was able to set a custom prompt in an ssh session (thanks!). Now I need to do the same in telnet, but I'm not sure of what syntax I could use for that.

Basically the telnet prompt is just a > character, I need to modify it to something I can more reliably detect in automation jobs. Hope this makes sense.

From inside telnet, trying to escape that command with a bang like !PS1=spam and !PS2=eggs did not change it.

wim@wim-acer:~$ ssh guest@192.168.1.124 -i ~/.ssh/guest_nopassphrase -t "export PS1='Sending a custom prompt \w \$ '; exec sh"
Sending a custom prompt ~ $ set
HOME='/var/tmp'
IFS='   
'
LOGNAME='guest'
PATH='/sbin:/usr/sbin:/bin:/usr/bin'
PPID='1128'
PS1='Sending a custom prompt \w $ '
PS2='> '
PS4='+ '
PWD=''
SHELL='/bin/sh'
TERM='xterm'
USER='guest'
Sending a custom prompt ~ $ telnet localhost <snip>

Entering character mode
Escape character is '^]'.

> !set
CONSOLE='/dev/ttyp0'
HOME='/var/tmp'
IFS='   
'
LOGNAME='root'
PATH='/sbin:/bin:/usr/sbin:/usr/bin'
PPID='546'
PREVLEVEL='N'
PS1='\w \$ '
PS2='> '
PS4='+ '
PWD='/var/tmp'
RESPAWN_COUNT='1'
RESPAWN_LAST='0'
RESPAWN_MAX='5'
RESPAWN_TIME='5'
ROOTDEV='/dev/sla1'
RUNLEVEL='5'
SHELL='/bin/false'
TERM='linux'
USER='root'
> 
> Connection closed by foreign host
Sending a custom prompt ~ $ Connection to 192.168.1.124 closed.
wim@wim-acer:~$ 

wim

Posted 2012-03-13T06:47:08.350

Reputation: 2 523

1Hmmm... Spam, lovely Spam, wonderful Spam! – Daniel Beck – 2012-03-13T08:22:45.363

Answers

1

The prompt is provided by the command shell, not by network protocols such as SSH or Telnet and their associated services and clients. It does not matter whether you are using telnet, ssh, a serial terminal or a local console.

You can set the prompt in many shells (e.g. bash) by setting environment variables such as PS1 and PS2.

Try

 > PS1="hello : "
 hello : 

UPDATE

Your questions says ...

  $ telnet localhost <snip>
  …
  > !set
  …
  SHELL='/bin/false'
  …
  > Connection closed by foreign host

/bin/false isn't a shell! It is the sort of entry that is used to disable telnet logins. You are not going to get anywhere with this. If you really want to use an insecure protocol like telnet instead of a secure protocol like ssh, you'll have to configure the telnet service to provide you with a proper shell.

RedGrittyBrick

Posted 2012-03-13T06:47:08.350

Reputation: 70 632

This works in sh, but in telnet I get PS1="hello : ": unknown command – wim – 2012-03-21T05:59:19.617

@wim: see updated answer. – RedGrittyBrick – 2012-03-21T11:22:26.607

thanks! the device is a set-top box and i don't have permissions to modify firmware, i'm just trying to automate certain jobs on hundreds of these boxes with pexpect and it would be easier if i could match the prompt reliably – wim – 2012-03-21T13:32:26.060

0

Like @RedGrittyBrick wrote, the fact that you're using telnet or SSH doesn't matter. What does matter is environment variable forwarding.

What you are, in fact, telling ssh to do is to connect to a TTY, then pass the command export PS1='Sending a custom prompt \w \$ '; exec sh to the default shell.

For telnet, look at environ (see the man page). It uses the telnet ENVIRON protocol to transfer environment variables across the connection, and I imagine you can add the appropriate commands to your ~/.telnetrc if you want to, or just export the appropriate environment variables on a per-session basis.

For a proper way of doing it in SSH, look at the ENVIRONMENT section of the ssh man page. Not sure I see an obvious way of doing it per instance, though.

a CVn

Posted 2012-03-13T06:47:08.350

Reputation: 26 553