Shell scripting

Hi All,
I am not able to see the output of $1 variable as I am passing in below script:--

#!/bin/bash -xv
echo "Enter ur password"
+ echo 'Enter ur password'
Enter ur password
read pw
+ read pw
dbaguest
echo "Enter username"
+ echo 'Enter username'
Enter username
read usrnm
+ read usrnm
password
echo "enter ip addr"
+ echo 'enter ip addr'
enter ip addr
read ip
+ read ip
192.168.12.1
while [ $# -ne 0 ]
do
echo $1
shift
done
+ '[' 0 -ne 0 ']'
echo "logging in to the server"
+ echo 'logging in to the server'
logging in to the server
ssh $2/$1@$3
+ ssh /@
ssh: : host/servname not known
echo "success"
+ echo success
success
exit 0
+ exit 0

Please suggest me some way out.

Regards,
Manish

Because $1,$2 and $3 contains nothing in your case.
these the special variable used when you pass values as the command line arguments while executing script. and I guess you are not passing anything instead expecting from user to type the username, passwd and hostname.

when you reading the values these are automatically defined the the variable which you are passing in the read ( usrnm, pw and ip ) in your case.

so try replacing $1,$2 and $3 with $pw, $usrnm and $ip respectively.

#!/bin/bash -xv

echo "Enter ur password"
read pw
echo "Enter username"
read usrnm
echo "enter ip addr"
read ip
echo "logging in to the server"
ssh ${usrnm}/${pw}@${ip}
echo "success"
exit 0

use the variables that you use for reading $pw, $usrnm, $ip...and not $1, $2 and $3...

See below ....

#!/bin/bash -xv
echo "Enter ur password"
+ echo 'Enter ur password'
Enter ur password
read pw
+ read pw
dbaguest
echo "Enter username"
+ echo 'Enter username'
Enter username
read usrnm
+ read usrnm
password
echo "enter ip addr"
+ echo 'enter ip addr'
enter ip addr
read ip
+ read ip
192.168.12.1
while [ $# -ne 0 ]
do
echo $pw
shift
done
+ '[' 0 -ne 0 ']'
echo "logging in to the server"
+ echo 'logging in to the server'
logging in to the server
ssh $usrn/$pw@$ip
+ ssh /@
ssh: : host/servname not known
echo "success"
+ echo success
success
exit 0
+ exit 0

Why don't you just paste the script content ?

Hi ,
Below script is not working as expected.It is timing out while it is trying to log to the server using ssh.Password is correct but I am not sure how password is going to be sent using expect module.Please let me know if any syntax error is there in my script.

#!/bin/bash -xv
username="dbaguest"
echo  "Enter your password:"
read  newPassword
cat ssh.csv | while read line;
do
username=`echo $line | cut -d, -f1`
addr=`echo $line | cut -d, -f2`
paswd=`echo $line | cut -d, -f3`
echo "$username,$addr,$paswd"
cct=$(./test.exp $paswd $newPassword $addr $username)
if [[ "$cct" == *login* ]]; then
echo "SSH Connection to $host succeeded" >> testlog
if [[ "$cct" == *successfully* ]]; then
echo "Password has been changed successfully for $host" >> testlog
else
echo "Password change failed for $host." >> testlog
echo "$host,$addr,$paswd" >> errorlog
fi
else [[ "$cct" == *Permission* ]]
echo "$host,$addr,$paswd" >> errorlog
fi
cct=""
done

==============================
test.exp:--

#!/usr/local/bin/expect -f --
set currentpasword [lrange $argv 0 0]
set newpassword [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set username [lrange $argv 3 3]
set cmd "passwd"
spawn  ssh -t $username@$ipaddr
expect  "Enter password for key:"
send "yes\r"
expect "*password*"
send "$currentpasword\r"
expect "$"
expect "Last login:" {
send "$cmd\r"
expect "$"
expect "*(current) UNIX password*"
send "$currentpasword\r"
expect "*New UNIX Password*"
send "$newpassword\r"
expect "*Retype*"
send "$newpassword\r"
expect "$"
expect "exit\r"
}

Regards