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.
Related
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 successfully managed to connect to a Cisco IE-2000-L switch via SSH. I used the Renci SSH.NET library.
Starting guide: http://vwiki.co.uk/SSH_Client_(PowerShell)
My working code is
# Load SSH library (for .NET 4.0 and PowerShell 3)
$DllPath = "D:\temp\Renci.SshNet.dll"
[void][reflection.assembly]::LoadFrom( (Resolve-Path $DllPath) )
# Connect to switch (Cisco IE2000-L) with IP, port, username, password
$SshClient = New-Object Renci.SshNet.SshClient('172.20.91.30', 22, 'admin', 'mypassword')
$SshClient.Connect()
# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')
# show result
$SshCommand.Result
# close SSH connection
$SshCommand.Dispose()
$SshClient.Disconnect()
$SshClient.Dispose()
My problem is
The above code sends just one command. But I want to execute several commands consecutively without closing and reopening a session.
If I add a second command right after the first one
# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')
$SshCommand = $SshClient.RunCommand('show start')
...the script hangs and never finishes. What am I doing wrong?
Minor relevant information
My main goal is to send multiple commands at once to a Cisco switch
I already tried Plink together with batch cmd input. It's not reliable enough. It works sometimes and sometimes not.
I already tried telnet scripting. Too awkward.
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.
When I try to configure remote kernels in mathematica via Evaluation>Parallel Kernel Configuration ... then I go to "Remote Kernels" and add hosts. After that I try to Launch the remote kernels and only some of them get launched (the number of them varies). And I get a msg like the following.
KernelObject::rdead: Subkernel connected through remote[nodo2] appears
dead. >> LinkConnect::linkc: Unable to connect to
LinkObject[36154#192.168.1.104,49648#192.168.1.104,38,12]. >>
General::stop: Further output of LinkConnect::linkc will be suppressed
during this calculation. >>
Any ideas how to get this working?
Take into account it sometimes does load some of the remote kernels but never all of them. Thanks in advance.
This is my ouput for $ConfiguredKernels // InputForm
{SubKernels`LocalKernels`LocalMachine[4],
SubKernels`RemoteKernels`RemoteMachine["nodo2", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo1", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo3", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo4", 2],
SubKernels`RemoteKernels`RemoteMachine["nodo5", 2]}
Once it did load all of the kernels, but it commonly doesn't, just one or two remote kernels.
There is very little information given, so this answer may not be 100% useful.
The first issue to always consider is licensing on the remote machine. If some kernels launch, but others don't, it is possible you have run out of licenses for kernels on that machine. The rest of this post will assume licensing is not the issue.
Connection Method
The remote kernel interface in Mathematica by default assumes the rsh protocol, which is not the right choice for many environments, because rsh is not a very secure protocol.
The other option is ssh, which is much more widely supported. There are many ssh clients, but I will focus on a client included with Mathematica, namely WolframSSH.jar. This client is java based, which has the added benefit of working the same on all platforms supported by Mathematica (Mac, Window and Linux).
To avoid having to type a password for every kernel connection, it is convenient to create a private/public key pair. The private key stays on your computer and the public key needs to be placed on the remote computer (usually in the .ssh folder of the remote home directory).
To generate a private/public key pair you can use the WolframSSHKeyGen.jar file, like so:
java -jar c:\path\to\mathematica\SystemFiles\Java\WolframSSHKeyGen.jar
and follow the instructions on the dialogs that come up. When done, copy the public key to the .ssh folder
on the remote machine. In my case, I called the keys kernel_key and kernel_key.pub was automatically named that way.
You can now test the connection from a command line, like so (using the ls command on the remote machine):
java -jar c:\path\to\mathematica\SystemFiles\Java\WolframSSH.jar --keyfile kernel_key arnoudb#machine.example.com ls
If this works, you should be able to finish on the Mathematica side of things.
Remote Kernel Connection
To make a connection you need the following settings, the name of the remote machine:
machine = "machine.example.com";
The login name, usually $UserName:
user = $UserName;
The ssh binary location:
ssh = FileNameJoin[{$InstallationDirectory, "SystemFiles", "Java", "WolframSSH.jar"}];
The private key as described above:
privatekey = "c:\\users\\arnoudb\\kernel_key";
The launch command for the kernel:
math = "math -mathlink -linkmode Connect `4` -linkname `2` -subkernel -noinit >& /dev/null &";
A configuration function to put everything together:
ConfigureKernel[machine_, user_, ssh_, privatekey_, math_, number_] :=
SubKernels`RemoteKernels`RemoteMachine[
machine,
"java -jar \"" <> ssh <> "\" --keyfile \"" <> privatekey <> "\" " <> user <> "#" <> machine <> " \"" <> math <> "\"", number]
This uses the configuration function and defines it to use 4 remote kernels:
remote = ConfigureKernel[machine, user, ssh, privatekey, math, 4]
This launches the kernels:
LaunchKernels[remote]
This command verifies if the kernels are all connected and remote:
ParallelEvaluate[$MachineName]
I am trying to do some basic scripting using ruby to log in to a windows machine via telnet and pull some files over using the dos command line ftp. When I do this manually everything goes swimmingly but when I try it via ruby I'm getting an error in the login call.
Here is my test program in its entirety:
require 'net/telnet'
tn = Net::Telnet::new("Host"=>"xxx.xxx.xxx.xxx", "Timeout"=>25, "Output_log"=>"output_log.log", "Dump_log"=> "dump_log.log", "Prompt"=>"C:.*>")
tn.login("administrator", "xxxxxxx") {}
tn.cmd('dir')
exit
The contents of output_log don't betray anything as being wrong:
Trying 208.10.202.187...
Connected to 208.10.202.187.
Welcome to Microsoft Telnet Service
login: administrator
password:
*===============================================================
Welcome to Microsoft Telnet Server.
*===============================================================
C:\Documents and Settings\Administrator>
Same for the dump_log which has very similar but awkwardly formatted contents. When I run the program it sits around for a while and then outputs the following error:
PS C:\code\tools\deployment> ruby test.rb
C:/Ruby/lib/ruby/1.8/net/telnet.rb:551:in `waitfor': timed out while waiting for more data (Timeout::Error)
from C:/Ruby/lib/ruby/1.8/net/telnet.rb:685:in `cmd'
from C:/Ruby/lib/ruby/1.8/net/telnet.rb:730:in `login'
from test.rb:3
Which leads me to suspect that the telnet class is not recognizing the command prompt. I've tried several different regex strings for the Prompt parameter, including the default and nothing seems to help.
I think the prompt field needs to be a regexp, not a string
Try
tn = Net::Telnet::new("Host"=>"xxx.xxx.xxx.xxx", "Timeout"=>25,
"Output_log"=>"output_log.log", "Dump_log"=> "dump_log.log",
"Prompt"=> /C:.*>/)