×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

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.