Batch file to telnet and run commands on a unix se

2019-08-17 10:42发布

I'm stuck here, and I really can use your help. Here's my situation.

Im looking forward to create a batch script which can access a unix server using telnet. Here the unix sever will ask for username/password. After it has authenticated the user, i want to preform some operations like chown on a perticular file.

Here's what im looking to do :

    telnet
    open xyz.abc.com
    username
    password

    command 1
    command 2
    command 3
    exit 

where

xyz.abc.com is the unix server where i want to connect.
username and password are the credentials to access this server 
command 1,command 2,command 3 are the commands i want to run in the server.

My problem is i cant use SSH. It should take all the data from the file instead of prompting the user to enter data and should run as an automated script.

2条回答
乱世女痞
2楼-- · 2019-08-17 11:24

This is the .bat file I created to telnet to a server, change a file permission, then ftp the file to my PC and open it. Hopefully it will help you out. The variable filename is there because I always login and move to the same directory name every time. You can change it to a prompt so the user can enter where they would like to cd to. Let me know if you have any questions.

:: This will telnet to the server, change the permissions, 
:: download the file, and then open it from your PC. 

:: Add your username, password, servername, and file path to the file.
:: I have not tested the server name with an IP address.

:: Note - telnetcmd.dat and ftpcmd.dat are temp files used to hold commands

@echo off
SET username=
SET password=
SET servername=
SET filepath=

set /p id="Enter the file name: " %=%

echo user %username%> telnetcmd.dat
echo %password%>> telnetcmd.dat
echo cd %filepath%>> telnetcmd.dat
echo SITE chmod 777 %id%>> telnetcmd.dat
echo exit>> telnetcmd.dat
telnet %servername% < telnetcmd.dat


echo user %username%> ftpcmd.dat
echo %password%>> ftpcmd.dat
echo cd %filepath%>> ftpcmd.dat
echo get %id%>> ftpcmd.dat
echo quit>> ftpcmd.dat

ftp -n -s:ftpcmd.dat %servername%

SET mypath=%~dp0
%mypath%%id%

del ftpcmd.dat
del telnetcmd.dat
查看更多
男人必须洒脱
3楼-- · 2019-08-17 11:26

Check out expect. Thie above is exactly what it's designed for.

Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. It uses Unix pseudo terminals to wrap up subprocesses transparently, allowing the automation of arbitrary applications that are accessed over a terminal.

查看更多
登录 后发表回答