I have a windows exe which displays some copyright information, connects to a server and then prompts for input of username and password.
So whenever I run this, I have to wait for the prompt to input username and password.
I generally type ahead the username, , password after the command starts displaying copyright information, but that is a crude way.
Is there a better way to pass parameters to a input prompt of windows command, in a batch file, so that i can avoid typing them always?
P.S. In Linux we do this using the << operator like this
linux_command <<delimiter
inputparamvalue1
inputparamvalue2
delimiter
linux_command will run and on first input prompt, it reads inputparamvalue1 and at next input prompt, it reads inputparamvalue2
In response to James, thanks for the advice on security. I will take care of that.
I tried to implement your solution but that did not work.
These are my files. Please see if you get any hint.
vpn.bat
# echo off
"C:\Program Files (x86)\Cisco\Cisco AnyConnect VPN Client\vpncli.exe" connect "mycompanyvpnsite.com"
login.txt
myusername
mypassword
startvpn.bat
#ECHO OFF
CALL vpn.bat < login.txt
ECHO I'm back!
Result
D:\>startvpn.bat
Cisco AnyConnect VPN Client (version 2.5.6005) .
Copyright (c) 2004 - 2010 Cisco Systems, Inc.
All Rights Reserved.
>> state: Disconnected
>> notice: Ready to connect.
>> registered with local VPN subsystem.
>> state: Disconnected
>> notice: Ready to connect.
VPN> >> contacting host (mycompanyvpnsite.com) for login information...
>> notice: Contacting mycompanyvpnsite.com.
VPN>
>> Please enter your username and password.
Username: [myusername] Password:
Username myusername which it is showing is cached from my previous manual logins. So it appears that this run did not take any values from the login.txt.
If I had executed my vpn.bat alone without any params, i get prompt like this
VPN>
>> Please enter your username and password.
Username: [myusername] <i press enter to take the cached value>
Password: ******** <i enter password and press enter>
and it connects.
The vpn client does not have privilege to specify password on command line, that's why I am trying this way.
i've got a working code! It's similar to James K's but with a few bugs hammered out. Once again, I want to reiterate that this is rather insecure because it exposes your password in an unencrypted setting. that being said:
'Anyconnect login script
Dim oShell
Set oShell = WScript.CreateObject("WScript.Shell")
' rather clunky way of selecting the correct directory - open to suggestion
oShell.Run "cmd /k cd %PROGRAMFILES(x86)%\Cisco & cd Cisco* & vpncli.exe connect MYCOMPANYVPN.COM"
' sleep of 100 neccessary or else it opens multiple cmd windows
Wscript.sleep 100
' activate correct window
oShell.AppActivate "Connect"
' optional ipsec Identifier used for multiple VPN profiles on the same server
'(if you don't have one, just comment this out)
oShell.SendKeys "IPSECID{ENTER}"
' Send Username and Password to Active Window.
oShell.SendKeys "USERNAME{ENTER}"
oShell.SendKeys "USERPASSSWORD{ENTER}"
oShell.SendKeys "exit{ENTER}"
Good luck!
Saving your username and password in a file to automate login processes is a very bad idea and invalidates any security that requiring usernames and passwords supplies because anyone who get's electronic or physical access to your computer will have access to your plain-text username and password.
That said, in DOS / Windows Command Prompt you will need an extra file containing your username and password like so:
LOGIN.TXT
MyUserName
MyPassword
Then pipe that file into your command like this:
#ECHO OFF
CALL Command_Or_Batch < login.txt
ECHO I'm back!
This will act just as if you were at the console and typed MyUserName[ENTER] MyPassword[ENTER].
login.txt is just a plain vanilla text file, and does not need the .txt extension, or any extension at all for that matter.
The CALL statement is only necessary when launching another batch file, if you want it to, when finished, continue executing in the original calling batch file. Using CALL makes no difference when calling an executable (anything other than a batch file).
EDIT: As per your comments, I believe that you need to do this to get it to work:
# echo off
"C:\Program Files (x86)\Cisco\Cisco AnyConnect VPN Client\vpncli.exe" connect "mycompanyvpnsite.com" < login.txt
In this case you redirect to your vpncli.exe, but I believe that this will have the side-effect of making vpncli.exe ONLY accept input from login.txt, and not from the keyboard at all until vpncli.exe exits. Meaning that you'll need to automate (store in login.txt) all the stuff you would normally need to type in. This should not be a problem unless you need to manually interact with vpncli.exe later on.
EDIT: Since vpncli.exe seems to be clearing the buffer before reading any keystrokes, I suggest that possibly using VBS might get you where you're wanting to go.
VPN.VBS
'VBScript Example
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "C:\Program Files (x86)\Cisco\Cisco AnyConnect VPN Client\vpncli.exe connect mycompanyvpnsite.com"
' You need to know the name of the window, It should be in the upper left corner
' I'm guessing that it will be the filename of the executible
WshShell.AppActivate "vpncli.exe"
' Send Username and Password to Active Window.
WshShell.SendKeys "MyUserName{ENTER}"
WshShell.SendKeys "MyPassword{ENTER}"
But I can't be sure. I've had problems testing this, though I'm just bouncing back and forth between batch files and VBS scripts.
Though I do know if you replace the WshShell.Run and WshShell.AppActivate with the following two lines that...
WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"
...notepad will open and the following two lines will appear:
MyUserName
MyPassword
If this doesn't work for you, I'm thinking about deleting this answer entirely since it only contains suggestions that haven't worked.
Related
I need to write a batch script which connects to a vpn automatically when username and password is saved somewhere (Ex: in a file). VPN client is openconnect which provides a CLI but the problem is user input needs to be provided interactively to the command in order for it to complete.
See the output below when I run :
openconnect <serverhostname>
OUTPUT
POST https://<serverhostname>/
Connected to <serverhostname>:443
SSL negotiation with <serverhostname>
Server certificate verify failed: signer not found
Certificate from VPN server "<serverhostname>" failed verification.
Reason: signer not found
To trust this server in future, perhaps add this to your command line:
--servercert pin-sha256:<somesha>
Enter 'yes' to accept, 'no' to abort; anything else to view:
So I basically have to type yes manually and press Enter (it also prompts for further input), this needs to be automated in a script. Also, it's worth noting that the output is suggesting to provide --server-cert option and I could do that but when it asks the password, there's no option for it.
I tried putting the input lines in a file and redirecting that to the stdin of the command (which did not work but same the method worked on zsh on linux)
openconnect <serverhostname> < inputfile.txt
I also tried piping to stdin of the command which also didn't work.
I think the particular command doesn't read from stdin but directly from the console somehow which I really don't know how, but I could find a bit of information about something called "CON" on cmd.
Any solution is highly appreciated.
I am currently trying to create a script that does a bit of remote scripting that triggers a simple messagebox to appear on the screen of a remote user to let a user on a domain network alert every other user that there is a threat in the building. I run the script that is supposed to send the warning to every other users but I get the above mentioned error.
Line: 9
Char: 2
Error: Permission denied
Code: 800A0046
I have tried locating the script in a public network share folder so all users can reach it and have highest permissions to reach it.
I have tested to make sure they can read the pclist file which is just a file with the computer name of every computer in the building that needs to alert.
Dim oFSO, oTSin, oController, oRemote, sComputer
Set oController = CreateObject ("WSHController")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTSin = oFSO.OpenTextFile("I:\Public\PCLIST.txt") 'List of PC's
Do Until oTSin.AtEndOfStream
sComputer = oTSin.ReadLine
'Location of script to make run below between ""
Set oRemote = oController.CreateScript("I:\Public\VBS\Alert.vbs", sComputer)
oRemote.Execute
WScript.Echo "Finished script on " & sComputer
Loop
oTSin.Close
WScript.Echo "Finished all computers"
As state above all I really want is just a remote messagebox and maybe a echo char 7 to play over every users computer from like a panic button script. If there is a better way someone please let me know.
https://www.repairwin.com/fixed-permission-denied-800a0046-windows-script-host/
This is a typical error when you try to run a program on a remote computer. It is due to the permissions you have on those machines. You must be administrator of all, even if you are fail many times.
What I do is to send through the Languard application a rar auto-run file to all the machines.
I explain the file you want to run on each machine I send it in rar format but auto-run. When it arrives at that machine the autorun opens giving way to the autorun of for example yours, Alert.vbs
I don't know how to send you here a file, or your Alert.vbs file in rar-exe format.
I am trying to connect to an SFTP server via software called WinSCP which is a Secure FTP client, I can script it to work by sending keys and doing it in a psuedo unattended mode, but the Server has to have a user logged in to send the keys, I need WinSCP to logon and transfer the files like a service where the console doesn't launch.
I have tried following the Tutorials on the
WinSCP website (for automated/unattended transfers but it gives me errors: Cannot created object, or cannot find library (from the dll file that I have associated with the COM)
The error I get when I run the following code is:
line 13, Char: 2
Could not created object named "WinSCP.SessionOptions"
Code: 80040154
Source: Wscipt.CreateObject
I should also probably mention that I get a similar error about line 21 or 22 about creating the Session object, when I remove the code on line 13 to see if it was the only issue
<job>
<reference object="WinSCP.Session"/>
<script language="VBScript">
Option Explicit
' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
With sessionOptions
.Protocol = Protocol_Sftp
.HostName = "host"
.UserName = "username"
.Password = "password"
End With
Dim session
Set session = WScript.CreateObject("WinSCP.Session")
' Connect and Get
session.Open sessionOptions
session.GetFiles("/Fromn/*.*", "C:\testFilesFrom").Check()
' Disconnect, clean up
session.Dispose
</script>
</job>
Has anyone had any experience scripting this kind of job where the server is not logged on and the script can run an SFTP session? Is there somethign I am doing wrong or is this not possible?
WinSCP was made for interactive use. Use PSCP for non-interactive jobs. And I'd strongly recommend using public key authentication instead of password authentication.
Hi I have created a file temp.txt with the the content as below
root
root123
ls -lrt
exit
and then I am excuting the below command in dos
c:\>telnet machine_ip < temp.txt
Output is
Welcome to the Microsoft Telnet client
Escape character is 'ctrl+]'
C:\> Telnet>
How to automate the telnet session in windows with this method. I cant use any other exe(putty.exe) or tool to do this. I have to use the available things in windows xp. Suggest me also if any other way is there.
I am not sure if you can pass the username and password to telnet program from a text file.
You can create a vbs script and use sendkeys to pass username and password.
Create a batch script which starts a telnet session of a box and then call the vbs script from the batch script using
CSCRIPT //NoLogo //B "C:\Path\to\Script>script.vbs"
Hope this helps!
So the boss asked for a spreadsheet that would show him all the computers in our enterprise and the users that are currently logged in.
I stopped asking "why?" and started with NBTSTAT. The <03> results were inconsistent. Then I tried NET CONFIG WORKSTATION, and finally PSLOGGEDON.EXE (SYSINTERNALS). These were good, but I'd have to find a way to pass the results of NET VIEW and format the output nicely for a csv.
But then I thought there must be a better way. 90% of our PCs are WinXP so I could use WMIC or maybe DSQuery. I'd rather isolate the command execution to the workstations in our AD Computers container and not touch our Servers.
Does anyone have any recommendations?
You could do an LDAP search to Active Directory (LDIFDE)
ldifde -m -f OUTPUT.LDF -b USERNAME DOMAINNAME * -s SERVERNAME -d "cn=users,DC=DOMAINNAME,DC=Microsoft,DC=Com" -r "(objectClass=user)"
searching users then computers by changing the objectclass.
You can also specify just the attributes you want at the end.
eg: ldifde -f e:\Exportuser.ldf -s DomainController -d "ou=user,ou=sitename,dc=uk" -p subtree -r "(objectClass=User)" -l givenname,sn,samaccountname,mail,dn
or use a .net script
PsExec.exe + PsLoggedOn.exe + batch file.
Step 1. Download PsExec.exe and put it in a folder.
Step 2. Put PsExec.exer and PsLoggedOn.exe in a folder together.
Step 3. Determine a network share.
Step 4. Create a text file called pc_list.txt with the results of NET VIEW pasted in it (minus the "\" - should be one computer name per line). Basically should be a text with the following:
computer1
computer2
computer3
computer4
10.1.1.1
10.2.1.3
10.3.1.4
etc.
You can use IP's or Computer names.
Step 5. Create a Batch file called PsExec.bat with the following code:
"location_of_PsExec.exe"\PsExec.exe #"location_of_text_file"\pc_list.txt -u domain\Admin_username -p password -n 60 -c -f "location_of_runPsLoggedOn.bat"\runPsLoggedOn.bat
Step 6. Create another batch file called runPsLoggedOn.bat with the following code:
#echo off
echo.%computername%>>"location_of_network_share"\userlist.txt
PsLoggedOn.exe -x>>"location_of_network_share"\userlist.txt
echo.>>"location_of_network_share"\userlist.txt
Step 7. Run the PsExec.bat
This is really probably not going to be the EXACT answer you're looking for, but this should at least get you pointed in the right direction...
I didn't test this out, but I use something similar to get other things done using PsExec.exe.
You would want to just write in the code to only output the domain\username to the file on your network share, (from PsLoggedOn.exe -x>>"location_of_network_share"\userlist.txt) because the output is like this:
Users logged on locally:
domain\user
No one is logged on via resource shares.
You also might want to find, as far as formatting goes, a way to output that username to a variable so that you could do the following:
echo.%computername% %variable_username_from_PsLoggedOn%>>"location_of_network_share"\userlist.txt
Anyhow, this should get you started on your way to getting this solved...
How about using Netscan from Softperfect?
This seems to give me what I want:
FOR /F "skip=3 delims=\ " %%A IN ('net view') DO WMIC /Node:"%%A" ComputerSystem Get UserName /Format:csv 2>nul | MORE /E +2 >> computer_user_list.csv
Note that there is a tabulator followed by a space after delims=\
Maybe someone else could use it.