Executing Host Commands MIS 3406 Department of MIS Fox School of Business Temple University
Why Execute Host Commands Why would you want to execute host commands within code? Automate repetitive tasks Moving data Monitoring systems Backups DevOps Automated operations Automated testing Automated migration of code and data Dev QA Prod
What do you need to be able to do? Execute a command Capture and process the output Stdout – Stderr – Capture the “Exit Code” (a.k.a. “Return Code”) Conditional execution of code based on results of the host command
Execute a command at the command line… Arguments 1, 2 and 3 stdout
Let’s execute it within NodeJS… Setup .spawnSync stdout Command Arguments 1, 2 and 3 stderr Exit Code
How about a bad command… Setup .spawnSync stdout Command Arguments 1, 2 and 3 stderr Exit Code
OS Command (not a .exe) Command Arguments Need a Shell stdout stderr Exit Code
HostCommandWithExe.js Get your first program running that executes a host command with an exe.
HostCommandWithoutExe.js Get your first program running that executes a host command without an exe.
MyPing.js Write a script that is passed the name of a node to ping which then pings that node, captures the output. If the Exit Code is 0 displays the message “The node is reachable” and just the “Reply” line from stdout. If the Exit Code is 1 the script displays the message “The node is not reachable”.
MyDir.js Write a script that uses the “DIR” command to show all of the files with an extension of “.js” in the current directory and captures the output. Display just the file names of each “.js” file in the directory.
WhoAmI.js Write a script that executes the “set” command and captures the output. Display the user that is currently logged on by finding and parsing the “USERNAME=“ string in the output.
MyHostName.js Write a script that executes the hostname command and captures the output. Display the string, “The name of my computer is hostname” where hostname contains the name that was returned by the hostname command
MyIPAddress.js Write a script that executes the “IPCONFIG /ALL” command and captures the output. Display the string, “My IP address is xxx.xxx.xxx.xxx” where “xxx.xxx.xxx.xxx” is the IP address returned from the “IPCONFIG /ALL” command.
LookuoIPAddress.js Write a script that is passed the name of a server or website like www.temple.edu and uses the nslookup command to look up the IP address of this server or website. Display the IP address of this server or website so it looks like: The IP address of www.temple.edu is 155.247.166.60