简体   繁体   中英

Telnet inside a shell script

How can I run telnet inside a shell script and execute commands on the remote server?

I do not have expect installed on my solaris machine because of security reasons. I also do not have the perl net::telnet module installed.

So with out using expect and perl how can I do it?

I tried the below thing but its not working.

#!/usr/bin/sh
telnet 172.16.69.116 <<!
user
password
ls
exit
!

When I execute it, this is what I am getting:

> cat tel.sh
telnet 172.16.69.116 <<EOF
xxxxxx
xxxxxxxxx
ls
exit
EOF
> tel.sh
Trying 172.16.69.116...
Connected to 172.16.69.116.
Escape character is '^]'.
Connection to 172.16.69.116 closed by foreign host.
> 

Some of your commands might be discarded. You can achieve finer control with ordinary script constructs and then send required commands through a pipe with echo . Group the list of commands to make one "session":-

{
sleep 5
echo user
sleep 3
echo password
sleep 3
echo ls
sleep 5
echo exit
} | telnet 172.16.65.209

I had the same issue...however, at least in my environment it turned out being the SSL Certificate on the destination server was corrupted in some way and the server team took care of the issue.

Now, what I'm trying to do is to figure out how to get a script to run the exact same thing you're doing above except I want it to dump out the exact same scenario above into a file and then when it encounters a server in which it actually connects, I want it to provide the escape character (^]) and go on to the next server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM