I'm trying to get Excel VBA to run a list of commands on a Linux server.
I used the following:
Set objShell = CreateObject("WScript.Shell")
cmd = plink & " -ssh -P 22 " & ip & " -l " & username & " -i " & ppk & " -m " & commands_file & " -t "
Set oEx = objShell.Exec(cmd)
where commands_file contains the following:
sudo su - root
/opt/ibm/ccm/collectLogs.sh
When I run it the VB script appears a command line for a while and after it disappears without appearing to run the given instructions on commands_file.
Where is the error?
Please let me know.
I'd like to have a command window opened and see it run the collectLogs without it closing after finished.
Related
I have this code:
global logFilePath
Job("/Volumes/Work/test.log")
on Job(logFilePath)
-- more code
vlog("text")
-- more code
end Job
on vlog(x)
do shell script "echo \"" & (do shell script "date +' %H:%M:%S '") & x & "\" >> " & quoted form of logFilePath
end vlog
I keep getting error "The variable logFilePath is not defined." number -2753 from "logFilePath" from within the on vlog block.
Why? Haven't I declared it as global?
This works but is less elegant:
global logFilePath, logFilePath2
Job("/Volumes/Work/test.log")
on Job(logFilePath)
set logFilePath2 to logFilePath
-- more code
vlog("text")
-- more code
end Job
on vlog(x)
do shell script "echo \"" & (do shell script "date +' %H:%M:%S '") & x & "\" >> " & quoted form of logFilePath2
end vlog
The local variable logFilePath in the handler
on Job(logFilePath)
is not the same object as the global variable with the same name, therefore it's indeed not defined.
To make it clear use a different name
global logFilePath
Job("/Volumes/Work/test.log")
on Job(localPath)
set logFilePath to localPath
-- more code
vlog("text")
-- more code
end Job
on vlog(x)
do shell script "echo \"" & (do shell script "date +' %H:%M:%S '") & x & "\" >> " & quoted form of logFilePath
end vlog
Or only with local variables
Job("/Volumes/Work/test.log")
on Job(localPath)
-- more code
vlog("text", localPath)
-- more code
end Job
on vlog(x, logFilePath)
do shell script "echo \"" & (do shell script "date +' %H:%M:%S '") & x & "\" >> " & quoted form of logFilePath
end vlog
I want to open a cmd.exe and then execute a few lines of code.
I searched the web for some examples.
Code I tried modifying:
strToPrint = "Hello World!"
Shell "cmd.exe /K echo " & strToPrint, vbNormalFocus
I found How to write message to command window from VBA?
I tried multiple lines of coding, but the lines are executed in different command windows:
Sub CMD_VBA_Script()
Shell "cmd.exe /K echo Hello World!", vbNormalFocus
Shell "cmd.exe /K color 0a", vbNormalFocus
End Sub
I understand when I call the Shell two times, that it will execute two times.
My goal is to call the following script from VBA:
#echo off
title Matrix
color 0a
mode 1000
:a
echo %random%%random%
goto a
How can I execute multiple lines of code from VBA in command prompt?
MyFile = "C:\cmdcode.bat"
fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, "#echo off"
Print #fnum, "title Matrix"
Print #fnum, "color 0a"
Print #fnum, "mode 1000"
Print #fnum, ""
Print #fnum, ":a"
Print #fnum, "echo %random%%random%"
Print #fnum, "goto a"
Close #fnum
' Run bat-file:
Shell MyFile, vbNormalFocus
' optional, remove bat-file:
Kill "C:\cmdcode.bat"
So in short. You need to create a bat-file that you run.
If you don't need the bat-file after it's done you can delete it with Kill
You can write something like this -
Call Shell("cmd.exe /c "cd C:\Users\username\local\temp\" & " && temp.vbs" & " && mkdir newfolder")
This executes 3 lines of command:
Change directory
Execute a vbs file
Make a new folder
It looks like you want to execute a Command Prompt Batch file - batches are stateful, so simply executing each line separately is not going to have the same effect as executing the batch as a whole.
Two other alternative approaches involve faking batch execution by instructing an interactive instance of cmd.exe to execute commands as-they're-entered by some automated process: either sending window messages, or piping into the cmd process' stdin stream. I do not recommend either of these approaches because of their inherent flakiness (i.e. dependency on undocumented behavior)
So the best approach would be to just execute a batch file as it's intended - you would need to write the batch to a temporary file first, and then execute it:
Using the code from here: https://support.microsoft.com/en-us/kb/195763
Dim tempFileName As String
tempFileName = CreateTempFile("SomeBatch.cmd")
WriteToBatchFile( tempFileName ) ' you will have to write to the temp batch file yourself here
Shell "cmd.exe /c """ & tempFileName & """", vbHide, True ' Run the batch file, then cmd.exe will terminate. The Shell function will block until cmd is closed
Kill tempFile ' delete the temp batch file
Use "cmd.exe /K" option, and '&' to connect multiple commands.
In C:\Users\%USERNAME%, create test.bat with two simple commands:
echo %PATH%
dir
Here is the complete sample:
Sub Demo_Multi_Commands()
Cmd1 = "cd " & Environ("USERPROFILE")
Cmd2 = "test.bat"
Connector = " & "
Commands = "cmd.exe /K " & Cmd1 & Connector & Cmd2 & Connector & "systeminfo"
Debug.Print Commands
pid = Shell(Commands, vbNormalFocus)
End Sub
Environment: Windows 10 Enterprise, VBA 7.1
I'm ftping in to an Adrive.com account to upload a backup file. I do not know the version of linux that is installed. I tried to get the version with these commands:
ftp> uname -r
Invalid command.
ftp> cat /etc/*-release
Invalid command.
ftp> lsb_release -a
Invalid command.
ftp> cat /proc/version
Invalid command.
ftp> uname -a
Invalid command.
As you can see none of them worked.
Here is my script that I'm trying to run:
ECHO open ftp://xxx:xxxx#ftp.adrive.com>FTPSND.TXT
echo cd backup>>FTPSND.TXT
echo cd companyfiles>>FTPSND.TXT
echo mkdir %3>>FTPSND.TXT
echo cd %3>>FTPSND.TXT
echo option transfer binary>>FTPSND.TXT
ECHO send %1>>FTPSND.TXT
ECHO close>>FTPSND.TXT
ECHO exit>>FTPSND.TXT
winscp.com /script=ftpsnd.txt >>%2
And the error that is happening on the mkdir:
Error creating folder 'BEL'.
File or directory already exists.
(A)bort, (R)etry, (S)kip: Abort
My main problem is that if the directory does not exist, I need to create it, otherwise I skip that step and move on to the send.
Any ideas how I get around this?
EDIT
In response to Hackoo:
I failed to mention that this batch file is called from another batch file close to the end. When I used your code, I am not getting the file uploaded and the log shows:
ftp> Open ftp.adrive.com
Connected to ftp.adrive.com.
220 Username is your email address used for web interface
User (ftp.adrive.com:(none)):
331 Username OK, please send password.
230 Welcome xxxxx
ftp> prompt n
Interactive mode Off .
ftp> bin
200 TYPE changed to I.
ftp> MkDir /backup/companyfiles/IFC
550 File or directory already exists.
ftp> cd /backup/companyfiles/IFC
250 Changed directory OK.
ftp> mput D:\SSSAVES\sssbackup\TMPDIR\IFC_20160705141154.ZIP
200 PORT command OK.
150 Opening BINARY mode data connection for file IFC_20160705141154.ZIP.
It is not moving from this point and doing anything. I let it sit there for 2 hours just to make sure. The file is only 70mb in size so I doubt it would take that long.
EDIT #2
The file did eventually show up on Adrive, so I believe the ftp mput is working. It just doesn't seem to be releasing after it is sent
EDIT #3
I did change it from mput to put since I am only sending 1 file. That did not make a difference.
I created an account on ADrive.com and i created this script, just give a try
#echo off
Title Multiple file Upload by Hackoo on adrive
Color 0A
::****** Settings for FTP ************
Set FTPSERVER=ftp.adrive.com
Set USER=Your Login
Set Password=Your Password
Set LocalFolder=C:\Hackoo
Set RemoteFolder=/backupFolder
::************************************
::--- FTP commands below here ---
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo lcd %LocalFolder%
>> ft.do echo MkDir %RemoteFolder%
>> ft.do echo cd %RemoteFolder%
>> ft.do echo mput "*.*"
>> ft.do echo disconnect
>> ft.do echo bye
::************************************
ftp -s:ft.do
del ft.do
Pause
Can you try with this vbscript and tell me if it upload or not this file with 70Mb in size :
set objShell = CreateObject("WScript.Shell")
Login = "Your Username"
'If your username contains the # symbol, and your web browser does not support this, you can substitute for the +
Login = Replace(Login,"#","+")
Password = "Your Pass"
FTPSERVER = "ftp.server.com"
RemoteFolder = "RemoteFolderName"
FTPURL = "ftp://"& Login &":"& Password &"#"& FTPSERVER &"/"& RemoteFolder
Connect2FTP = objShell.run("Explorer "& FTPURL ,1,False)
I have done an old vbscript to deal like that
Description :
You are on a different computer than you, either with friends or in a cybercafe?
You want to add, modify, delete files or folders on your FTP server?
No chance, you do not have programs on hand as FTP clients like (FileZilla, CuteFTP, FlashFXP. Etc ...) to access your Private FTP server !
No problem => FTP Explorer is the solution to turn your web browser or windows explorer in your FTP client !
I'd just translate this old french version to english version !
Hope that can help you !
Titre = "FTP EXPLORER © Hackoo © 2016"
Set objIE = CreateObject( "InternetExplorer.Application" )
objIE.Navigate "about:blank"
objIE.Document.Title = Titre
objIE.ToolBar = False
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 320
objIE.Height = 390
ScreenWidth = objIE.document.ParentWindow.screen.width
ScreenHeight = objIE.document.ParentWindow.screen.height
objIE.Left = (ScreenWidth - objIE.Width ) \ 2
objIE.Top = (ScreenHeight - objIE.Height) \ 2
Do While objIE.Busy
WScript.Sleep 200
Loop
objIE.Document.Body.InnerHTML = "<div align=""center""><p><h3 style='color:Red'>UserName " _
& "<br><input type=""text"" style='color:Blue' size=""20"" " _
& "id=""Login"" value=""put your user name here""></h3></p>"_
& "</p><p><h3 style='color:Red'>Password<br><input type=""password"" style='color:Blue' value=""Put your password here"" size=""20"" " _
& "id=""Password""></h3></p><p><input type=" _
& """hidden"" id=""OK"" name=""OK"" value=""0"">" _
& "<h3 style='color:Red'>FTP Server " _
& "<br><input type=""text"" style='color:Blue' size=""20"" " _
& "id=""FTPSERVER"" value=""ftp.server.com""></h3>"_
& "<br><h3 style='color:Red'>Remote Folder "_
& "<br><input type=""text"" style='color:Blue' size=""20"" " _
& "id=""DossierDistant"" value=""/www""></h3></p>"_
& "<input type=""submit"" value="" Browse your FTP Folder"" " _
& "onclick=""VBScript:OK.Value=1""></p></div>"
objIE.Document.Body.Style.overflow = "auto"
objIE.Document.body.style.backgroundcolor="lightGreen"
objIE.Visible = True
objIE.Document.All.Password.Focus
On Error Resume Next
Do While objIE.Document.All.OK.Value = 0
WScript.Sleep 200
If Err Then
IELogin = Array( "", "" )
objIE.Quit
Set objIE = Nothing
wscript.quit
End if
Loop
On Error Goto 0
Set ws = CreateObject("wscript.Shell")
Login = objIE.Document.All.Login.Value
Login = Replace(Login,"#","+")'If your username contains the # symbol, and your web browser does not support this, you can substitute for the +
Password = objIE.Document.All.Password.Value
FTPSERVER = objIE.Document.All.FTPSERVER.Value
DossierDistant = objIE.Document.All.DossierDistant.Value
URL = "ftp://"&Login&":"&Password&"#"&FTPSERVER&"/"&DossierDistant
Connect2FTP = ws.run("Explorer "& URL ,1,False)
objIE.Quit
ws.Popup "Connecting to "&qq(FTPSERVER)&" is in progress ..........",3,"Connecting to "&qq(FTPSERVER)&" is in progress ..........",64
Set objIE = Nothing
Set ws = Nothing
Close("iexplore.exe")
'****************************************************
Sub Close(Process)
Set Ws = CreateObject("Wscript.Shell")
Command = "cmd /c Taskkill /F /IM "&Process&""
Execution = Ws.Run(Command,0,True)
End Sub
'****************************************************
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
'****************************************************
I want to open a cmd utility placed at C:\Users\123456\Desktop\Automation\Cucumber\ansi160\x64\ansicon.exe and execute below two DOS commands using VB script.
Commands,
1.CD C:\Users\123456\Desktop\Automation\Cucumber\ruby-1.9.3-p551-i386-mingw32\ruby-1.9.3-p551-i386-mingw32\bin 'change the directory
2.cucumber C:\Users\123456\Desktop\Automation\Cucumber\FeatureFiles\Lib_Max_Create_Lead.feature 'execute a script
Code i am using is -
Set owshell = CreateObject("Wscript.Shell")
cmd0 = "C:\Users\123456\Desktop\Automation\Cucumber\ansi160\x64\ansicon.exe" 'for switching the cmd window
cmd1 = "cd C:\Users\123456\Desktop\Automation\Cucumber\ruby-1.9.3-p551-i386-mingw32\ruby-1.9.3-p551-i386-mingw32\bin" 'for changing the directory to ruby set up
cmd2 = "cucumber C:\Users\123456\Desktop\Automation\Cucumber\FeatureFiles\Lib_Max_Create_Lead.feature" 'for executing a script
'Execute the command
owshell.Run "%comspec% /K pushd " & "&" & cmd0 & "&" & cmd1 & "&" & cmd2
When i execute the above statement from a VBS file,its swtiching the window using cmd0,but the second(cmd2) & third(cmd3) commands are not getting executed.Can some one help me to correct the mistake?
Thanks in Advance
Sona Shetty
I made, better I rummage together an AppleScript that results encrypted zip Files. It works except for Applications. If I expand the Archiv it is not an Application anymore. How can I compress valid Applications?
tell application "Finder"
set theItem to ((choose file) as alias)
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
display dialog "Enter password" default answer "" buttons {"OK"} default button 1
set thePassword to (text returned of result)
end tell
set cmd to "zip -P " & thePassword & " -rj " & zipFile & " " & itemPath & " -x *.DS_Store"
do shell script cmd
Check the -y option for zip. It may be what you need.
From iPhone: Compressing .app files in command line (Mac OS X) removes CodeSigning