frisco
May 21st, 2004, 11:07
I'm looking for a cmd line program that will automatically type in the login and password for a telnet session, then return control to the tty. Any recommendations?

Reason why is b/c we have a few Avocent serial consoles that we currently telnet into (isolated network, accesible via ssh to one machine that only lives for this) manually, that is, we ssh to the first machine, then telnet in to the avocent/port that we want.

But my end goal is to have this machine run screen, with one screen for each serial connection, and in each screen is this program running that has already logged into the serial connection and is just waiting for someone to type something in. This gives the benefit of being able to log all the last X number of lines via screen (we also syslog to central servers but not all messages go that route) as well as providing a common method to access a console and a mechanism for all the admins to see what another admin is doing via console.

bmw
May 21st, 2004, 12:13
One word: expect

http://expect.nist.gov/

Expect is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc. Expect really makes this stuff trivial. Expect is also useful for testing these same applications. And by adding Tk, you can also wrap interactive applications in X11 GUIs.

You need to install TCL (aka Scriptics) if you haven't already.

frisco
May 21st, 2004, 12:21
I was thinking i'd have to go that route, or use perl's Net::Telnet, but was hoping to not install too much extra. Thanks for the reply.

bmw
May 21st, 2004, 12:33
It's not too much stuff actually. In FreeBSD build it with WITHOUT_X11=1 and it is quite minimal, ie not much bigger than the average shell (ie bash, not sh :-).

soup4you2
May 21st, 2004, 20:24
sorry.. i've often wondered about doing cross port applications.. meaning if i run a command in a script if value: name or something is stated to have a script return a command..

better explination

i run

$myscript.sh

and it say for example telnets to server foo at port 23 (telnet) and it's given a output of

username:

how to get my script to return something based on the specific username: output..

i've been learning python hopeing to learn someway of doing this.. but havent learned anything yet.. my overall goal is to have xinetd run some scripts for me tunneled though stunnel.. i can think of tons of uses but dont know how to interact between sockets yet..

bmw
May 21st, 2004, 20:35
Then, soup, I recommend that you play with expect. The basic paradigm is you create pairs of "expect this" / "send that" strings. So for automating telnet, your base script calls an expect script to express,

- if you see "password:" send "mysecret"
- if you see "bash$ " send "ls -l"

and so on. You can tell expect when to relinquish control to an interactive shell so a user can take over, or to exit to a containing script, etc.

|MiNi0n|
May 21st, 2004, 22:45
Remember good ol' ppp.conf files?:

# The login script shown says that you're expecting ``ogin:''. If you
# don't receive that, send a ``\n'' and expect ``ogin:'' again. When
# it's received, send ``ppp'', expect ``word:'' then send ``ppp''.
# You *MUST* customise this login script according to your local
# requirements.
#
pmdemand:
set phone 1234567
set login "ABORT NO\\sCARRIER TIMEOUT 5 ogin:--ogin: ppp word: ppp"


That's the stuff :) Anyway, I've had to resort to expect for a number of things. It works... it's a bit ugly and difficult to get used to at first, but it gets the job done.

elmore
May 21st, 2004, 22:57
I thought we agreed we wouldn't say anything about those remote expect scripts minion! :silly:

frisco
June 9th, 2004, 13:48
I created a user to run screen at boottime via cron, with one line like this for each avocent (using different screenrc files):
@reboot /usr/local/bin/screen -d -m -c /home/conman/.screenrc-207 > /home/conman/207 2>&1


The .screenrc files look like the following:

startup_message off
defscrollback 10000
log on
detach

screen -t orchard 2
exec /home/conman/bin/connect console-207 3002 Admin password

screen -t green 5
exec /home/conman/bin/connect console-207 3005 Admin password

screen -t fuji 6
exec /home/conman/bin/connect console-207 3006 Admin password

screen -t seed 7
exec /home/conman/bin/connect console-207 3007 Admin password

This labels the screen window with the hostname, and connects to the port of the same number as the screen number (screen 5 == port 3005).


The /home/conman/bin/connect script is rather short:

#!/usr/local/bin/expect

set hostname [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]

spawn /usr/bin/telnet $hostname $port

expect "Username:"
send "$user\r"
expect "Password:"
send "$password\r"

interact


I also run this in /etc/rc.local:

TMPFILE=`mktemp`
head -4 /etc/motd >> $TMPFILE
mv $TMPFILE /etc/motd
chmod 644 /etc/motd
(
sleep 30
echo -n 'At Boottime, Console Manager Said That ' >> /etc/motd
sudo -u conman /usr/local/bin/screen -list >> /etc/motd
echo 'To connect, try: sudo -u conman screen -x pid.tty.host' >> /etc/motd
echo You may need to set g+rw perms on your tty first. >> /etc/motd
echo >> /etc/motd
) &


So when i login to the machine, i get a message like this:

OpenBSD 3.5-stable (GENERICRAID) #0: Tue May 18 11:20:39 EDT 2004

Authorized Use Only. Terminal Server.

At Boottime, Console Manager Said That There are screens on:
7555..pie (Detached)
16274..pie (Detached)
2 Sockets in /tmp/uscreens/S-conman.

To connect, try: sudo -u conman screen -x pid.tty.host
You may need to set g+rw perms on your tty first.



Which lets me know what to do to get to the consoles.

The only problem is that in the default install there are not enough pty's to support running 3 screens with 8 screens each and still allow people to ssh in.

molotov
June 9th, 2004, 15:36
in your kernel increase max users and psudo-devices for pty's


maxusers 32 # estimated number of users



pseudo-device pty 64 # pseudo-terminals


then run MAKEDEV

for more info, check out http://monkey.org/openbsd/archive/misc/9811/msg00247.html

hope this helps