Jump to content

Changing linux password remotely using plink


Recommended Posts

I need to change user's passwords remotely for Linux devices. I was recommended using plink for achieving the same, but I am facing little issue while resetting the passwords.

Sharing the command using which I change password via cmd (working): 

echo 'y' | plink.exe admin@X.X.X.X -pw Old-password -no-antispoof "echo -e 'Old-password\nNew-password\nNew-password'|(passwd)" > passwdChange.log

When I integrated it with AutoIT, I embedded the above command in a batch file as below.

@echo off
echo 'y' | plink.exe %1@%2 -pw %3 -no-antispoof "echo -e '%3\n%4\n%4'|(passwd)" > passwdChange.log

When I try to execute the above batch file via AutoIT Run/ShellExecute command it doesn't work properly (doesn't change password).

$DOS = Run(@ComSpec & " /c "&@ScriptDir &"\chpasswd.bat "&$sUser&" "&$sIP&" "&$sOldPwd&" "&$pwd, "", @SW_SHOWMAXIMIZED, $STDERR_CHILD + $STDOUT_CHILD)

Where I am going & what changes I need to make in order for this to work

Thank you in advance.

Edited by Iraj
Link to comment
Share on other sites

  • Developers

First thing I always do when a Run @Comspec doesn't work is change the /c to /k so the cmd window stays open and I can see any errors.

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

50 minutes ago, Jos said:

First thing I always do when a Run @Comspec doesn't work is change the /c to /k so the cmd window stays open and I can see any errors.

 

when i run from cmd, it will proceed to change the password, but when i compile & run with autoit, it doesn't. Is the line ($DOS = Run(@ComSpec & " ....) correct?

Link to comment
Share on other sites

  • Developers
1 hour ago, Iraj said:

when i run from cmd, it will proceed to change the password, but when i compile & run with autoit, it doesn't. Is the line ($DOS = Run(@ComSpec & " ....) correct?

It is fully clear to me what you are doing, so did you try what I suggested as that is not clear from your answer?

If so, what was shown in the CMD window that remained open?

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

40 minutes ago, Jos said:

It is fully clear to me what you are doing, so did you try what I suggested as that is not clear from your answer?

If so, what was shown in the CMD window that remained open?

yes, I ran, it showed me a blank prompt.

Link to comment
Share on other sites

  • Developers

When you run manually, do you include the full path as well? 

Maybe that needs to be quoted when the full path contains spaces?

While testing please get rid of the last parameter ($STDERR_CHILD + $STDOUT_CHILD) to ensure output is shown in the actual console and keep using the /k.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

taken from some other script to solve a similar task:

#include <AutoItConstants.au3>

$PlinkDir='"C:\Program Files\PuTTY"'
$PlinkExe='"C:\Program Files\PuTTY\plink.exe"'

$CmdLine=$PlinkExe & " root@your.host.fqdn"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $CmdLine = ' & $CmdLine & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$pid=Run($Cmdline,@TempDir,@SW_SHOW,$STDERR_MERGED + $STDIN_CHILD)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $pid = ' & $pid & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Sleep(4000)

$StrRead=""
while ProcessExists ($pid)
    $NxRead=StdoutRead($pid)
    if $NxRead <> "" Then
        $StrRead &=$NxRead
        ConsoleWrite($NxRead)
    EndIf
    ; analyze the STRING for interactive password change, if the right time has come, send your input. Finally EXIT, terminate your strings with @CRLF
    $Wait4String="password:"
    if StringRight($StrRead,StringLen($Wait4String)) = $Wait4String Then
    StdinWrite($pid,$Str2Send & @CRLF)
    ConsoleWrite("Written: """ & $Str2Send & """"  & @CRLF)
    EndIf
    Sleep(100)
WEnd

$NxRead=StdoutRead($pid)
$StrRead&=$NxRead
StdioClose($pid)

ConsoleWrite("------------------------" & @CRLF)
ConsoleWrite($StrRead & @CRLF)
ConsoleWrite("------------------------" & @CRLF)

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

18 hours ago, rudi said:

taken from some other script to solve a similar task:

#include <AutoItConstants.au3>

$PlinkDir='"C:\Program Files\PuTTY"'
$PlinkExe='"C:\Program Files\PuTTY\plink.exe"'

$CmdLine=$PlinkExe & " root@your.host.fqdn"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $CmdLine = ' & $CmdLine & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$pid=Run($Cmdline,@TempDir,@SW_SHOW,$STDERR_MERGED + $STDIN_CHILD)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $pid = ' & $pid & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Sleep(4000)

$StrRead=""
while ProcessExists ($pid)
    $NxRead=StdoutRead($pid)
    if $NxRead <> "" Then
        $StrRead &=$NxRead
        ConsoleWrite($NxRead)
    EndIf
    ; analyze the STRING for interactive password change, if the right time has come, send your input. Finally EXIT, terminate your strings with @CRLF
    $Wait4String="password:"
    if StringRight($StrRead,StringLen($Wait4String)) = $Wait4String Then
    StdinWrite($pid,$Str2Send & @CRLF)
    ConsoleWrite("Written: """ & $Str2Send & """"  & @CRLF)
    EndIf
    Sleep(100)
WEnd

$NxRead=StdoutRead($pid)
$StrRead&=$NxRead
StdioClose($pid)

ConsoleWrite("------------------------" & @CRLF)
ConsoleWrite($StrRead & @CRLF)
ConsoleWrite("------------------------" & @CRLF)

 

Sorry, I may be wrong, but can you explain how to run this? I ran it & it doesn't take the password I have provided on the right output screen. 

 

Capture.PNG

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...