Automating telnet login and commands on a Mac

3

Currently I do something like this:

telnet 192.168.0.3 23

username

password

cd /dir/

programname -s -g dosomething

I want to do this with one step, the coolest would be to compile a program for this using Automator, but i have no idea how to do that.
"shell script" in Automator fails at telnet 192.168.0.3 23 saying "connection refused" of course: I had no chance to type in password and so on.

Any ideas?

choise

Posted 2011-02-15T14:08:29.523

Reputation: 197

any particular reason why you're using telnet, not ssh? – vartec – 2011-02-15T14:10:03.573

only telnet running on the remote machine (external windows server =/ ) – choise – 2011-02-15T14:15:32.777

good question I have to do the same thing with my modem - and no ssh – smashtastic – 2011-10-15T21:21:48.857

Answers

2

When ssh isn't available, expect is typically used to automate access using telnet.

From the linked article:

#!/usr/bin/expect #Where the script should be run from.
set timeout 20 #If it all goes pear shaped the script will timeout after 20 seconds.
set name [lindex $argv 0] #First argument is assigned to the variable name
set user [lindex $argv 1] #Second argument is assigned to the variable user
set password [lindex $argv 2] #Third argument is assigned to the variable password
spawn telnet $name #This spawns the telnet program and connects it to the variable name
expect "login:" #The script expects login
send "$user " #The script sends the user variable
expect "Password:" #The script expects Password
send "$password " #The script sends the password variable
interact #This hands control of the keyboard over two you (Nice expect feature!)

Paused until further notice.

Posted 2011-02-15T14:08:29.523

Reputation: 86 075

2

Then, if no sshd is installed, you could automate your telnet using expect, that you probably will have to install on your mac.

Expect is a tool primarily for automating interactive applications, such as telnet, ftp, passwd, fsck, rlogin, tip, and more. Expect really makes this stuff trivial. Expect is also useful for testing these applications. It is described in many books, articles, papers, and FAQs. There is an entire book on it available from O'Reilly.

See http://www.nist.gov/el/msid/expect.cfm .

rems

Posted 2011-02-15T14:08:29.523

Reputation: 1 850

0

If the system running on 192.168.0.3 has a sshd server running, then you could simply do

ssh username@192.168.0.3 'cd /dir/ ; programmname -s -g dosomething'

To check if you have sshd running on 192.168.0.3 you could either just try it, or login using telnet as you do already and, if it's a unix/linux/maxosx try doing a

pgrep -fl sshd

or

ps -e | grep sshd

or

lsof -i :22

All of these above will either not find anything or give you a line showing that sshd is running.

rems

Posted 2011-02-15T14:08:29.523

Reputation: 1 850

poorly there is no ssh installed. and also its password saved – choise – 2011-02-15T15:00:09.773

It's a Windows server. – user1686 – 2011-02-15T15:37:43.330

1I hope you know that using telnet is very bad security wise. Your username and passwords are sent in plain through the network. Anyone with some computer knowledge sitting on that network would be able to read your username and password in few minutes. Try moving to ssh which also works on Windows. – rems – 2011-02-15T15:47:32.900