LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2010, 12:37 PM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
script which logs into devices over ssh and executes commands


I would like to make a script, where I specify my Cisco devices IP addresses as arguments to the script and then this script automatically logs into every one of them and does "show version" and "exit" in every Cisco device. I have public/private key authentication system with my Cisco devices- thats why I'm using ssh-agent and ssh-add. I did something like this:

Code:
#!/bin/bash

if [ -z $1 ]; then
echo USAGE:
echo "$0 IP1 IP2 IP3 IPn"
else
eval `ssh-agent` >& /dev/null
ssh-add
for i in "$@"; do
ssh $i
show version
exit
done
fi
However, it logs nicely into the first device(I'm able to execute IOS commands in this first Cisco device etc) and then does nothing further. Any ideas?
 
Old 03-23-2010, 12:51 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
You should really be doing this via expect or via a scripting language or even via a ssh with command attached... your problem is the exit-- it's likely getting evaluated locally.

I'd try... "ssh $i show version" in place of "ssh $i; show version; exit" if you really want to stick with bash alone.

Last edited by rweaver; 03-23-2010 at 12:54 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running multiple commands remotely via SSH in a script gimpy530 Linux - General 4 12-19-2009 10:22 PM
How to write a script with auto login ssh and then run commands? keber314 Linux - Server 13 02-16-2009 03:37 PM
need to write a script that executes on startup! philspargo Linux - Newbie 3 09-16-2008 07:10 PM
Program that logs in on remote computer via ssh and executes commands on it harrybazeegar Linux - Software 2 09-08-2007 07:14 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration