automate an SSH connection to cisco switch

Looking for some advice on writing a script to connect to a cisco switch and do a shut/no shut command. This is a setup involving an outdoor Wifi modem powered by PoE, so on occasion the ISP advises power cycling if internet access gets flaky.


Goal is to make a very simple script, connect, conf t, int fa1/0/x, shut, no shut, and if it could get a bit more detailed, do a 15 or 20 second ping before the script exits.

I could easily do it manually but the target end user isn't terribly tech savvy, and there isn't a concern for security on scripted access to the switch.

I work mostly on the PC side so I'm not terribly familiar with what may be involved for doing this on the Mac side.


Any suggestions, sample scripts, I'm all ears!


Thank you

Posted on Dec 14, 2014 9:51 PM

Reply
4 replies

Dec 15, 2014 1:06 PM in response to militarymedic23

Do you have an example script for the PC? Posting it would be helpful to those who might want to translate it to the Mac.



------------------------------------------

You could write a Unix bash script. To but a non-command line user interface on it, use Applescript. Here is an example applescript that invokes a Unix command.


(* 

It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.


    Author: rccharles
    
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
      
    For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
    


 *)


on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set desktopPath to (path to desktop) as string
    log "desktopPath = " & desktopPath
    
    set unixDesktopPath to POSIX path of desktopPath
    log "unixDesktopPath = " & unixDesktopPath
    
    set quotedUnixDesktopPath to quoted form of unixDesktopPath
    log "quoted form is " & quotedUnixDesktopPath
    
    try
        set fromUnix to do shell script "ls -l  " & quotedUnixDesktopPath
        display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
    on error errMsg
        log "ls -l error..." & errMsg
    end try
    
end run

Dec 15, 2014 9:13 PM in response to rccharles

I found this on the cisco forums which may be leading in the right direction.

https://supportforums.cisco.com/discussion/11553001/script-automate-tasks

Would be a bit less involved as it's only a port flip. Since the modem this port will be powering is external, I am aiming for this process to show the 15-20 of ping results so the end user can see the modem coming back online. Otherwise it just appears like nothing happens, one of those you don't believe if you can't see results.


I'm a bit out of my comfort zone here as my scripting is mostly in windows command line or powershell. The unix/linux arena is quite foreign to me.


Worst case scenario is I manage to automate a login process, and he can copy and paste the group of switch commands.


Based on either your unix script, or a conversion of the cisco process, is there a basic go-to I could start with for connecting via telnet or ssh, passing login info, and then passing commands? I can run a VM of OS X and try these commands from my workstation and go from there.

Dec 16, 2014 1:02 PM in response to militarymedic23

A problem is scripting whatever method you use to communicate with the modem or whatever. The solution you showed uses Expect to script a terminal. The terminal is then used to script the path to the modem.


You may be able to develop this on Windows. Expect claims to be portable across operating systems. On the downside, Expect lets you include OS commands which will not be. The action takes place in a scripting language called Expect.

http://en.wikipedia.org/wiki/Expect


Microsoft seems to have based PowerShell on Unix script languages like Bash, but not an exact copy.


getting Expect to run may be a hassle. I haven't tried.

http://expect.sourceforge.net/


https://supportforums.cisco.com/discussion/11553001/script-automate-tasks

file ssh.sh invokes the file enableint.sh for each ip address in the File1-IP-list.
.

#!/bin/bash
while read  ipadd
do
#echo $ipadd
export ipadd
./enableint.sh $ipadd
done < IP-list




#!/bin/bash

Identifies this file as a bash script.


#!/usr/bin/expect -f

Identifies this file as an expect script

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

automate an SSH connection to cisco switch

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.