Jump to content

Parsing a Variable and SSH


Recommended Posts

Hi folks. I've been using AutoIT for all of my past jobs but all the work I've done has been simple or at least easy to figure out by going off of the wellspring of knowledge on the internet. However, I am frustrated with my current challenges and decided to post for direct support. I am no where near the level of complexity as some of you can get so forgive my (hopefully) feeble challenges here.

1. I want a user to be able to get prompted a box where they can paste in a 14 character string. The string is always the same format, so thats something. However, I then want a function that will parse out that data into segments, skipping the 5th and 10th character. For example, if the user keys in 12345678901234, then the function will snip out "1234", "6789", "1234". I imagine thats an array function, but I cant seem to find specifics on it to be able to do this exactly.

2. I need to be able to SSH into a remote server. I've been doing a LOT of reading on here about SSH and all the posts I've read have been 2006-ish or such and I'm hoping that things have gotten easier since then. I've got plink (as it was recommened in numerous threads) but when it runs it's just a blank screen, regardless of how I format my command. Does anyone have a streamlined code for this anywhere?

Link to comment
Share on other sites

$string = "12345678901234"

$aString = stringsplit($string , "")

msgbox (0, '' ,'"' & $aString[1] & $aString [2] & $aString[3] & $aString[4] & '"' & ',' & _
               '"' & $aString[6] & $aString [7] & $aString[8] & $aString[9] & '"' & ',' & _
               '"' & $aString[11] & $aString [12] & $aString[13] & $aString[14] & '"')

..i., regexp

 

2) and for ssh does plink accept arguments like putty?

Local $iPID = Run('"C:\Program Files (x86)\Putty\putty.exe" -ssh root@HOST:22 -pw PASSWORD')

*knew i got that comment off the forums somewhere:  

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I really appreciate everyone's help. Even though I'm not where I want to be just yet, all of your suggestions are teaching me more as I try to implement them. 

Firstly @ Boththose: The $string code that you suggested returns an error saying there are too many Dimensions or such. HOWEVER, your ssh suggestion was EXACTLY what I needed. Thanks a BUNCH!

As for the initial string problem, Mikell, your code yeilds an array box that says "16" in it. Czardas, your code yeilds no output at all, so I tried a few methods but I can only get a msgbox with the output of "16" as well. Geez. I feel retarded.

Let me elaborate. The script is to convert MAC Addresses into a more common format. When copying addresses straight out of a CMTS a mac address is typically formatted like this: "0000.1111.2222" (14 char).  I need the format to be changed to "00:00:11:11:22:22"

I thought something as simple like this (taken off the main AutoIT site) would be the key, modified for my own purposes of course:

#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sText = "This\nline\ncontains\nC-style breaks." ; Define a variable with a string of text.
    Local $aArray = StringSplit($sText, '\n', $STR_ENTIRESPLIT) ; Pass the variable to StringSplit and using the delimeter "\n".
    ; Note that flag paramter is set to $STR_ENTIRESPLIT (1) as it would split at \ or n otherwise.
    #cs
        The array returned will contain the following values:
        $aArray[1] = "This"
        $aArray[2] = "line"
        ...
        $aArray[4] = "C-style breaks."
    #ce

    For $i = 1 To $aArray[0] ; Loop through the array returned by StringSplit to display the individual values.
        MsgBox($MB_SYSTEMMODAL, "", "$aArray[" & $i & "] - " & $aArray[$i])
    Next
EndFunc   ;==>Example

But the program doesn't like these "include statements" or the "STR_ENTIRESPLIT" option. 

Link to comment
Share on other sites

I can't imagine how you get nothing. You should get an array displayed as I do. To access the elements within the array you need to append the number of the element, between square brackets, as shown below. I have no idea where the number 16 comes from. If your AutoIt version is older than the current version, then it will not recognize MsgBoxConstants.au3. You should check you have the latest stable release version of AutoIt. The following code should work on both the current and previous AutoIt versions.

;

#include <Array.au3> ; For _ArrayDisplay function

Local $sString = "123456789012348"
Local $aRegExp = StringRegExp($sString, "(.{1,4}).?", 3) ; OR ==> StringRegExp($sString, "(.{4}).?", 3)

MsgBox(0, "Array Values", $aRegExp[0] & @CRLF & $aRegExp[1] & @CRLF & $aRegExp[2]) ; Does not require MsgBoxConstants.au3
_ArrayDisplay($aRegExp) ; This should display an array

;

Run the code as is and see if anything happens.

Edited by czardas
Link to comment
Share on other sites

if you copy my code exactly as pasted it throws an error?  

or did you decide to enter your own string, of less than 14 characters, and are learning about accessing elements that dont exist?

if you do an _arraydisplay($sArray) after the stringsplit can you see all the characters and their place in the array?

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Hmm. Interesting. I'm not sure what I fat-fingered initially, but now the code is giving me the anticipated result of 1234, 6789 and 1234. 

Using a bastardized form of your code, I was able to hit my final mark! YAY!

