hi everyone,

Originally I had a script like: telnet host port then type STATUS for checking remote server,local server status and connection. In order to omit typing "STATUS", now I'm trying combine these two commands into one line, how to write it?
or do we have another way to achieve the same purpose?

I'm a newbie of Unix, the question I already asked many people, but still cannot get a answer, really hope someone can teach me how to solve the problem. thank you~

Recommended Answers

All 12 Replies

if the host supports ssh, that would be the way to go.

thank you, griswolf. but how can I check the host support ssh or not?

I've checked process(ps -ef|grep ssh), and its alive. but what is the way to use ssh?

I have Filezilla in my computer, but I still cannot understand how to make one command to check remote server(with port) status.

Filezilla makes use of the ssh file transfer protocol, but it is not otherwise an SSH client. Are you saying you want the port status of the telnet daemon on the particular port? That would not work with SSH which is just a way to do generic work on the remote machine (anything from a shell session to running a single shell command to running a big script to ...)

seems like SSH cannot work for my prupose. And exactly, I need to check two particular port status of the telnet daemon. My manager asked me to write into one command or shell and shows the result directly, so that operator didn't need to type any word. Do we have another way to solve the problem?

Here's an interesting thread: http://www.linuxforums.org/forum/linux-programming-scripting/63702-can-you-write-script-starts-telnet-then-input-commands-into-telnet.html

And the answer appears to be "You can learn to do it". I seem to maybe remember messing with expect a dozen years ago, but that was then and this is now... Here's an example (http://www.computing.net/answers/unix/a-script-to-telnet-a-service/6542.html):
The expect syntax looks like this:

spawn "/bin/sh"
send "telnet localhost 1300\r"
sleep 1
expect "some_prompt"
send "subnet 1.2.3.4 > output.txt\r\r"
close

thanks griswolf, I've tried the code below...

#!/usr/bin/expect --

spawn telnet 1.2.3.4 8080

expect "Trying 1.2.3.4...
Connected to 1.2.3.4.
Escape character is '^]'."

send "STATUS"

close

but it shows ksh: test.sh: not found

On my machine, this gets somewhat closer without actually managing any output. The spawn apparently has to be a shell? Not clear. From that shell(?) you send the telnet command. Anyway, I do get connected, and disconnected again, just don't get output. And note: You can "expect" just the last line of the prompt. Also note the double quotes enclosing a newline. If you are crossing platform boundaries, you may need to instead send "blah\r" or the like as the found example did.

#!/usr/bin/expect --

spawn "/bin/bash"
send "telnet localhost
"
expect "login: "
send "griswolf
"
expect "Password:"
send "a big secret
"
send "ls -la
"
close

Aha. Late breaking news. Look here for everything you could ever want, and more (including examples)... You will have to search around, the layout is pretty dense: http://expect.sourceforge.net/ Examples: download and open the gz file and look at .../expect-5.43/example. Wow.

Final note: in the examples directory is autoexpect. Edit the shbang line to be correct for your system's expect, then follow instructions as found in the first few lines. The result is very informative, even if you don't use it directly.

The examples are really great, even if I don't figure out how to solve my problem yet , but very informative.

I modify the code that you told me, add "\r" and get only the last line(?), but it still not work and shows same message : ksh: test.sh: not found

#!/usr/bin/expect --

spawn "/bin/bash" 
send "telnet 1.2.3.4 8080\r"

expect "Escape character is '^]'.""
send "STATUS\r"

close

Since I don't understand expect I have the same trouble with this that you have. Three ways forward.

  1. Get autoexpect working; and in that context from the autoexpect command prompt, by hand, telnet to your machine and ask it for STATUS. Examine the file that results. If you post it here, be sure to change the password and login names.
  2. Go read more documents about expect. Think. Practice. Become the guru for expect (and incidentally solve your problem)
  3. Debug by binary search: Discard last "half" of the script. Same trouble? Problem is in the top half. Different trouble? Problem is likely in the bottom half. Now split the problem part again and repeat until you know the line of the problem. Note: For 4 line program, just chop off bottom line, then bottom two lines, then ... until you see a change in behavior

I have made these suggestions in priority order: Do option 1 unless there is a good reason not...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.