A
A
Alexey Lebedev2012-10-25 13:56:52
linux
Alexey Lebedev, 2012-10-25 13:56:52

Connect via SSH and change password?

In general, there is such a task. There is an admin panel in which there is a functionality to change the user's password.
The idea is this: we call a PHP script that logs in via SSH to the server under root and changes the user's password. We know the root password.
From my predecessor I got this code:

#!/usr/bin/expect
set USER [lindex $argv 0]
set PASS [lindex $argv 1]
set HOST [lindex $argv 2]

spawn ssh [email protected]$HOST

expect "word:"
send "123456\r"
expect "$*"
send "echo \"$USER:$PASS\" | chpasswd  \r"
send "exit\r"
expect eof

Called with PHP like this:
system("./do.py ".$user." ".$new_password." ".$did->did);

The above code does not work, perhaps the problem is incorrect handling of expect.
And it seems to me that there is a more normal option for changing the password. Unfortunately, to put additional software and libraries on the server and clients can not. I can't upload anything to other PCs, even key files.
What is the best way to connect to the server and change the password?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stdit, 2012-10-25
@swanrnd

Theoretically, you can reduce the number of unnecessary entities if you pass the entire command in one line.
Something like this (slash passwords):

ssh [email protected] 'echo -e "password\npassword" | passwd username

A
Alexey, 2012-10-25
@alexxxst

Some wild shit...

$connection = ssh2_connect($server_host, 22);
if(ssh2_auth_password($connection, $login, $password)){
        $stdio = ssh2_exec($connection, $command);
        stream_set_blocking($stdio, 1);
}

Can you handle errors and write a $command?

B
BasilioCat, 2012-10-26
@BasilioCat

You can try replacing this expect script with the sshpass utility (you will have to install it separately)
sshpass -p password ssh [email protected] "echo user1:passwd1 | chpass"
for security reasons, it is better to transfer the password through a file or an environment variable (there is such a possibility)
Well, the correct method (which was recommended above) is to generate the
ssh-keygen key
and go through all the servers (since you know the root password), and enter this key to access via ssh
cat /root/.ssh/id_rsa.pub | ssh [email protected] "cat >> /root/.ssh/authorized_keys"
after which you will go to all servers without a password

M
mcleod095, 2012-10-25
@mcleod095

well, to change the password in sh scripts, I would not recommend using the passwd command
, it is better to use the chpasswd command
echo "user:password" | chpasswd

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question