LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: SickWorm

如何自动telnet

[复制链接]
发表于 2004-9-21 19:36:37 | 显示全部楼层
何不用SSH实现自动登录?
发表于 2004-12-27 01:18:48 | 显示全部楼层
:cool:  :cool:  :cool:  :cool:  :cool:
发表于 2004-12-27 01:30:03 | 显示全部楼层
偶也顶一下
发表于 2005-1-6 14:27:40 | 显示全部楼层

linux下自动telnet脚本

from:
http://www.linuxforum.net/forum/ ... &o=all&vc=1

(
sleep 1;\
echo "username";\
sleep 1;\
echo "userpasswd";\
sleep 1;\
sleep 1)|telnet aix_server

========
基本思路是这样的,以前参照上面做过一个带配置文件(server_ip、登录口令)的脚本自动重启路由器,改天贴上来。

我的cu口令丢了,这边贴子不是很务实啊。大家加油。
发表于 2005-1-6 22:40:22 | 显示全部楼层
Post by keltan
from:
http://www.linuxforum.net/forum/ ... &o=all&vc=1

(
sleep 1;\
echo "username";\
sleep 1;\
echo "userpasswd";\
sleep 1;\
sleep 1)|telnet aix_server

========
基本思路是这样的,以前参照上面做过一个带配置文件(server_ip、登录口令)的脚本自动重启路由器,改天贴上来。

我的cu口令丢了,这边贴子不是很务实啊。大家加油。



谁来讲讲,看者有点晕. 我的不能实现登陆,刚登陆上就掉了.
发表于 2005-1-7 11:13:50 | 显示全部楼层
ok, i back.
命令(command here) |telnet server_ip执行时,会在新的子sh环境下运行telnet server_ip,并把管道符前面的(command here)的输出作为该子sh的输入——按照这种理解(command here)执行结束后,会返回一个类似Ctrl+c的结束信号给telnet所打开的子sh——于是你的shell环境就会出现"Connection closed by foreign host."

其实http://www.linuxforum.net/forum/ ... 在bash下输入:
(sleep 1; echo "login_name"; sleep 1; echo "password"; sleep 1; { command_here } ) |telnet server_ip
注意观察输出结果——sleep 1等待1秒钟,用来控制输出给telnet的“echo文本”的先后序列,如果server_ip主机响应很慢的话,需要加大等待时间。你必须把要执行的全部命令放在管道符前面执行。

下面是我用过的脚本——登录cisco的路由器,检查并记录有关配置信息后退出。其中echo -en "\40"是模拟输入回车给路由器,让显示的"more"信息翻页。希望能给你些参考。
#!/bin/sh

# function to reboot Router
function rebootrouter ()
#$1 should be remote_ip or hostname
#$2 should be login_name
#$3 should be wait_time for each command
{
echo "===================`date `======================$1========================"
( echo "$2"
sleep "$3"
echo "en"
sleep "$3"
echo  "$2"
sleep "$3"
echo  "show run"
sleep "$3"
echo -en "\40"
sleep "$3"
echo -en "\40"
sleep "$3"
echo -en "\40"
sleep "$3"
echo -en "\40"
sleep "$3"
echo -en "\40"
sleep "$3"
echo -en "\40"
sleep "$3"
echo -en "\40"
sleep "$3"
echo -en "\40"
sleep "$3"
echo "exit") | telnet $1
}

# get the month and year of today, using for log filename
today=`date +%Y%m`
cfgfile="`basename $0`.cfg"

#if currently not in the directory where this shell-script in, then go into it
[ -z `echo $0 |cut -d'/' -sf2` ] || cd `dirname $0`
# check whether config file exist
if [ ! -f $cfgfile ]; then
  echo "Error: 配置文件: `basename $0`.cfg file 不存在! 你必须先建立该文件后才能运行此程序."
  echo "the configure file looks like as following:"
  cat << ENDF
# ipaddr:要操作的远程路由器的IP地址
# id:要操作的远程路由器的标识,用于命名日志文件名
# rtpwd:要操作的远程路由器的登录口令,不指定请输入"N"(大写),默认值cisco会被使用
# waitime:登录这台路由器,执行每步操作所需要花的时间(秒),不想指定值请输入"N",默认值3秒会被使用
# logdir:重启操作时的交换记录(日志)存放的目录,不指定则放在脚本所在的目录下
# 远程路由器的IP地址  地名简拼  路由器passwd  操作响应延时(秒)  日志文件路径
# ipaddr        id    rtpwd   waitime     logdir      
192.168.0.254   JN    N       1         
192.168.1.254   GL    abcd    5           /home/jerry/
ENDF
exit 1
fi

while read ipaddr id rtpwd waitime logdir
do
  case $ipaddr in
  \#*) ;;
  "") ;;
  *)
    [ $rtpwd = "N" ] && rtpwd="cisco"
    [ waitime = "N" ]&& waitime="3"
    [ -z $logdir ]&& logdir=`dirname $0`
  
  logfile="$id$today.log"
  output="tmp"
  cd $logdir
  # operate & redirect output and error to temp file
  rebootrouter $ipaddr $rtpwd $waitime  > $output 2>&1
  #dump temp file into logfile
  touch $logfile
  cat $output $logfile > dumpfile
  mv dumpfile $logfile
  esac
done < $cfgfile

rm $output
发表于 2005-1-12 14:31:36 | 显示全部楼层
自动输入用户名和密码用于telnet的shell, 哈哈 (原创)
转自:www.chinaunix.com
原文http://bbs.chinaunix.net/forum/v ... 4&show_type=new
#===========autotelnet.sh==============
#!/bin/bash
if (( $# != 1 ))
then
echo " usage: $0 address "
exit 1
fi
ip=$1
inp1=`cat param |grep "$ip" |awk '{ print $2 }'`
inp2=`cat param |grep "$ip" |awk '{ print $3 }'`
inp3=`cat param |grep "$ip" |awk '{ print $4 }'`

inputfile=in
outputfile=out
rm -fr $inputfile
rm -fr $outputfile
mknod $inputfile p
touch $outputfile

#file description 7 for out and 8 for in
exec 7<>$outputfile
exec 8<>$inputfile

telnet $ip <&8 >&7 &

sleep 1; echo $inp1 >> $inputfile
sleep 1; echo $inp2 >> $inputfile
sleep 1; echo $inp3 >> $inputfile

tail -f $outputfile &

while true
do
read str
if [[ $str = "quit" || $str = "exit" ]]
then echo $str >> $inputfile ; exit
else echo $str >> $inputfile
fi
done
#==================================


参数文件, 输入在出现正常提示符之前需要输入的所有内容, 用空格分开, 以ip地址或者hostname开头
#=====param============
localhost root password

#=====================
发表于 2005-1-14 19:35:55 | 显示全部楼层
试了一下,不错
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表