crackdonalds

ControlSend doesn't uppercase and special character

13 posts in this topic

Hello all,

I'm using constrolsend in my script and it works perfectly fine when i run it in a visable Windows session. If, however, I run the script as a scheduled task (thus not visible (which is what i want)) the script suddenly types 3 instead of #. Also $ becomes 4 and all uppercase characters are sent as lowercase.

I've tried the 1 and 0 flags and both give me the same problem.

I've even tried to get A by using {ASC 065} yet it is still sent as lowercase a.

What does work is {shiftdown}a{shiftup} but I don't want to use this because on my keyboard shift+4 = $ but on foreign keyboards this may be different and then the script will be useless.

 

If anyone has any clue on how to work around this, please let me know

Thanks

 

Share this post


Link to post
Share on other sites



Quote

{$}

{#}

{A}

Use curly braces

Share this post


Link to post
Share on other sites

ControlSetText, and you don't have to worry about it.

Sometimes the Shift key sticks, and then you don't send what you think you should.  The FAQ page describes how to release stuck keys.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

@InunoTaishou i've tried your suggestion but unfortunately i'm still getting lowercase characters and numbers instead of symbols.

 

jdelaney When i replace ControlSend with ControlSetText, no text is inserted at all anymore.

ControlSend($connection1 & " - PuTTY", "", "", "$")

Becomes:

ControlSetText($connection1 & " - PuTTY", "", "", "$")

 

Unfortunately that doesn't send any text.

 

Thanks

Share this post


Link to post
Share on other sites

Ah, it's a console window, right?  No real controls there to set.  You are actually just sending at the window, which happens to have the console area active.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

yea. just strange that it behaves differently in a invisible windows session than in a visible windows session: with/without scheduled windows task.

Can't really use ControlSetText :(

Share this post


Link to post
Share on other sites

yea that's why i went from send to controlsend. it misbehaving with the characters seems to be some sort of bug :S

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hi.

Instead of sending chars to PuTTY.EXE's GUI, use PLINK.EXE.

I guess, your intention might be quite similar to mine. This is a simplified script, I use to bulk backup the config of several Cisco switches.

 

Note:

the vtp config of Cisco switches is *NOT* included in copying the running-config --> backup / note separately!

 

#RequireAdmin ; required to be able to store the SSH keys to the registry, iirc
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Fileversion=2.0.0.1
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Language=2055
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

; Autoit & SciTE Version v3.3.14.2
; autoit wrapper from the beta pages: AutoIt3Wrapper.au3, dated 2016-02-27


#include <array.au3>
#include <constants.au3>
#include <Date.au3>


$plink = @TempDir & "\plink.exe"

FileInstall("plink.exe", $plink, 1)


$RegExHostCheck = "(?i)(.*Address or name of remote host \[\]\? $)"
$RegExDestFile = "(?i)(.*Destination filename \[)([a-z0-9_-]+-confg)(\]\? $)" ; $2 = vorgeschlagener Dateiname
$RegExTftpSucess = "(?i)(\d+ bytes copied in \d+\.\d+ secs \(\d+ bytes/sec\).*)"
$RexExStoreKey = "(?i)(.*Store key in cache\? \(y/n\) $)"
$Message = ''
$NextErr = ''
$HashNoticed = False


#Region TFTP Settings (INI) and folder
$CiscoBakBaseDir="C:\temp\CiscoBackups"

$tftpbaseDir = $CiscoBakBaseDir & "\" & TimeStamp2DirName()
DirCreate($tftpbaseDir)

$iniTFTPD32 = $CiscoBakBaseDir & "\tftpd32.ini"
if not FileExists($iniTFTPD32) Then
    ; disable DHCP: We don'w want bogus DHCP servers!
    $section="DHCP"
    $key="Lease_NumLeases"
    $val=0
    IniWrite($iniTFTPD32,$section,$key,$val)

    ; disable IP-v6
    $section="TFTPD32"
    $key="Enable IPv6"
    $val=0
    IniWrite($iniTFTPD32,$section,$key,$val)
EndIf

$tftpd32 = $CiscoBakBaseDir & "\tftpd32.exe"
If Not FileExists($tftpd32) Then
    MsgBox(0, "Error", "TFTPD32.exe not found:" & @CRLF & $tftpd32)
    Exit
EndIf
$section = "TFTPD32"
$key = "BaseDirectory"
IniWrite($iniTFTPD32, $section, $key, $tftpbaseDir) ; no error checking done here!

#Region Firewall Rule
$FW_ALLOW = 'netsh advfirewall firewall add rule name="tftpd32 do backup Cisco Switches" dir=in action=allow program="' & $tftpd32 & '" enable=yes  profile = any'
$FW_REMOVE = 'netsh advfirewall firewall del rule name="tftpd32 do backup Cisco Switches"'
RunWait($FW_REMOVE, @TempDir, @SW_HIDE)
Sleep(1000)
If RunWait($FW_ALLOW, @TempDir, @SW_HIDE) Then ; errorlevel is not zero ==> an error occured!
    ; ClipPut($FW_ALLOW)
    MsgBox(48, "Firewall Rule", "The firewall rule to allow tftpd32.exe (started from CiscoSaveConfig DIR) could *NOT* be created: " & @CRLF & _
            $FW_ALLOW & @CRLF & @CRLF & _
            "This line is in your clipboard now.")
    Exit
EndIf
#EndRegion Firewall Rule

While ProcessExists("tftpd32.exe") ; singleton would be better, this is ugly, I know
    ProcessClose("tftpd32.exe")
    Sleep(100)
WEnd
MsgBox(0, "Firewall", "Rule created:" & @CRLF & _
        $FW_ALLOW, 5)
$PIDtftpd32 = Run($tftpd32, $CiscoBakBaseDir) ; Base Dir *HAS* to be specified, as tftpd32 looks for its INI in "working directory" !!
Sleep(3000) ; wait for tftpd32 window, otherwise it will pop over the next input box
$TftpHost = InputBox("TFTPD32 IP Address", "IP Address of tftpd32.exe:", @IPAddress1)
#EndRegion TFTP Settings (INI) and folder

$U = InputBox("PLINK Backup Switches", "Username:", "")
$P = InputBox("PLINK Backup Switches", "Password:", "", "*")
$Cisco_IP = InputBox("IP Address", "Switch IP address?")

$HashNoticed = False

$Message = ''

$cmd = $plink & " " & $Cisco_IP & " -ssh -l " & $U & " -pw " & $P
ConsoleWrite($cmd & @CRLF)

$PID = Run($cmd, @TempDir, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED) ; it is important to use $STDERR_MERGED and *NOT* $STDOUT + STDERR, see help file for "run()"
ConsoleWrite($PID & @CRLF)

AdlibRegister("Message2Tooltip",100)

While ProcessExists($PID)
    $NextRead = StdoutRead($PID)
    $Message &= $NextRead
    ; ClipPut($Message)

    Sleep(100)
    If StringRight($Message, 1) = "#" And ($HashNoticed = False) Then ; your switches might show up with a ">", then an "enable" will be required first!
        $HashNoticed = True
        $CpRun2tftp = "copy running-config tftp://" & $TftpHost & "/" & @CRLF
        StdinWrite($PID, $CpRun2tftp)
    EndIf

    If StringRegExp($Message, $RexExStoreKey) Then
        StdinWrite($PID, "y" & @CRLF)
        MsgBox(0, "New SSH Key!", "Not cached in the Registry so far for " & $Cisco_IP, 10)
        Sleep(1000)
    EndIf

    If StringRegExp($Message, $RegExHostCheck) Then
        StdinWrite($PID, @CRLF)
    EndIf

    If StringRegExp($Message, $RegExDestFile) Then
        StdinWrite($PID, @CRLF)
        Sleep(3000)
    EndIf

    If StringRegExp($Message, $RegExTftpSucess) Then
        StdinWrite($PID, "exit" & @CRLF)
    EndIf
    Sleep(2000)
WEnd
AdlibUnRegister("Message2Tooltip")
ToolTip("")

MsgBox(0, "Done", "Please check, that all the switches have been saved!")

ProcessClose($PIDtftpd32)




Func Message2Tooltip()
    Local $x = MouseGetPos(0) + 30
    Local $y = MouseGetPos(1) + 30
    If $Message = '' Then
        ToolTip("<empty>", $x, $y, " $Message:")
    Else

        ToolTip($Message, $x, $y, "$Message:")
    EndIf
EndFunc   ;==>Message2Tooltip


Func TimeStamp2DirName()
    Local $foo = _NowCalc()
    $foo = StringReplace($foo, "/", "-")
    $foo = StringReplace($foo, " ", "_")
    $foo = StringReplace($foo, ":", "'")
    Return $foo
EndFunc   ;==>TimeStamp2DirName

 

ToDo:

Use scp instead of tftp, so that also the copy action is done encrypted. I'm on the move to get OpenSSH for Windows up and running as a service.

 

 

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Thanks @rudi that's a possible alternative. The purpose is almost the same. A tip for you: you can backup switches automatically with the archive command. there is no need to script cisco switches.

 

I found that {SHIFTDOWN}${SHIFTUP} works to give me a dollar symbol. Therefore do not have to worry about keyboard settings.

The problem is that i want to read a password from a variable $password

 

Is there anyone who knows how to get all uppercase and symbol characters out of a varialbe and add  {SHIFTDOWN} and {SHIFTUP} to them?
This doesn't seem to be easy.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Not sure, but maybe something like this ?

$var = "aBc$eFg"
$res = ""
$split = StringSplit($var, "")
For $i = 1 to $split[0]
    If StringIsUpper($split[$i]) or StringRegExp($split[$i], '\W') Then 
        $res &= "{SHIFTDOWN}" & $split[$i] & "{SHIFTUP}"
        ContinueLoop
    EndIf
    $res &= $split[$i]
Next
Msgbox(0,"", $res)

 

Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites
18 hours ago, mikell said:

Not sure, but maybe something like this ?

$var = "aBc$eFg"
$res = ""
$split = StringSplit($var, "")
For $i = 1 to $split[0]
    If StringIsUpper($split[$i]) or StringRegExp($split[$i], '\W') Then 
        $res &= "{SHIFTDOWN}" & $split[$i] & "{SHIFTUP}"
        ContinueLoop
    EndIf
    $res &= $split[$i]
Next
Msgbox(0,"", $res)

 

Thanks @mikell that's the last piece i needed. I did have to make a small adjustment:

$var = "aBc$eFg"
$res = ""
$split = StringSplit($var, "")
For $i = 1 to $split[0]
    If StringIsUpper($split[$i]) or StringRegExp($split[$i], '\W') Then
        $res &= "{SHIFTDOWN}{" & $split[$i] & "}{SHIFTUP}"
        ContinueLoop
    EndIf
    $res &= $split[$i]
Next
Msgbox(0,"", $res)

 

I've added an additional { and } so each character will be put down as {a}{b}. this because the "#" symbol only works when put betwee these brackets{}

 

Everything is working fine for me now.

Thanks guys.

Share this post


Link to post
Share on other sites

Nice  :)
My code was done to show the concept, you also can do the same treatment with a regex

$var = "aBc$eFg"
$res = StringRegExpReplace($var, '[[:upper:]\W]', "{SHIFTDOWN}{$0}{SHIFTUP}")
Msgbox(0,"", $res)

 

1 person likes this

Share this post


Link to post
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