need script for passwd , can't use expect tool

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Infrastructure Monitoring need script for passwd , can't use expect tool
# 29  
Old 06-29-2009
Perderabo Script

Hello,

Many thanks for the script posted by Perderabo. I came across it many years ago, and it was an immense help in automating a password change on a number of servers and for a number of accounts on those servers.

Now, I am in need of a script to do the same thing using SSH since our Unix Admins have disabled the Telnet process and are forcing only SSH access.

Searching this web site and the internet for any assistance, it plainly became clear that the 'expect' package was the only solution that would do something similar to Perderabo's script.

But,

I had setup SSH authentication, copying the source servers RSA tokens to the destination servers authorized_keys file.

I changed Perderabo's script to have just the two sections - 0 and 2. 0 to set the script up, request passwords, etc. and 2 to do the actual password change.

I then changed 'telnet' to be 'ssh -t -t' and removed the USER and OLDPASS prints, since they are not needed due to the SSH automated authentication.

Quote:
#!/bin/ksh

HOSTLIST="<destination server>"
DELAY=3
stty -echo
print -n Enter Old Password-
read OLDPASS
print
print -n Enter New Password-
read NEWPASS
print
stty echo
exec 4>&1

for HOST in $HOSTLIST ; do
echo "Changing ${HOST} password:"
ssh -t -t $HOST >&4 2>/dev/null |&
sleep $DELAY
print -p passwd
sleep $DELAY
print -p $OLDPASS
sleep $DELAY
print -p $NEWPASS
sleep $DELAY
print -p $NEWPASS
sleep $DELAY
print -p exit
wait
echo "Changed ${HOST} password!"
done

exit 0
This works for me ... it will connect via SSH to the destination server and does the password change.
# 30  
Old 07-15-2009
Perderabo Image Image
Unix Daemon

Hi Perderabo

The script is really great. But in my system the option print -p and telnet is not working. I only have ssh and sftp enabled in the system.

This is working in Linux but not in solaris.
(sleep1; echo $OLD; sleep 1; echo $NEW; sleep 1; echo $NEW;sleep 1) | passwd

Is there any other way to do this in solaris......

Hi Perderabo

It is really a good script. In my machine print -p and telnet are not working. I am having sftp and ssh to write this type of script. I tried changing your script and implement it, but its not working. Is there any other way to change the password on multiple SunOS machines.


The below command is working fine in Linux but not in SunOS.
(sleep1; echo $OLD; sleep 1; echo $NEW; sleep 1; echo $NEW; sleep 1) | passwd.


Could you please advice.

Last edited by vbe; 07-16-2009 at 04:49 AM.. Reason: artefact
# 31  
Old 07-15-2009
"print -p" is a built-in command in the Korn shell. It will work only if there is a coprocess running otherwise it will produce an error message. In fact it sends the output of a print-command to the running coprocess as input to stdin. (likewise "read -p" will read from the coprocesses stdout)

If you try this script with bash (or any other shell) it won't work because most shells lack the coprocess facility.

The reason why simple redirections to/from passwd do not work on most systems is that passwd clears stdin upon start to enforce real, physical keyboard input. It was designed this way with security in mind. Setting passwords via scripts is always a probable security hazard.

I hope this helps.

bakunin
# 32  
Old 07-16-2009
I do agree with you. But I executed this portion and I got the problem like this
I have Generating public/private rsa key pair using this (ssh-keygen -t rsa)
I am using ssh here to change the password after loging into that HOST.
But it is throwing me the error saying

passwdChg.ksh[19]: print: no query process
passwdChg.ksh[21]: print: no query process
passwdChg.ksh[23]: print: no query process
passwdChg.ksh[25]: print: no query process
passwdChg.ksh[27]: print: no query process

I can see the coprocess is running in line #17. But I could not understand why I getting the above error.

####################
1 #! /usr/bin/ksh
2
3 HOSTLIST="test1 test2"
4 DELAY=3
5 stty -echo
6 print -n Enter Old Password-
7 read OLDPASS
8 print
9 print -n Enter New Password-
10 read NEWPASS
11 print
12 stty echo
13 USER=$(whoami)
14 exec 4>&1
15
16 for HOST in $HOSTLIST ; do
17 ssh -t -t $USER@$HOST >&4 2>&4 |&
18 sleep $DELAY
19 print -p passwd
20 sleep $DELAY
21 print -p $OLDPASS
22 sleep $DELAY
23 print -p $NEWPASS
24 sleep $DELAY
25 print -p $NEWPASS
26 sleep $DELAY
27 print -p exit
28 wait
29 done
30 exit 0


