我试图批量生成Windows的本机IRC客户端。您可以通过telnet连接到IRC服务器,但是您必须非常快地键入您的用户并且缺口,否则服务器将断开连接
SET /P server=[Server Address: ]
telnet %server% 6667
SET /P user=[Username/nick:]
echo "USER %user% 8 * : %user%"
echo "NICK %user%"
程序将连接到指定的服务器,但我无法将用户和nick命令回显给服务器。
答案 0 :(得分:0)
Windows内置的telnet客户端不是scriptabe。
试试这个(它是一个自编译的bat / c#脚本,应该使用.bat
扩展名保存。它来自here):
// 2>nul||@goto :batch
/*
:batch
@echo off
setlocal
::delete line when ready
del %~n0.exe >nul 2>nul
:: find csc.exe
set "csc="
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do set "csc=%%#"
if not exist "%csc%" (
echo no .net framework installed
exit /b 10
)
if not exist "%~n0.exe" (
call %csc% /nologo /warn:0 /out:"%~n0.exe" "%~dpsfnx0" || (
exit /b %errorlevel%
)
)
%~n0.exe %*
endlocal & exit /b %errorlevel%
*/
// minimalistic telnet implementation
// conceived by Tom Janssens on 2007/06/06 for codeproject
//
// http://www.corebvba.be
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace MinimalisticTelnet
{
enum Verbs {
WILL = 251,
WONT = 252,
DO = 253,
DONT = 254,
IAC = 255
}
enum Options
{
SGA = 3
}
class TelnetConnection
{
TcpClient tcpSocket;
int TimeOutMs = 100;
public TelnetConnection(string Hostname, int Port)
{
tcpSocket = new TcpClient(Hostname, Port);
}
public string Login(string Username,string Password,int LoginTimeOutMs)
{
int oldTimeOutMs = TimeOutMs;
TimeOutMs = LoginTimeOutMs;
string s = Read();
if (!s.TrimEnd().EndsWith(":"))
throw new Exception("Failed to connect : no login prompt");
WriteLine(Username);
s += Read();
if (!s.TrimEnd().EndsWith(":"))
throw new Exception("Failed to connect : no password prompt");
WriteLine(Password);
s += Read();
TimeOutMs = oldTimeOutMs;
return s;
}
public void WriteLine(string cmd)
{
Write(cmd + "\n");
}
public void Write(string cmd)
{
if (!tcpSocket.Connected) return;
byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(cmd.Replace("\0xFF","\0xFF\0xFF"));
tcpSocket.GetStream().Write(buf, 0, buf.Length);
}
public string Read()
{
if (!tcpSocket.Connected) return null;
StringBuilder sb=new StringBuilder();
do
{
ParseTelnet(sb);
System.Threading.Thread.Sleep(TimeOutMs);
} while (tcpSocket.Available > 0);
return sb.ToString();
}
public bool IsConnected
{
get { return tcpSocket.Connected; }
}
void ParseTelnet(StringBuilder sb)
{
while (tcpSocket.Available > 0)
{
int input = tcpSocket.GetStream().ReadByte();
switch (input)
{
case -1 :
break;
case (int)Verbs.IAC:
// interpret as command
int inputverb = tcpSocket.GetStream().ReadByte();
if (inputverb == -1) break;
switch (inputverb)
{
case (int)Verbs.IAC:
//literal IAC = 255 escaped, so append char 255 to string
sb.Append(inputverb);
break;
case (int)Verbs.DO:
case (int)Verbs.DONT:
case (int)Verbs.WILL:
case (int)Verbs.WONT:
// reply to all commands with "WONT", unless it is SGA (suppres go ahead)
int inputoption = tcpSocket.GetStream().ReadByte();
if (inputoption == -1) break;
tcpSocket.GetStream().WriteByte((byte)Verbs.IAC);
if (inputoption == (int)Options.SGA )
tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WILL:(byte)Verbs.DO);
else
tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
tcpSocket.GetStream().WriteByte((byte)inputoption);
break;
default:
break;
}
break;
default:
sb.Append( (char)input );
break;
}
}
}
////
static void Main(string[] args)
{
string server=args[0];
int port= Int32.Parse(args[1]);
string user=args[2];
string password=args[3];
string command=args[4];
//create a new telnet connection to hostname "gobelijn" on port "23"
TelnetConnection tc = new TelnetConnection(server, port);
//login with user "root",password "rootpassword", using a timeout of 100ms, and show server output
string s="";
string prompt = "";
try{
s = tc.Login(user, password,100);
Console.Write(s);
// server output should end with "$" or ">", otherwise the connection failed
prompt = s.TrimEnd();
prompt = s.Substring(prompt.Length -1,1);
if (prompt != "$" && prompt != ">" )
throw new Exception("Connection failed");
}catch(Exception e){
}
// while connected
while (tc.IsConnected && prompt.Trim() != "exit" )
{
// display server output
Console.Write(tc.Read());
// send client input to server
//prompt = Console.ReadLine();
try{
tc.WriteLine(command);
// display server output
Console.Write(tc.Read());
}catch(Exception ex){
}
}
Console.WriteLine("***DISCONNECTED");
//Console.ReadLine();
}
////
}
}
并使用它:
telnetcs.bat localhost 21 admin password command
测试非常有限,脚本不是故障安全的,但是如果你能发送命令就可以改进它。