Answer the question
In order to leave comments, you need to log in
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
system("./do.py ".$user." ".$new_password." ".$did->did);
Answer the question
In order to leave comments, you need to log in
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
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);
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question