2

I am trying to make a batch file which connects to my router via SSH and executes some commands. The batch file currently looks like:

ssh user@192.168.1.1
configure
etc.....

When I run the batch file the first line executes, and then the command window asks

user@192.168.1.1's password:

After inputting the password, the second line does not execute (I imagine this is obvious to someone who knows what they're doing)

I'm looking for a solution that either lets me manually enter the password and then lets the script continue, or one where the script enters the password itself.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Ben Lewis
  • 21
  • 1
  • 1
  • 2
  • 1
    Possible duplicate of [Automatically enter SSH password with script](https://stackoverflow.com/questions/12202587/automatically-enter-ssh-password-with-script) – MrSmile Jul 14 '18 at 19:47
  • How would the remote host (which is not running the script) know what commands are in the script? – SomethingDark Jul 14 '18 at 19:57
  • Also, that question may be a duplicate of the question in the title, but it is certainly not a duplicate of the question in the contents. – SomethingDark Jul 14 '18 at 19:58
  • Please [edit] your question to show the relevant parts of your script. I.e., how it invokes ssh and then invokes the commands on the remote system. – Kenster Jul 14 '18 at 21:43
  • Regarding how to avoid entering a password, see https://stackoverflow.com/a/43526842/13317 – Kenster Jul 14 '18 at 21:45
  • Your commands in a batch file go to CMD exe. You shell to another program (and it doesn't matter that is `ssh` it could be any console program), the batch file will wait for that program to exit. Good programs don't use StdIn for passwords, but to issue commands to console programs you are starting from `CMD` you redirect StdIn (or pipe) into the program. `Nslookup` is a program that runs like ssh. This pipes the exit command into nslookup `Echo exit | nslookup`. – CatCat Jul 14 '18 at 23:13

1 Answers1

0

Try this (doesn't need any 3rd party tools):

$env:TMPPW=Get-Content -Path 'secure_file.txt'
$j=Start-Job -ScriptBlock{Start-Sleep -Seconds 1;
(New-Object -ComObject wscript.shell).SendKeys("$env:TMPPW{ENTER}")}
Get-Content -Path 'MyScript.txt' | & ssh.exe -q -4 -l user 192.168.1.1
$env:TMPPW=([guid]::NewGuid()).Guid ; $env:TMPPW=$null
Phill
  • 11
  • 2