Plink and powershell after successful login to server, enter command 1 and enter password after command 1

enter image description here

Newbie with coding, but doing my best, I believe I’m close.

I’m able to successfully log into the server via plink and powershell, but I’m having issues passing further commands after the initial server login. Command 1 requires a password to be entered after before I can actually pass command 3 and 4.

Anything after the last line of code -pw $password | this is where I start having issues. I get the prompt for the second password, but i’m unable to pass the password to the terminal window however i’ve tried.

I have attempted send keys method here to at least press the enter key in the terminal, but neither have worked for me.

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('windows powershell.exe')
Sleep 1
$wshell.SendKeys('~')

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

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');

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

  # Import the encrypted password from file
    $securePassword = Import-Clixml -Path "C:\Users\SecurePass.xml"
    
    
    # Import the encrypted username from file
    $secureUsername = Import-Clixml -Path "C:\Users\SecureUsername.xml"
    
    # Convert the secure password to plaintext
    $password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword))
    $username = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureusername))
    $command1 = "mysql -u -p"
    $command2 = $password
    $command3 = "something goes here"
    $command4 = "something goes here"
    
    
    $servername="8.8.8.8"
    $plinkpath="C:\Program Files\PuTTY\"
    
    
    #Execute SSH command
    &($plinkpath + "plink.exe") -v -batch $username@$servername -pw $password "$command1;$command2;$command3"


`-v` output

>     Access granted
>     Opening main session channel
>     Opened main channel
>     Allocated pty
>     Started a shell/command
>     Welcome to Pop!_OS 19.10


    Last login: Thu Feb  1 12:09:32 2024 from myipaddress

  • stackoverflow.com/q/46838702/850848

    – 

  • @MartinPrikryl I see your point, I have used this in the past to manually enter keys into a putty window and it works fine. I still cannot get it to press the enter key into the powershell window to show the password failed (to test). I have tried both methods of send keys in the answer from the thread you listed.

    – 

Leave a Comment