VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Secure Shell (https://forums.vandyke.com/forumdisplay.php?f=15)
-   -   SecureCRT – Command Line Options - Non-interactive command on the server (https://forums.vandyke.com/showthread.php?t=13431)

 akd 03-04-2019 01:30 PM

SecureCRT – Command Line Options - Non-interactive command on the server

After a non-interactive SSH log in to a remote server without any interactive prompting, can I execute a script on the remote server?

Using Plink (Putty), I can execute a remote script. For example, to execute a script backup.sh on a remote server(myhost.local), I can issue a command:

I am wondering if this is possible using SecureCRT SSH.

 ekoranyi 03-04-2019 02:57 PM

1 Attachment(s)
Hi akd,

SecureCRT can absolutely run commands on the remote system. Because Plink is a command line only utility and SecureCRT is a GUI application there will some variance in how this is done.

One option would be to use SecureCRT's Remote command functionality.
1. Create and save a session to the remote server
2. Right click on the saved session and choose Properties
3. Select the Logon Actions category
4. Enable the Remote command option
5. Enter the command you would like to run on the remote system

https://forums.vandyke.com/attachmen...1&d=1551736251

When connecting to this session, SecureCRT will automatically run the remote command. After the remote command completes SecureCRT will automatically disconnect the session. Based on your needs, there are additional configuration options available to automatically close the tab/application upon disconnect as well.

Does this help get you the behavior you're looking for?

 southca 04-30-2019 10:09 AM

How to script a remote command

Going back to the plink example:

How do I accomplish this in a script using SecureCRT.exe instead of plink? I have a list of hosts and do not want to create/save a separate session for each one.

 berdmann 04-30-2019 12:05 PM

Hi southca,

The general command line syntax for SecureCRT is:

Code:

 SecureCRT.exe [standard options] [protocol-specific options]
More specifically, if you want an "ad-hoc" connection to your remote host using a public key file you would use the following syntax:

Code:

SecureCRT.exe /SSH2 /I "C:\Path\to\identity\file" ubuntutest@myhost.local
Please review SecureCRT's Help for more on SecureCRT's command-line options.

 southca 04-30-2019 01:24 PM

What about the command to execute?

Your examples make a scripted connection to a host, but I also want to pass a command to execute. I see this is possible using a saved session with the "Remote command" option, and I'm looking for the command line equivalent without saving a session for every host.

I can make a scripted connection to a host using PowerShell as follows:
Code:

$ssh = "$env:ProgramFiles\VanDyke Software\Clients\SecureCRT.exe" $cred = Get-Credential -Message "SSH credentials"$user = $cred.GetNetworkCredential().UserName$pass = $cred.GetNetworkCredential().Password &$ssh /T /SSH2 /AUTH password /L "$user" /PASSWORD "$pass" myhost
Now let's say I want to extend the above connection command to execute "runme" on the target system. Ideally I could just update my existing command with something like this:
Code:

& $ssh /T /SSH2 /AUTH password /L "$user" /PASSWORD "$pass" /COMMAND "runme" myhost Unfortunately with SecureCRT there is no direct command line option to simply pass commands as arguments. I found the /SCRIPT option documented and managed to make it work by saving the following text as "myscript.vbs" Code: #$language = "VBScript" #$interface = "1.0" crt.Screen.Synchronous = True Sub Main crt.Screen.Send "runme" & chr(13) crt.Screen.Send "exit" & chr(13) End Sub With the above text block saved as a script file I can use it with my PowerShell command like this: Code: $script = "$env:USERPROFILE\Desktop\myscript.vbs" &$ssh /SCRIPT $script /SSH2 /AUTH password /L "$user" /PASSWORD "$pass" myhost Please let me know if there's a better way to accomplish this task. I've seen a number of similar questions in the support forums and it seems users are looking for simple OpenSSH client behavior where commands can be passed directly to the target host. Please consider adding this feature to your command line client. Thank you.  berdmann 04-30-2019 04:04 PM Hi southca, There is not currently a command line option that will allow you to send a command to the remote host. I have submitted a feature request on your behalf to implement such a command line option. Should a future release of SecureCRT include this functionality, notification will be posted here. If you prefer direct email notification, send an email to support@vandyke.com and include *"Feature Request - Forum Thread #13431"* in the subject line. If you are running the same command(s) on the remote hosts that you are connecting to, you might be interested in our "Run Commands On Multiple Hosts And Log Results" script example: https://forums.vandyke.com/showthread.php?t=10365 The script demonstrates how to connect to hosts read in from a "hosts" file and for each host, send a list of commands read in from a "commands" file.  southca 05-01-2019 08:45 AM Executing commands on a remote host from CLI I found it's possible to modify the VBS script to accept arguments, which adds some flexibility to the solution. Here's the modified version of "myscript.vbs": Code: #$language = "VBScript" #$interface = "1.0" ' SecureCRT script executes a single command on a connected session and exits crt.Screen.Synchronous = True Sub Main crt.Screen.Send crt.Arguments(0) & chr(13) crt.Screen.Send "exit" & chr(13) End Sub Here's the PowerShell to pass an argument to the above script that will ultimately be executed on the target SSH host, making it easy to run any command: Code: $ssh  = "$env:ProgramFiles\VanDyke Software\Clients\SecureCRT.exe"$cred = Get-Credential -Message "SSH credentials" $user =$cred.GetNetworkCredential().UserName $pass =$cred.GetNetworkCredential().Password $script = "$env:USERPROFILE\Desktop\myscript.vbs" $command = "runme" &$ssh /SCRIPT $script /ARG$command /SSH2 /AUTH password /L "$user" /PASSWORD "$pass" myhost
While this works, it's much more complicated than it needs to be. If/when the command line option is implemented the extra VBScript won't be needed. Thanks for submitting the feature request.

 All times are GMT -6. The time now is 04:21 AM.