Could you please advice???

Thanks
Siddharth
# 33  
Old 07-16-2009
The problem seems to be with the ssh command. My ssh book seems to imply that -t only works with a command but I tried using ksh and it still fails.

This works:
echo env | ssh user@host ksh

This fails:
echo env | ssh -t -t user@host ksh

I don't know why the latter fails. But I think that is the crux of your problem.
# 34  
Old 07-19-2009
Hi Guys,

With all respect to Perderabo script - great job = THANKS, in case anybody use Secure CRT to ssh/telent, the latest version 6.2.1 has a feature called Chat window which allow you to run the same command on multiple servers.

This feature will do the job for you if you have the same users on all servers.

Cheers,
Dani
# 35  
Old 07-20-2009
Hi Perderabo

I am trying this script in Solaris 5.10. First of all I am trying to change the password for the local SunoS 5.10 server. But with the below error.

Command:
--------------
( sleep 6 && echo $OLDPASS >&0 ;sleep 6 && echo $NEWPASS >&0 ;sleep 6 && echo $NEWPASS >&0 )|passwd

Error
-------------
passwd: Sorry, wrong passwd
Permission denied

######################

I have update the the following..

exec 4>&1
exec 0>&4

for HOST in $HOSTLIST ; do
exec >&4
exec 2>&4
ksh |&
sleep $DELAY
print -p $USER
sleep $DELAY
print -p $OLDPASS
sleep $DELAY
print -p passwd
sleep $DELAY
print -p $OLDPASS
sleep $DELAY
print -p $NEWPASS
sleep $DELAY
print -p $NEWPASS
sleep $DELAY
print -p exit
wait
exec 4>&-
done

Error:
------------

ksh: user : not found
ksh[2]: OldPass: not found
passwd: Changing password for user
Enter existing login password:
passwd: Sorry, wrong passwd
Permission denied
ksh[4]: OldPass : not found
ksh[5]: NewPass : not found
ksh[6]: NewPass : not found

I think the output is not going properly to the input of passwd in the aboce part...

Could you pelase advice????
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

3. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

4. Shell Programming and Scripting

script using expect tool

Hi All, I need ur help and suggestion to make my code efficient.I have to reset the password for multiple unix flavour using expect tool.There is one file in which I have mentioned ip address and main script will pickup those ip and reset all password and it will write to a log file whether it has... (1 Reply)
Discussion started by: manish_1678
1 Replies

5. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

6. Solaris

tool to convert /etc/passwd and etc/shadow

i wonder if there is a tool to read the /etc/passwd or /etc/shadow files in order to reset user accounts to the same one. By moving (restore) all filessytem and data to another same Sun box, none of the users are able to logon to the new box which i didn't change nothing. But if i reset the user... (1 Reply)
Discussion started by: lamoul
1 Replies

7. Shell Programming and Scripting

Expect passwd scripting

I am trying to write an expect script which will read information from a file that contains username and password, and change the password for each user accordingly. The list contains around 100 users. I am new to both Solaris and expect. I have successfully been able to set the first user's... (9 Replies)
Discussion started by: DoctorOctagon
9 Replies

8. Shell Programming and Scripting

Help with Expect tool Script

Problem Description: I have written the Expect script in Linux box, able to login from Linux to Windows and able to execute the command(eg, hostname) on windows server which produces some value. I want to pass this value from Windows to Linux box . Can we pass any parameter from Linux with expect... (2 Replies)
Discussion started by: khagendra
2 Replies

9. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

10. UNIX for Dummies Questions & Answers

'expect' tool

Can someone please provide a simple sample of syntax using the expect tool with an app. Let's say FTP. Maybe point me to where I may find some information on the syntax used. I'm not going to by a book on it. Thanks in advance! (2 Replies)
Discussion started by: shaggy
2 Replies
Login or Register to Ask a Question