The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Â
You have a windows server that has cygwin installed and sshd enabled.
You want to log in by using remote desktop to resolve an issue but usually the 3 licensed sessions that your company has purchased from Microsoft are being used by your colleagues.
Then you ssh into the box, that means that you only have access to the command line and your shell is the cygwin bash.
These boxes have a full cygwin installation and that means you have perl, python, ruby, awk , sed , etc ..
You can visit the cygwin portal to check what all comes with cygwin.
To trouble shoot the issue after careful consideration you realize that you need to create a tool that can show a process tree similar to what is shown by pstree.
Example:
$ pstree -aplc
?,1
├─bash,6008
└─cygrunsrv,516
└─sshd,912 -D
├─sshd,4672 -D -R
├─sshd,5556 -D -R
│ └─bash,4644
│ └─screen,4840 -DR MAIN
│ └─screen,3064 -DR MAIN
│ └─bash,5676
│ └─pstree,3892 -aplc
└─sshd,2264 -D -R
Â
Â
You don’t need the flags you just want to output a tree using text or drawing characters and you want to identify the hierarchy of processes using the process id and the parent process id.
You are also aware that the cygwin pstree only work for processes started in a cygwin shell but you want to have a similar tool that you can use for native windows processes.
You have at your disposal powershell.
And you know that you can invoke it from the cygwin shell like this:
Â
$ echo | powershell -ExecutionPolicy Unrestricted 'ps' | head
Â
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
186 13 17856 10176 87 244 amswmagt
187 8 7808 9528 433 0.09 4136 bash
189 8 7872 9524 435 0.03 4604 bash
185 9 9040 10920 435 1.79 5168 bash
182 8 7812 9480 433 0.08 5376 bash
193 8 7872 9528 435 0.08 5784 bash
197 9 7944 9708 435 0.67 6828 bash
Â
Â
That is fine but does not give you the process parent id.
Â
In google you find that you can access more information from the win32_process object.
Â
You figure out a powershell script that can give you the info you need (and notice that you have to call powershell within bash).
Â
$ echo | powershell -ExecutionPolicy Unrestricted 'Get-WmiObject win32_process -ComputerName localhost | foreach { [console]::out.writeline($_.ProcessName+"`t"+$_.ProcessId+"`t"+$_.parentprocessid+"`t"+$_.commandline) } ' | tail
Server.exe 5560 2668
Server.exe 20992 2668
Server.exe 7116 2668
Server.exe 7480 2668
Server.exe 7620 2668
Server.exe 4984 2668
LogonUI.exe 16916 2404
bash.exe 9392 6828 C:cygwinbinbash.exe
powershell.exe 3924 9392 C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -ExecutionPolicy Unrestricted "Get-WmiObject win32_process -ComputerName localhost | foreach { [console]::out.writeline($_.ProcessName+"`t"+$_.ProcessId+"`t"+$_.parentprocessid+"`t"+$_.commandline) } "
tail.exe 11472 19180 C:cygwinbintail.exe
Â
Â
Notice that no all the processes have command line arguments and that is ok.
Â
Taking the output from the powershell script above:
Â
[executable] [PID] [PPID] [COMMANDLINE]
Â
Write a program that reads this output from the standard input and outputs a process tree, the process tree can be drawn using drawing characters or ascii characters.
Â
- Bonus points (these are optional)
1. How to do this to explore process trees in remote computers? [easy]
2. Add flag to truncate command line to n number of characters as it can be too big [easy]
3. Add flag to show CPU usage instead of the command line [medium]
4. Output the information in an XML representation of your choice [medium]
5. Using Item 4. Apply an XSL transformation to show a tabular HTML representation of the tree [hard]
Â
-You are encouraged to google the terms that you do not understand-Part of the evaluation in the ability to dive into new issues-And also the understanding of non-trivial problems.You have a windows server that has cygwin installed and sshd enabled.You want to log in by using remote desktop to resolve an issue but usually the 3 licensedsessions that your company has purchased from Microsoft are being used by your colleagues.Then you ssh into the box, that means that you only have access to the command line and yourshell is the cygwin bash.These boxes have a full cygwin installation and that means you have perl, python, ruby, awk ,sed , etc ..You can visit the cygwin portal to check what all comes with cygwin.To trouble shoot the issue after careful consideration you realize that you need to create a toolthat can show a process tree similar to what is shown by pstree.Example:$ pstree -aplc?,1├─bash,6008└─cygrunsrv,516└─sshd,912 -D├─sshd,4672 -D -R├─sshd,5556 -D -R│ └─bash,4644│ └─screen,4840 -DR MAIN│ └─screen,3064 -DR MAIN│ └─bash,5676│ └─pstree,3892 -aplc└─sshd,2264 -D -RYou don’t need the flags you just want to output a tree using text or drawing characters and youwant to identify the hierarchy of processes using the process id and the parent process id.You are also aware that the cygwin pstree only work for processes started in a cygwin shell butyou want to have a similar tool that you can use for native windows processes.You have at your disposal powershell.And you know that you can invoke it from the cygwin shell like this:$ echo | powershell -ExecutionPolicy Unrestricted 'ps' | headHandles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName------- ------ ----- ----- ----- ------ -- -----------186 13 17856 10176 87 244 amswmagt187 8 7808 9528 433 0.09 4136 bash189 8 7872 9524 435 0.03 4604 bash185 9 9040 10920 435 1.79 5168 bash182 8 7812 9480 433 0.08 5376 bash
Attachments: