批处理文件运行提示输入密码的软件。我可以让批处理文件自动填写密码吗?

时间:2017-05-16 08:25:23

标签: windows batch-file password-prompt

我的问题不是PostgreSQL 9.6特有的,但PostgreSQL 9.6是我试图运行的软件,所以我会用它作为我的例子。我也在Windows 10上运行。

编辑:Magoo给出了一个非常PostgreSQL的具体答案。如果其他人可以给出一个更有效的答案,那么我将改变哪一个是那个答案。

我在批处理文件中运行以下代码:

@echo off
SET/P file=Select file:
@echo on
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file1]
"%~dp0\osm2pgsql-bin\osm2pgsql.exe" --slim --drop --latlong --keep-coastlines --multi-geometry --hstore -S [file2] --tag-transform-script [file3] -W -U gisuser -H [IP adress] -d [databasename] "%file%"
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file4]
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file5]
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file6]
@echo off
pause
@echo on

(括号内的所有内容都是硬编码值。)

psql.exeosm2pgsql.exe开始时,他们会提示输入密码。这本身很好,但随着它们运行多次,它会强制用户多次输入密码。 osm2pgsql也是一个需要很长时间的进程,因此用户必须等待两次输入密码。

我想要做的是提示用户输入一次密码,然后在提示输入密码时psql.exe和osm2​​pgsql.exe正在运行时自动输入该密码。

我尝试过这个但没有成功:https://stackoverflow.com/a/43896549/7398644

好奇的是,上面的代码应该在PostgreSQL数据库中的DROP CASCADE中有多个表(file1中的SQL代码),然后从OpenStreetMap数据中重新创建这些表,最后在同一个数据库中创建一些视图(文件4,5和6中的SQL代码)。

2 个答案:

答案 0 :(得分:0)

From the PostgreSQL page on command-line execution of psql.exe

-w --no-password

Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

Note that this option will remain set for the entire session, and so it affects uses of the meta-command \connect as well as the initial connection attempt.

So, I'd suggest a .pgpass file is the way to go...

答案 1 :(得分:0)

一种可能性是使用Autoit或Autohotkey等脚本语言。

  
    

>     AutoIt的

  

AutoIt v3是一种免费的类似BASIC的脚本语言,用于自动化Windows GUI和通用脚本。它使用模拟击键,鼠标移动和窗口/控制操作的组合,以便以其他语言(例如VBScript和SendKeys)不可能或不可靠的方式自动执行任务。 AutoIt也非常小,独立,可以在开箱即用的所有Windows版本上运行,不需要烦人的“运行时”!

  
    

>

  

您可以创建一个以批处理启动的脚本,并在后台等待特定的Windows对话框(密码对话框) 如果识别出对话框,您可以要求输入密码,将其存储在变量中并在所有后续对话调用中自动填写。