telnet 用shell脚本实现远程操作实例
(2012-11-04 14:24:33)
标签:
it |
分类: IT_Bolg |
shell中的telnet命令
(1)Test1
sleep 2;echo "password";\
sleep 2;echo "^M";
sleep 2;echo "tar xf filename&";\
sleep 2)|telnet yourIP
^M用ctrl + V +M
很多人认为telnet象passwd和su一样,无法通过管道和输入转向使其自动工作,但实际上telnet是支持输入转向的.虽然,严格的说,shell脚本无法使telnet进入交互状态,但它确实可你使你进入远程系统并运行你希望的命令后退出.这远比用rsh(remsh,rcmd)更令人放心,
使用如下的脚本可以达到你的要求:
(sleep 1;echo user;sleep 1; echo passwd;echo yourcmd ;sleep
1)|telnet remotehost
其中user,passwd,yourcmd,remotehost分别更换成你的用户名,用户口令,你想要运行的命令,远程机器名或IP便可以了.
如果要想使用脚本进入交互状态,可以使用expect.
如果你要通过telnet某个IP的端口来得到部分数据,则可以用以下命令
(sleep 5;) | telnet IP PORT 这样就可以在telnet后将PORT在5秒内输出的数据打印到屏幕上,然后自动退出
EG:
tempValue="KEY"
str4=`(sleep 1;echo AA;sleep 1; echo 123; sleep 1; echo tail -f
/aa/log/a.log;sleep 1) | telnet ${IP} | grep ${tempValue}`
num4=`echo ${#str4}`
if [ ${num4} -eq 0 ];then
else
fi
(3)Test3
shell脚本:自动FTP和TELNET
下面是脚本的主要框架:
#!/bin/bash
#don't use chinese character
#####################################################
#
#####################################################
CONFIG_FILE="auto.cfg"
REMOTEHOST=""
USERNAME=""
PASSWD=""
SRC_DIR="userfile/llh/task"
DEST_DIR="userfile/llh/task"
COMMAND=$1
COMMAND_FILE=$2
#####################################################################
#
#####################################################################
#param:remoteHost,username,password,dir
function my_telnet
{
}
#ftp put command and file
function ftp_put
{
#put command and file
#ip=
#user
ftp -n $REMOTEHOST <<FTPEOF
FTPEOF
}
#ftp put file only
function ftp_put_file
{
#put command and file
#ip=
#user
ftp -n $REMOTEHOST <<FTPEOF
FTPEOF
}
function usage
{
}
function test_input
{
}
##################################################
#
##################################################
################################################################################
#
################################################################################
test_input
(4)Test4
set timeout 30
spawn telnet 127.0.0.1 4000
expect "command-line:"
sleep .3
interact
-----------------
ok,搞定,谢谢了:)
#!/bin/ksh
user=xxxx
passwd=xxxxxx
ipaddress[0]="xx.xx.xx.xx"
..
..
ipaddress[n]="xx.xx.xx.xx"
for I in ${ipaddress[@]}
do
{
}
done
exit 0