×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Switch and router provide very limit interface to you. Not like normal unix server you can write shell script easily. But if you really want to do that, here is the way:

Use a script to simulate a telnet client to do automation ;) see below is an python example. I added some timer to make sure every command fire correctly:


import getpass
import sys
import telnetlib
import time

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("ogin")
tn.write(user + "\n")
if password:
tn.read_until("assword")
tn.write(password + "\n")

tn.write("\n")
tn.write("ls\n")
time.sleep(1)
tn.write("exit\n")
time.sleep(1)
print tn.read_all()
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 各位UNIX 大侠指点一下。 从windows 2000 上telnet 进SWITCH, 想执行多行命令,怎样才可以一次执行多行命令,就象WINDOWS 下的.bat 文件一样。 多谢了。
    • ";" semicolon., but sonsole in a switch is a piece of customized mini OS, it might not work.
    • use Expect in unix shell script.
      • 可以用EXPECT 在WINDOWS 2000 机器上TELNET , 然后执行多行命令吗?
        • yes, find a win32 ported expect.
    • 这个与UNIX 没什么关系嘛
    • some telnet clients like secureCRT allow you to execute a script ( or a macro). Or write a Perl script to do it.
      • perl is perfect at this case, find a cisco perl module do everything you want with router and switch.
    • Switch and router provide very limit interface to you. Not like normal unix server you can write shell script easily. But if you really want to do that, here is the way:
      Use a script to simulate a telnet client to do automation ;) see below is an python example. I added some timer to make sure every command fire correctly:


      import getpass
      import sys
      import telnetlib
      import time

      HOST = "localhost"
      user = raw_input("Enter your remote account: ")
      password = getpass.getpass()

      tn = telnetlib.Telnet(HOST)

      tn.read_until("ogin")
      tn.write(user + "\n")
      if password:
      tn.read_until("assword")
      tn.write(password + "\n")

      tn.write("\n")
      tn.write("ls\n")
      time.sleep(1)
      tn.write("exit\n")
      time.sleep(1)
      print tn.read_all()
    • Here is my solution, I used Excel to generate config script from design document, use PUTTY logon each Switch, paste script in PUTTY. I had finished 20 Switches config in a afternoon.