This is a discussion on SSH - Passing Unix login passwords through shell scripts within the Shell scripting forums, part of the Development/Scripting category; Hi, I am new to SSH and need your inputs to achieve the following: Develop a shell script which will ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
Hi,
I am new to SSH and need your inputs to achieve the following: Develop a shell script which will read a configuration file with entries like server1 smith/tiger server2 dave/lion where server1 is machine name and smith/tiger is username / password respectively. By reading the configuration file, shell script should login to server1 as user smith preferably using SSH. Thanks, Arul. |
| Sponsored Links | ||
|
|
|
||||
|
You need to create a login file as follows login.txt:
Code:
server1|user|password Code:
#!/bin/bash
FILE=login.txt
CONNECT=sshlogin.exp
SERVERNAME=$1
MyServer=""
MyUser=""
MyPassword=""
exec 3<&0
exec 0<$FILE
while read line
do
MyServer=$(echo $line | cut -d'|' -f1)
MyUser=$(echo $line | cut -d'|' -f2)
MyPassword=$(echo $line | cut -d'|' -f3)
if [ "$SERVERNAME" == "$MyServer" ];
then
echo "Running ssh $MyUser@$MyServer..."
$CONNECT $MyPassword $MyServer $MyUser
fi
done
exec 0<&3
echo "$SERVERNAME not found in login.txt file"
In order to use following script you need to install expect tool, use apt-get or yum command! Code:
#!/usr/bin/expect -f # Expect script to supply root/admin password for remote ssh server # and execute command. # This script needs three argument to(s) connect to remote server: # password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # ------------------------------------------------------------------------ # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- # set Variables set password [lrange $argv 0 0] set ipaddr [lrange $argv 1 1] set username [lrange $argv 2 2] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh $username@$ipaddr match_max 100000 # Look for passwod prompt expect "*?assword:*" # Send password aka $password send -- "$password\r" # send blank line (\r) to make sure we get back to gui send -- "\r" expect eof Code:
chmod +x sshlogin.sh chmod +x sshlogin.exp Code:
./sshlogin 127.0.0.1 Last edited by monk; 07-20-2007 at 07:20 PM.. |
|
|||
|
Hi Experts,
The information provided here is really helpful but it does not serve my requirements. I have more than 200 machines in my network running linux and I want to be able to ssh to each one of them using thier IP address stored in a file and then run some commands inside each machine, log out and log in the next machine in the list and do the same, so on ... Now, using key-gen is not practical for me and I do not want to install the "expect" utility due to some reason. Please tell me if there is any way to supply ssh password using bash scripting? I know supplying the password in script might not be very secure, but still I want to do it this way. I shall be greatful to any help. Regards, R. |
|
|||
|
Hi,
There is one option in SSH,i.e ssh-keygen, we can generate the public keys and we need to move it to particulars users home directory... /home/dave/.ssh/ur-keys use this following link to know little bit clear ssh-keygen: password-less SSH login ssh-keygen - authentication key generation SSH with Keys HOWTO: SSH with Keys in a console window Good luck.............. |
|
|||
|
Thanks to Monk for the script. i am very new to Linux but i need to create a script to run another script on a remote server. i can now log in through SSH but i cannot even run an ls command. it returns "invalid". i have done a lot of research and still cannot find the answer. the script i am using is below and i have inserted the TOP command as an example. what i need to do is:
1. Open SSH connection on one remote server. 2. run a script ./Myscript -m 3. capture the results 4. it would be nice to keep the connection open #!/usr/bin/expect -f # Expect script to supply root/admin password for remote ssh server # and execute command. # This script needs three argument to( connect to remote server:# password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # ------------------------------------------------------------------------ # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit Bash Shell Scripting Directory For Linux / UNIX for more information. # ---------------------------------------------------------------------- # set Variables set password [lrange $argv 0 0] set ipaddr [lrange $argv 1 1] set username [lrange $argv 2 2] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh root@123.456.789.123 match_max 100000 # Look for passwod prompt expect "*?assword:*" # Send password aka $password send -- "MyPassWord\r" # send blank line (\r) to make sure we get back to gui send -- "\r" top -b -n 1 | head -n 8 expect eof |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| passing options to a shell script | bigpaw | Shell scripting | 2 | 10-11-2007 03:40 AM |
| shell scripts required for students | roni | Shell scripting | 6 | 07-20-2007 07:21 PM |
| Linux / UNIX set increase the number of failed login retries with SSH client | sweta | Getting started tutorials | 0 | 06-12-2007 03:35 AM |
| running command a root in shell scripts | chiku | Shell scripting | 1 | 07-17-2006 07:39 PM |
| sample scripts for FTP in Unix | jerry | Shell scripting | 1 | 06-24-2006 12:01 AM |