Func button5 ()
   $CPEMAC = GuiCTRLRead ($Input1)
   Local $aRegExp = StringRegExp($CPEMAC, "(.{1,4}).?", 3) ; OR ==> StringRegExp($sString, "(.{4}).?", 3)
   $Q1 = $aRegExp[0]
   $Q2 = $aRegExp[1]
   $Q3 = $aRegExp[2]
   Local $aRegExp2 = StringRegExp($Q1, "(.{1,2})?", 3)
   Local $aRegExp3 = StringRegExp($Q2, "(.{1,2})?", 3)
   Local $aRegExp4 = StringRegExp($Q3, "(.{1,2})?", 3)
   MsgBox(0, "Converted MAC", $aRegExp2[0] & ':' & $aRegExp2[1] & ':' & $aRegExp3[0] & ':' & $aRegExp3[1] & ':' & $aRegExp4[0] & ':' & $aRegExp4[1])
EndFunc

So, essentially I'm taking the 14 digit user entry and parsing out the 5th and 10th character, and segmenting the other 12 letters into groups of 4 using your code.

Then, through some trial and error, I figured out to assign each of those groups thier own variable so that a similar parsing can happen, thus reducing those 4 characters into 2 groups of 2 each.

Then finally, slam it all together in the format I needed. I'm sure there was an easier way to get here (lol) but the trip was filled with a great deal of personal growth, so Thanks a HEAP all!

Hmm... now to make the output on the msgbox be copyable...

Link to comment
Share on other sites

Look at ClipPut, so then all you have to do is paste what is on your clipboard.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Hmm. Interesting. I'm not sure what I fat-fingered initially, but now the code is giving me the anticipated result of 1234, 6789 and 1234. 

Using a bastardized form of your code, I was able to hit my final mark! YAY!

Func button5 ()
   $CPEMAC = GuiCTRLRead ($Input1)
   Local $aRegExp = StringRegExp($CPEMAC, "(.{1,4}).?", 3) ; OR ==> StringRegExp($sString, "(.{4}).?", 3)
   $Q1 = $aRegExp[0]
   $Q2 = $aRegExp[1]
   $Q3 = $aRegExp[2]
   Local $aRegExp2 = StringRegExp($Q1, "(.{1,2})?", 3)
   Local $aRegExp3 = StringRegExp($Q2, "(.{1,2})?", 3)
   Local $aRegExp4 = StringRegExp($Q3, "(.{1,2})?", 3)
   MsgBox(0, "Converted MAC", $aRegExp2[0] & ':' & $aRegExp2[1] & ':' & $aRegExp3[0] & ':' & $aRegExp3[1] & ':' & $aRegExp4[0] & ':' & $aRegExp4[1])
EndFunc

So, essentially I'm taking the 14 digit user entry and parsing out the 5th and 10th character, and segmenting the other 12 letters into groups of 4 using your code.

Then, through some trial and error, I figured out to assign each of those groups thier own variable so that a similar parsing can happen, thus reducing those 4 characters into 2 groups of 2 each.

Then finally, slam it all together in the format I needed. I'm sure there was an easier way to get here (lol) but the trip was filled with a great deal of personal growth, so Thanks a HEAP all!

Hmm... now to make the output on the msgbox be copyable...

 

Your function is missing a return value. The following code does what you want with one regular expression. I imagine it can also be done with fewer lines of code, but read the comments to get a better idea of what is going on.

;

Global $sString = "12345678901234"

Global $sMAC = _Convert() ; Declare and assign a variable in a single line of code
MsgBox(0, "Converted MAC", $sMAC) ; Display the results

Func _Convert()
    Local $aRegExp = StringRegExp($sString, "(.{2})(.{2}).?", 3)
    Local $sMAC = "" ; Start with an empty string to concatenate
    
    ; Loop through the array to get the values of each element
    For $i = 0 To UBound($aRegExp) -1 ; Avoids going out of bounds
        $sMAC &= $aRegExp[$i] & ':' ; Concatenation
    Next
    $sMAC = StringTrimRight($sMAC, 1) ; Get rid of unwanted characters

    Return $sMAC ; Return the converted string
EndFunc

:

So your function could be written something like this:

:

Func button5 ()
    Local $CPEMAC = GuiCTRLRead ($Input1)
    Local $aRegExp = StringRegExp($CPEMAC, "(.{2})(.{2}).?", 3)
    Local $sMAC = ""
    
    For $i = 0 To UBound($aRegExp) -1
        $sMAC &= $aRegExp[$i] & ':'
    Next
    $sMAC = StringTrimRight($sMAC, 1)
    
    Return $sMAC
EndFunc

;

You can also use ClipPut() if you want the return value on the clipboard, as boththose suggested. Do it after calling the function, rather than from inside the function - unless you have reason to do otherwise.

Edited by czardas
Link to comment
Share on other sites

EXCELLENT! ClipPut was (once again) exactly what i needed for that.

I see the changes you made and I think I will try and incorporate that so that I have the experience of doing it for later, should a similar need arise. Still, much obliged!

I still have so much work ahead of to accomplish things I dont even know if they are possible. Before I post, however, I will make a conscious effort to learn.

Thanks againg everyone!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...