LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Replace systemd tty1 getty with telnet session (https://www.linuxquestions.org/questions/linux-general-1/replace-systemd-tty1-getty-with-telnet-session-4175609755/)

RandyTech 07-13-2017 06:17 AM

Replace systemd tty1 getty with telnet session
 
I know it can be done but cannot figure out how and my searches lead only to mass confusion.

I have a Raspberry Debian/Jessie installation. I'm setup with auto-login with GUI disabled so the completed boot leaves me sitting on a command prompt in the tty1 screen. Instead, I want the Raspberry to boot straight into a telnet session connecting to a separate local host computer at a static address, in essence, acting as a dumb terminal into that separate server. (PLEASE!!! No lectures about telnet -- I know already.)

I've researched systemd angles, .bashrc angles, .bash_profile angles, rc.local angles, crontab... I do not necessarily need this to be a systemd solution but I think that would be the cleanest solution. Not opposed to alternative boot script solution either if it can give me an automated telnet connection and login prompt on that separate local server. Ideally the solution would respawn to a fresh telnet login prompt when the user disconnects or telnet login times out. I think I can figure that out if I can just get the initial auto telnet thing working.

MensaWater 07-13-2017 09:01 AM

Prior to systemd in SysVInit the way to condition specific ports was with /etc/inittab.

Looking for that led to this link post that seems to have ideas I would try were I doing what you are asking. I haven't done any of this myself on systemd.

It's been awhile since I mucked with even inittab but my recollection was that you'd set use a getty definition (which once upon a time were in /etc/gettydefs) and that would simply prompt for login. What you did to login depended on what daemons you were running (e.g. sshd and/or telnetd, sftpd, ftpd etc...) rather than being part of the gettydef itself. If you're already getting login prompts you may wish to focus on starting telnetd.

RandyTech 07-15-2017 12:14 AM

Thanks for taking a stab at it MensaWater, but I didn't find any joy there. He starts out talking about a systemd equivalent on the inittab entry for tty1 and apparently ends up running some background process instead. I need to replace the tty1 login prompt with an active telnet session. Systemd :banghead:

RandyTech 07-15-2017 06:06 AM

Got it! :)
Not a "systemd" solution per say, but I found this reference that led me to an acceptable solution. This was the first reference I found that suggested editing the /etc/profile file.
http://www.opentechguides.com/how-to...uto-start.html

The following is my basic outline. More code will be need to be added to loop around to restart the telnet session when the user logs out or telnet prompt times out, but I'm not going to clutter the basic outline with those details. This is tested and working in both the minimal and full Raspian (Recommended) Debian/Jessie install.


To begin, get your Pi configured to boot into CLI mode with auto-login.
Next, install the telnet package (not included in defaults OS installation):
Code:

sudo apt-get -y install telnet
Now begins the magic trick I was looking for where .bashrc, .bash_profile, rc.local and crontab all failed me:
Code:

sudo vi /etc/profile
Append the bottom of the file with this line and be sure to include the preceding "dot":
Code:

. /opt/dotelnet
That line will invoke your script after the Raspberry reaches the system command prompt. I have no idea what that "dot" is there for but it was in the reference sample so I just followed suite, and it works. (Don't fix it if it ain't broke)


Next you create the script you want to invoke after the auto-login:
Code:

sudo vi /opt/dotelnet
Populate the file with the following code:
Code:

#!/bin/bash
portName=`who am i | awk '{print $2}'`
echo "you are $portName"
case $portName in
  tty1) portName="ok" ;;
  tty2) portName="ok" ;;
  tty3) portName="ok" ;;
  tty4) portName="ok" ;;
  tty5) portName="ok" ;;
  tty6) portName="ok" ;;
esac
if [ "$portName" = "ok" ] ; then
  echo "Entering terminal mode..."
  sleep 1
  /usr/bin/telnet 192.168.1.138
fi

Save it and make the script executable:
Code:

sudo chmod 755 /opt/dotelnet
One last step to see the magic happen:
Code:

sudo reboot
Obvious to some more than others, you might want to change the name and/or location of the "/opt/dotelnet" file. Up to you. And the 192.168.1.138 address will probably need to be adjusted to point to your local host machine.

It should also be noted here, the above solution is invoked not only on the tty1 auto-login session but also works when you login on any of the VT screens, and this should work regardless what user name you use. I am using the default "pi" user.

In case you are asking "why did he use the case esac structure to test for the console screens", it turned out this simpler more elegant code worked perfectly in the full Raspian installation but failed in the minimal Raspian installation. (go figure :rolleyes: )
Code:

#!/bin/bash
portName=`who am i | awk '{print $2}'`
echo "you are $portName"
if [ "$portName" = "tty[1-6]" ] ; then
  echo "Entering terminal mode..."
  sleep 1
  /usr/bin/telnet 192.168.1.138
fi

Would be interesting to know why that is. Whatever the case, the script is never invoked when you ssh into the Raspberry.


All times are GMT -5. The time now is 11:57 AM.