[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

Help me convert VBS script to Linux/Darwin

Can someone help me convert a vbs script to linux?  I have a opensource application that currently supports vbs on windows only.   I am working with them to convert it to also support the Mac OS X, which runs on darwin.  This could also be configured to support Linux distro's as well.

I'm not sure if I am allowed to post the entrie script here or not.  It is a paid 3rd party addon and I have the rights to work with it and submit the results for future enhancement.  Anyone can get the code as a 14 day trial so I will post it here if the admins respond and let me know whether I can or not. Otherwise just email me at lschafroth at winterset.k12.ia.us and I can give you the code in the meantime.

This is basically a login script that will run at login and update the gateway what the machines name, ip and username is.  that's it.

Thanks!!

ITreq
0
itreq
Asked:
itreq
  • 10
  • 6
1 Solution
 
Duncan RoeSoftware DeveloperCommented:
The admins won't look at this page unless you ask them to. Click on "Request Attention" in the Question panel (near bottom Right on my screen - but you may be using a different skin).
BTW private email correspondence breaks EE rules - any chance you could put up the code on a web site?
0
 
itreqAuthor Commented:
I have receive permission to post it here.

I found some commands on the OS X side that return certain values:


username:  USERNAME=$USER
userdomain:
computername: COMPUTERNAME=$(hostname -s)
'Handle or Ignore all errors
On Error Resume Next
 
'Time in milliseconds to sleep between request
SLEEP_PERIOD = 300000
 
URL_PREFIX = "http"
 
If WScript.Arguments.Count = 1 Then
	ServerName = WScript.Arguments.Item(0)
Else
	ServerName = "192.168.1.1"
End If
 
'WScript.Echo "ServerName is:"
'WScript.Echo ServerName
 
Do While True
  Set AJAX = CreateObject("MSXML2.ServerXMLHTTP")
  Set wshShell = CreateObject("WScript.Shell")
  strUser = wshShell.ExpandEnvironmentStrings("%USERNAME%")
  strDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
  strHostname = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
  command = URL_PREFIX+"://"+ServerName+"/adpb/registration?username="+strUser+"&domain="+strDomain+"&hostname="+strHostname+"&action=login"
  'WScript.Echo command
  AJAX.Open "GET", command
  AJAX.Send ""
  WScript.sleep(SLEEP_PERIOD)
  AJAX.Abort 
  Set AJAX = nothing
Loop

Open in new window

0
 
itreqAuthor Commented:
At a minimum, can someone add comments to explain what each command it doing? Maybe that would help me understand the code and try and determine the darwin/linux command to match it.

ITreq
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
itreqAuthor Commented:
OK, I think I have a few figured out.

If WScript.Arguments.Count = 1 Then
        ServerName = WScript.Arguments.Item(0)
Else
        ServerName = "192.168.1.1"
End If

The script section above checks to see if the vbs file was ran with an argument.  If it was, then use the argument as the ServerName, otherwise default to 192.168.1.1?  Is this correct?

Lannie
0
 
itreqAuthor Commented:
This part is confusing me:

  'WScript.Echo command
  AJAX.Open "GET", command
  AJAX.Send ""

The whole purpose of this script is to update the server with the client information. If this is the case, wouldn't we want to use a POST command instead of a GET?  Or I wonder if the server can determine the information simply by seeing the GET function come through.

What is the function of the .end "" ?

Lannie
0
 
itreqAuthor Commented:
Here is my attempt at the code.  We dont use DOMAINs in workgroup manager so I am not sure what to use for this variable.  I have it hard coded for now until I get an answer from the active directory programmers. They are curently changing it to work with LDAP as well.


#!/usr/bin/env bash
 
### Bash skips errors and resumes by default
 
### Time in seconds to sleep between request
SLEEP_PERIOD=300
 
URL_PREFIX="http"
 
### Determine if different ip provided in command line
if [ "$1" != "" ]; then
    SERVERNAME=$1
else
    SERVERNAME="192.168.1.1"
fi
 
### echo "ServerName is:"
### echo $SERVERNAME
 
while true; do
  USERNAME=$USER
  DOMAIN="WCSD"
  COMPUTERNAME=$(hostname -s)
  URLCOMMAND=$URL_PREFIX"://"$SERVERNAME"/adpb/registration?username="$USERNAME"&domain="$DOMAIN"&hostname="$COMPUTERNAME"&action=login"
### echo $URLCOMMAND
### curl -f fails silently, -s silent mode with no progress status, -m maximum execution time allowed
  curl -f -s -m $SLEEP_PERIOD $URLCOMMAND
done

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
You want to translate this into a shell script, right?
I've never use AJAX, but from Ggogling it seems there may be an open-source alternative: http://sourceforge.net/projects/zk1/
Comments in shell scripts start with "#"
I have added comments to try to explain what is happening.
I have prefaced your original lines with "##" so they are now also comments. The lines I can't translate are prefixed "#!!"

For a better understanding of shell, on a Linux system type "man bash" (Linux sh is actually bash). May work on MAC too (or "man sh")

In answer to your 4th post, "command" is assembled earlier as something like

"http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login"

(guessing VBS syntax a bit ). I think the next 2 lines send this to the server and await its response.

When running the script as a test, I added "|head" to limit the amount of output (otherwise it runs forever)
14:49:48$ cat ee47.sh
#!/bin/sh
# Although it looks like a comment, you have to start scripts with this line.
# It tells the operating system that the file is to be interpreted by the /bin/sh program.
# Other interpreters are possible (e.g. /bin/bash -p, /usr/bin/perl &c.).
 
##'Handle or Ignore all errors
##On Error Resume Next
# This is the default. To be really sure, you could include "set +e" but no-one does
 
##'Time in milliseconds to sleep between request
##SLEEP_PERIOD = 300000
# Time in seconds to sleep between requests
SLEEP_PERIOD=0.3
# In shell variable assignment, there must not be any whitespace around the "="
# The shell sleep command takes its argument in seconds by default (see "man -s 1 sleep")
# Linux sleep accepts floating-point input but MAC may not
 
##URL_PREFIX = "http"
URL_PREFIX=http
# The shell would drop double quotes if present - they allow you to assign values containing spaces &c.
 
##If WScript.Arguments.Count = 1 Then
##        ServerName = WScript.Arguments.Item(0)
##Else
##        ServerName = "192.168.1.1"
##End If
if [ $# -eq 1 ];then
  ServerName="$1"
else
  ServerName=192.168.1.1
fi
# In shell, "$#" is the number of command-line arguments
 
##'WScript.Echo "ServerName is:"
##'WScript.Echo ServerName
echo "ServerName is: $ServerName"
# No longer a comment (for now)
 
##Do While True
while true; do
#!!  Set AJAX = CreateObject("MSXML2.ServerXMLHTTP")
#!!  Set wshShell = CreateObject("WScript.Shell")
##  strUser = wshShell.ExpandEnvironmentStrings("%USERNAME%")
##  strDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
##  strHostname = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
##  command = URL_PREFIX+"://"+ServerName+"/adpb/registration?username="+strUser+"&domain="+strDomain+"&hostname="+strHostname+"&action=login"
  strUser=$USER
  strDomain=$(hostname -d)
  strHostname=$(hostname -s)
  command="${URL_PREFIX}://${ServerName}/adpb/registration?username=${strUser}&domain=${strDomain}&hostname=${strHostname}&action=login"
# Putting braces round variable names protects against the following character being legal in a variable name
##  'WScript.Echo command
  echo $command
# No longer a comment (for now)
#!!  AJAX.Open "GET", command
#!!  AJAX.Send ""
##  WScript.sleep(SLEEP_PERIOD)
  sleep $SLEEP_PERIOD
#!!  AJAX.Abort 
#!!  Set AJAX = nothing
##Loop
done
14:49:59$ ./ee47.sh|head
ServerName is: 192.168.1.1
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://192.168.1.1/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
14:50:11$ ./ee47.sh 10.11.12.13|head
ServerName is: 10.11.12.13
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login
http://10.11.12.13/adpb/registration?username=dunc&domain=local.net&hostname=dimstar&action=login

Open in new window

0
 
itreqAuthor Commented:
Compared to your code it looks like I was close, but you have some better handling of the variables.

I will give this a test on Monday where the actual server is.  The primary goal is to make this work on a MAC computer first, and Linux as a bonus.  It has to work with the stock OS with no 3rd party software.  What you put together seems to be what we are looking for since MAC and Linux share bash as the default script.

Thanks!!! I will let you know how it goes.

Lannie
0
 
Duncan RoeSoftware DeveloperCommented:
Be aware - commands like "sleep" are not a part of bash but programs in their own right. MAC sleep may not accept a floating-point argument. Also, MAC's hostname may not accept -d - there may be a separate dnsdomainname command as there is on Linux, or hostname without -s may returned a fully qualified domain name (e.g.dimstar.local.net)  which you need to split somehow, as below.
I think everything else should be OK
strHostname=$(hostname|cut -d. -f2-)

Open in new window

0
 
itreqAuthor Commented:
The hostname -d does not work in MAC OS X.

Sleep does recognize floating-point arguments in Leopard.

The hostname -d returns just the computer name. It does not include the .local.

Your (duncan_roe) script also does not have a method of posting the command via http.  All yours is doing is echoing the command if I am reading it correctly.  It needs to simulate the GET / POST method in the original script.

Lannie
0
 
Duncan RoeSoftware DeveloperCommented:
Yes I thought you might have trouble there. You will have to look for a command that tells you the DNS domain name. Or you might be able to extract it from a system file. You asked this question in the Linux area and I gave you a Linux answer.
I am not familiar with AJAX and prefaced these lines with #!! because I could not translate them. Did you look at zk1 that I mentioned in http:#24230824?
0
 
Duncan RoeSoftware DeveloperCommented:
Bad link - try http:#24230824
0
 
itreqAuthor Commented:
I looked at his link but this has to work natively in the bash shell that comes with OSX.  I ran the following code and it was successfull, but it needs a way to check the curl command status. The do while true does not work.  However I have added a verbose command line option to curl and I get a HTTP 1.1 /200 OK status.

I need to figure out how to get the curl command or a variable set so I know when I get the 200 status.

So the only part of the script that needs fixed is the while true; do.  It needs replaced with a method of finding the result status of the curl command and know when to exit.

Lannie
#!/usr/bin/env bash
 
# Bash skips errors and resumes by default
 
# Time in seconds to sleep between request
SLEEP_PERIOD=300
 
# Lets define the protocol to be used.
URL_PREFIX="http"
 
# Determine if different ip provided in command line arguement
if [ $# -eq 1 ]; then
  SERVERNAME="$1"
else
  SERVERNAME="192.168.1.1"
fi
 
# Take out the comments below for testing the script
# echo "ServerName is:"
# echo $SERVERNAME
 
# Execute script until successful
while true; do
  strUser=$USER
  strDomain=$(domainname)
  strHostname=$(hostname -s)
  URLCOMMAND=${URL_PREFIX}"://"${SERVERNAME}"/adpb/registration?username="${strUser}"&domain="${strDomain}"&hostname="${strHostname}"&action=login"
 
# Take out the comments below for testing the urlcommand
# echo $URLCOMMAND
# curl arguments: -f fails silently, -s silent mode, -c connection limit in seconds, -m maximum execution time allowed
# curl -f -s -c 10 -m 10 $URLCOMMAND
curl -v $URLCOMMAND
sleep $SLEEP_PERIOD
done

Open in new window

0
 
Duncan RoeSoftware DeveloperCommented:
Can you post an example of running curl? Is the output helpful assuming you could capture it?
You could set a bash variable to the output:

var=$(curl -v $URLCOMMAND)

You could redirect the output to a temporary file:

tempfile=$(tempfile)
curl -v $URLCOMMAND >$tempfile

to additionally capture error mesages:

curl -v $URLCOMMAND >$tempfile 2>&1

You can then do whatever with the temp file. When finished, be sure to "rm $tempfile".

You can see if the command terminated successfully:

curl -v $URLCOMMAND >$tempfile 2>&1; result=$?
if [ $result -eq 0 ];then
# Commands on success
else
# Commands on failure
fi
0
 
itreqAuthor Commented:
I ended up converting it myself.  Here is the working code. Tested on the server in debug mode and it works well.

ITreq
#!/usr/bin/env bash
 
# Bash skips errors and resumes by default
 
# Time in seconds to sleep between request
SLEEP_PERIOD=300
 
# Lets define the protocol to be used.
URL_PREFIX="http"
 
# Determine if different ip provided in command line arguement
if [ $# -eq 1 ]; then
  SERVERNAME="$1"
else
  SERVERNAME="192.168.1.1"
fi
 
# Take out the comments below for testing the script
# echo "ServerName is:"
# echo $SERVERNAME
 
# Execute script until successful
while true; do
  strUser=$USER
  strDomain=$(domainname)
  strHostname=$(hostname -s)
  URLCOMMAND=${URL_PREFIX}"://"${SERVERNAME}"/adpb/registration?username="${strUser}"&domain="${strDomain}"&hostname="${strHostname}"&action=login"
 
# Take out the comments below for testing the urlcommand
# echo $URLCOMMAND
# curl arguments: -f fails silently, -s silent mode with no progress status, -m maximum execution time allowed
curl -f -s -m 10 $URLCOMMAND
sleep $SLEEP_PERIOD
done

Open in new window

0
 
itreqAuthor Commented:
Did not solve my questions since I did it myself but helped me go in the right direction!  Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now