LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-31-2012, 04:10 PM   #1
getraj.12
LQ Newbie
 
Registered: Jul 2010
Location: Bangalore, India
Distribution: RedHat, Suse, Solaris
Posts: 15

Rep: Reputation: 0
Script to SSHand run some commands


Hello Guys..

I am looking for a shell script to connect to servers and run some commands and copy the command O/P to a file on the local server and exit, Then move on to another server and do the same task.

Note : I do not have ssh password less authentication enabled for all the servers and I have 200 + servers to login.

I would appreciate a prompt response...
 
Old 10-31-2012, 07:46 PM   #2
flirek
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Rep: Reputation: Disabled
try: man expect
 
Old 10-31-2012, 11:19 PM   #3
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
I just did this and it worked. (Pretend my remote server is named server.com.) The text file is written locally. Is this what you mean?
Code:
ssh server.com ls > server-ls.txt
Expect was necessary back in the days of telnet, but ssh is so flexible, you can do almost anything directly in a shell script once you have password-less logins working. It should be trivial to extend this to a loop with the server name in a variable.

In fact, some of my scripts have lines like this, where $SSH is either empty or "ssh server" so I can use the same script on the local and remote machines.
Code:
$EVAL $SSH VBoxManage controlvm $VM poweroff
 
Old 11-01-2012, 07:48 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
ssh-agent

For the 200+ servers to log into, I would use key based authentication. Yes, the keys will have to be copied to each server individually, but once that is done they can be reused many times. You can leave the keys with a strong password and use an agent to help with authentication. That way you can enter the passphrase once and then use the key for many logins without having to reenter the passphrase.

Most distros have an agent available for you by default. If you run "set | grep SSH" and see SSH_AUTH_SOCK and SSH_AGENT_PID then you have an agent running and available. You can put it to use with a key using ssh-add.
 
Old 11-01-2012, 08:20 AM   #5
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by getraj.12@gmail.com View Post
Note : I do not have ssh password less authentication enabled for all the servers and I have 200 + servers to login.
Oops. I missed the not in that sentence.

I agree with Turbocapitalist. But I use keychain to store my key in memory. That is, the first time I login after a boot, I'm prompted for the passphrase that unlocks my SSH private key. Then the unlocked key is held in memory (even when I'm logged out) until the machine is rebooted or I kill the keychain process.

With that setup, I can use ssh and scp to access every host which I have previously added my public key to. The public key from the desktop in front of you, e.g. ~/.ssh/id_dsa.pub, must be appended to the ~/.ssh/authorized_keys file on all those servers. They're both text files.

Otherwise, maybe flirek's suggestion of expect was reasonable. It allows you to automate typing in a password.
 
  


Reply



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
script needs su commands but is run by user mervclel Solaris / OpenSolaris 4 11-24-2010 09:18 PM
bash script to run commands dormant Programming 1 08-25-2009 01:35 PM
Write a script to run several commands ??? wahaha Linux - Software 1 04-13-2007 08:20 PM
Creating a script to run two commands anjaan Programming 11 06-15-2004 02:51 AM
run 2 commands in a script ddpicard Linux - General 10 06-13-2003 04:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:41 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