Link to home
Start Free TrialLog in
Avatar of itreq
itreq

asked on

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
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

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?
Avatar of itreq
itreq

ASKER

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

Avatar of itreq

ASKER

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
Avatar of itreq

ASKER

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
Avatar of itreq

ASKER

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
Avatar of itreq

ASKER

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

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

Avatar of itreq

ASKER

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
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

Avatar of itreq

ASKER

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
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?
Bad link - try http:#24230824
Avatar of itreq

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itreq

ASKER

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

Avatar of itreq

ASKER

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