Creating a script for a Telnet session?

前端 未结 11 1923
孤街浪徒
孤街浪徒 2020-11-30 02:16

Does anyone know of an easy way to create a script that can connect to a telnet server, do some usual telnet stuff, and then log off? I am dealing with users who are not fa

相关标签:
11条回答
  • 2020-11-30 02:58

    Expect is built for this and can handle the input/output plus timeouts etc. Note that if you're not a TCL fan, there are Expect modules for Perl/Python/Java.

    EDIT: The above page suggests that the Wikipedia Expect entry is a useful resource :-)

    0 讨论(0)
  • 2020-11-30 03:04

    I've used various methods for scripting telnet sessions under unix, but the simplest one is probably a sequence of echo and sleep commands, with their output piped into telnet. Piping the output into another command is also a possibility.

    Silly example

    (echo password; echo "show ip route"; sleep 1; echo "quit" ) | telnet myrouter
    

    This (basicallly) retrieves the routing table of a Cisco router.

    0 讨论(0)
  • 2020-11-30 03:06

    Couple of questions:

    1. Can you put stuff on the device that you're telnetting into?
    2. Are the commands executed by the script the same or do they vary by machine/user?
    3. Do you want the person clicking the icon to have to provide a userid and/or password?

    That said, I wrote some Java a while ago to talk to a couple of IP-enabled power strips (BayTech RPC3s) which might be of use to you. If you're interested I'll see if I can dig it up and post it someplace.

    0 讨论(0)
  • 2020-11-30 03:09

    I like the example given by Active State using python. Here is the full link. I added the simple log in part from the link but you can get the gist of what you could do.

    import telnetlib
    
    prdLogBox='142.178.1.3'
    uid = 'uid'
    pwd = 'yourpassword'
    
    tn = telnetlib.Telnet(prdLogBox)
    tn.read_until("login: ")
    tn.write(uid + "\n")
    tn.read_until("Password:")
    tn.write(pwd + "\n")
    tn.write("exit\n")
    tn.close()
    
    0 讨论(0)
  • 2020-11-30 03:10

    Another method is to use netcat (or nc, dependent upon which posix) in the same format as vatine shows or you can create a text file that contains each command on it's own line.

    I have found that some posix' telnets do not handle redirect correctly (which is why I suggest netcat)

    0 讨论(0)
提交回复
热议问题