Linux / UNIX Tech Support Forum
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 free or login to your account to remove all advertisements.
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
Thread Tools | Display Modes |
|
|||
|
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. |
|
||||
|
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; 20th July 2007 at 06: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(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 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 |
|
|||
|
I'm having the same problem, using the above scripts I can login to the remote server, but cannot execute any commands. I have added a command to the sshlogin.exp file as has MeeToo, but just get a response stating "invalid command name while executing .....".
Any ideas why this isn't working? Thanks In Advance Andrew PS. Just tried adding Exec before the command to be run, in this case a tail command, I now get the following : cannot open `/var/log/emaild.log' for reading: No such file or directory tail: no files remaining while executing "exec tail -f /var/log/emaild.log" Last edited by trotskyicepick; 23rd June 2010 at 06:02 PM. |
|
||||
|
Can you paste your script here? Also specify your UNIX and expect version.
__________________
Vivek Gite |
|
|||
|
Hi, thanks for the speedy response.
I'm actually using Linux, Ubuntu 10.04 Lucid to be precise, and Expect v5.44.1.14. Please find my scripts attached. Could the problem simply be that it won't work under Linux? Regards Andrew Last edited by trotskyicepick; 25th June 2010 at 02:03 PM. |
|
|||
|
I take it that as there have been no responses for quite some time, 2 years in the case of MeeToo's enquiry, that either no one is interested, or no one knows the answer?
|
|
|||
|
Solved problem.
Just use spawn ssh -t $username@$ipaddr "tail -f /var/log/emaild.log" and don't send the command at the end of the script. |
![]() |
|
|
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 | 11th October 2007 02:40 AM |
| shell scripts required for students | roni | Shell scripting | 6 | 20th July 2007 06:21 PM |
| Linux / UNIX set increase the number of failed login retries with SSH client | sweta | Getting started tutorials | 0 | 12th June 2007 02:35 AM |
| running command a root in shell scripts | chiku | Shell scripting | 1 | 17th July 2006 06:39 PM |
| sample scripts for FTP in Unix | jerry | Shell scripting | 1 | 23rd June 2006 11:01 PM |