Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-12-2019, 10:07 PM
kingzleshe kingzleshe is offline
Registered User
 
Join Date: Feb 2018
Posts: 16
automate multiple host

Hello

I am using RunCommandsOnMultipleHostsAndLogResults.py It worked perfectly.
But sometimes the remote system requires user input password after executing the command, then the script got stuck.

I've already put the password in ##commands.txt file as one line, It still not work. hopefully get some help soon. thanks.
Reply With Quote
  #2  
Old 07-14-2019, 01:22 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,937
Hi kingzleshe,

I don't fully understand the issue. Please elaborate.

What is the interaction with the remote where you run into problems?

If explaining this would reveal sensitive data, please send an email to support@vandyke.com and include Attn Brenda - Forum Thread #13624.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 07-15-2019, 06:23 AM
kingzleshe kingzleshe is offline
Registered User
 
Join Date: Feb 2018
Posts: 16
Hello

If I run command ‘ipmcset -d adduser -v sam01’ on the remote system to create a user account. The system would ask me to input password and the script will be suspended. I asked the manufacturer it was designed this way on purpose to prevent run script against the system.

I wonder using below action in the script would fix the problem, but I don't know how to exactly do it.

crt.Screen.WaitForString "Input your password:"
crt.Screen.Send "MyPassword"

(remote system cli)
iBMC:/->ipmcset -d adduser -v sam001
Input your password:
Reply With Quote
  #4  
Old 07-15-2019, 07:50 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,937
Hi kingzleshe,

Your approach seems reasonable.

Are you using the command-specific format for the hosts file as noted in the May 11, 2018 comment in the script?

# Last Modified: 11 May, 2018
# - Add support for specifying a unique command file specific to each host.
# To take advantage of this feature, your hosts.txt file should have this
# format:
# ---------------------------------------------------------------------
# hostname1;commandfileA.txt
#etc.

If this is one command in a file with many other commands, you will have to add code to the script so it is recognized as a special case using an if statement:

Here's some pseudocode:
if the command sent is "special command"
- wait for "Input your password"
- send "mypassword"
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 07-15-2019, 08:52 AM
kingzleshe kingzleshe is offline
Registered User
 
Join Date: Feb 2018
Posts: 16
Hi Brenda

1、I am not using command-specific format for the hosts file and do I have to?
2、I am not capable of add code in the script. could you give me some guidance?such as add what code and where?
3、Please treat me as a noob
Reply With Quote
  #6  
Old 07-15-2019, 09:07 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,937
Hi kingzleshe,

Replace line 482 in the original example Python script:
Code:
                        strResult = g_objNewTab.Screen.ReadString(strPrompt)
With this:
Code:
                        strResult = g_objNewTab.Screen.ReadString([strPrompt, "Input your password:"])
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730

Last edited by bgagnon; 07-15-2019 at 09:37 AM. Reason: added brackets for python
Reply With Quote
  #7  
Old 07-15-2019, 09:45 AM
kingzleshe kingzleshe is offline
Registered User
 
Join Date: Feb 2018
Posts: 16
Hi Brenda

I should be more specific. let's say I have a couple of commands in ##commands.txt file which is command[1],command[2],command[3]

command[1] and command[2] is just normal command the system wouldn't ask for user input password. But command[3] is required.

like you said before
if the command sent is "command[3]"
- wait for "Input your password"
- send "mypassword"
- wait for "password"
- send "NewUserpassword"
- wait for "confirm password"
- send "NewUserpassword"

maybe we should do some change between line 441 to 466, I am not entirely sure
Reply With Quote
  #8  
Old 07-15-2019, 10:43 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,937
Hi kingzleshe,

You might be able to comment out line 439 and get this working:
Code:
                        # Wait for the command to be echo'd back to us.
                        # g_objNewTab.Screen.WaitForString(strCommand)
However, this script is probably not appropriate for creating new users on the remote system because the interaction is so different and much more involved. You might want to create users in a separate script and then when the accounts are all setup, you can use this example script to run commands.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #9  
Old 07-15-2019, 11:22 AM
kingzleshe kingzleshe is offline
Registered User
 
Join Date: Feb 2018
Posts: 16
Hi Brenda

Yes, you are right I should use a separate script.
I have a simple python script on Unix platform can achieve this.

Since I no longer have Unix platform to use , I am looking Other Methods on windows.
Can SecureCRT run this kind of script?

Code:
import paramiko
import time

username = "Administrator"
password = "Admin@9000"

for n in range (10,20):
  ip_address = "192.168.1." + str(n)

  ssh_client = paramiko.SSHClient()
  ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh_client.connect(hostname=ip_address,username=username,password=password)

  print "Successful connection", ip_address

  remote_connection = ssh_client.invoke_shell()

  remote_connection.send("ipmcset -d adduser -v ydadmin\n")
  time.sleep(2)
  remote_connection.send("Admin@9000\n")
  time.sleep(1)
  remote_connection.send("Pr@ject2018\n")
  time.sleep(1)
  remote_connection.send("Pr@ject2018\n")
  time.sleep(1)
  remote_connection.send("ipmcset -d privilege -v ydadmin 3\n")
  time.sleep(1)
  remote_connection.send("Admin@9000\n")
  time.sleep(2)

  output = remote_connection.recv(65535)
  print output

  ssh_client.close
  time.sleep(2)
Reply With Quote
  #10  
Old 07-15-2019, 11:48 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,937
Hi kingzleshe,

No, but you can likely accomplish the same thing with VBScript or a Python script using SecureCRT objects and functions.

We don't write custom scripts so I would like to see you at least attempt to convert that on your own using what's available in SecureCRT. All the objects, methods and properties are documented in the Help file under the Scripting / Script Objects Reference topics. Examples are given in both VBScript and Python. I will assume you are going to use Python since that's what you referenced earlier.

For example, the equivalent of this:
Code:
  ssh_client = paramiko.SSHClient()
  ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh_client.connect(hostname=ip_address,username=username,password=password)
To the best of my knowledge, would be:
Code:
strCommand = crt.Session.Connect("/SSH2 /ACCEPTHOSTKEYS /PASSWORD p4$$w0rd user@ip_address")
The arguments available to Connect() and ConnectinTab() are the same as would be used if connecting via the command-line and are documented in the Help topic Using the Software / Command-Line Options. (The first three subtopics in that folder relate to command-line/Connect() calls.)


This pattern:
Code:
  remote_connection.send("ipmcset -d adduser -v ydadmin\n")
  time.sleep(2)
Can be replaced by a Send()/WaitForString() construct similar to what you posted earlier, but you need to use Python syntax (if that's what you have chosen):
Code:
crt.Screen.Send("command_here\r")
crt.Screen.WaitForString("prompt")

A print command can be replaced with crt.Dialog.MessageBox() if you want to display the result or you could log the info to a file.


I am not sure what this line does:
Code:
output = remote_connection.recv(65535)
The number listed is equivalent to a default system buffer size so I am not sure it's even relevant to the task you are trying to accomplish.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730

Last edited by bgagnon; 07-15-2019 at 11:48 AM. Reason: clarified
Reply With Quote
  #11  
Old 07-15-2019, 10:47 PM
kingzleshe kingzleshe is offline
Registered User
 
Join Date: Feb 2018
Posts: 16
Hi Brenda

Much appreciate about the tips. I'll do some research on it.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 12:32 PM.