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... |
try: man expect
|
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 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 |
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. |
Quote:
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. |
All times are GMT -5. The time now is 02:22 PM. |