chrisluiz16

Untitled

Jul 29th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import ipcountry
  2.  
  3. host = input("Enter the ipaddress: ")
  4. result = ipcountry.get_country(ip = host)
  5. print(result)
  6.  
  7. ## Reading command line arguments
  8.  
  9. ! python3 ipsearch.py 2.2.2.2
  10.  
  11. ! cat ipsearch.py
  12.  
  13. import ipcountry
  14. import sys
  15.  
  16. host = sys.argv[1]
  17. result = ipcountry.get_country(ip = host)
  18. print(result)
  19.  
  20. import ipcountry
  21. import sys
  22.  
  23. host = sys.argv
  24. if len(host) == 2:
  25. arg = sys.argv[1]
  26. result = ipcountry.get_country(ip = arg)
  27. print(result)
  28. else:
  29. print('Please provide an Ip address')
  30.  
  31. ! python3 new.py 9.9.9.9
  32.  
  33.  
  34.  
  35. ## urls
  36.  
  37. import requests
  38.  
  39. url = [
  40. 'https://www.google.com',
  41. 'https://www.youtube.com',
  42. 'https://www.fsf.org',
  43. 'https://www.stackoverflow.com',
  44. 'https://www.gnu.org',
  45. 'https://www.kernel.org'
  46. ]
  47. for hostname in url:
  48. response = requests.get(url = hostname, timeout = 5)
  49. if response.status_code == 200:
  50. print('{} - is online'.format(hostname))
  51. else:
  52. print('{} - is down'.format(hostname))
  53.  
  54. import requests
  55.  
  56. url = [
  57. 'https://www.google.com',
  58. 'https://www.youtube.com',
  59. 'https://www.nosite.noite',
  60. 'https://www.fsf.org',
  61. 'https://www.stackoverflow.com',
  62. 'https://www.gnu.org',
  63. 'https://www.kernel.org'
  64. ]
  65. for hostname in url:
  66. try:
  67. response = requests.get(url = hostname, timeout = 5)
  68. if response.status_code == 200:
  69. print('{} - is online'.format(hostname))
  70. else:
  71. print('{} - is down'.format(hostname))
  72. except:
  73. pass
  74.  
  75. import requests
  76.  
  77. url = [
  78. 'https://www.google.com',
  79. 'https://www.youtube.com',
  80. 'https://www.nosite.noite',
  81. 'https://www.fsf.org',
  82. 'https://www.stackoverflow.com',
  83. 'https://www.gnu.org',
  84. 'https://www.kernel.org'
  85. ]
  86. for hostname in url:
  87. try:
  88. response = requests.get(url = hostname, timeout = 5)
  89. if response.status_code == 200:
  90. print('{} - is online'.format(hostname))
  91. else:
  92. print('{} - is down'.format(hostname))
  93. except:
  94. print('{} - Error'.format(hostname))
  95.  
  96. ## Running shell program
  97.  
  98. ##
  99. import subprocess
  100. result = subprocess.call('mkdir /tmp/mydir', shell = True)
  101. print(result)
  102.  
  103. ! ls -l /tmp/
  104.  
  105. ##
  106.  
  107. import subprocess
  108.  
  109. cp = input('Enter the file to copy to /tmp/mydir: ')
  110. cmd = "cp {} /tmp/mydir".format(cp)
  111. result = subprocess.call(cmd,shell = True)
  112. print(result)
  113.  
  114. ! ls -l /tmp/mydir/
  115.  
  116. ## if else vs if elif
  117.  
  118. s = 'a1b2c3@#$'
  119.  
  120. alpha_c = 0
  121. digit_c = 0
  122. special_c = 0
  123.  
  124. for item in s:
  125. if item.isalpha():
  126. alpha_c += 1
  127. if item.isdigit():
  128. digit_c += 1
  129. else:
  130. special_c += 1
  131. print('Alpha - {}, Digit - {}, Special chara - {}'.format(alpha_c,digit_c,special_c))
  132.  
  133. ###
  134.  
  135. s = 'a1b2c3@#$'
  136.  
  137. alpha_c = 0
  138. digit_c = 0
  139. special_c = 0
  140.  
  141. for item in s:
  142. if item.isalpha():
  143. alpha_c += 1
  144. elif item.isdigit():
  145. digit_c += 1
  146. else:
  147. special_c += 1
  148. print('Alpha - {}, Digit - {}, Special chara - {}'.format(alpha_c,digit_c,special_c))
  149.  
  150. ## While
  151.  
  152. s = 0
  153. number = 1
  154.  
  155. while number <= 10:
  156. print(number)
  157. number += 1
  158.  
  159.  
  160. s = 0
  161. number = 1
  162.  
  163. while number <= 10:
  164. s = s + number
  165. number += 1
  166. print(s)
  167.  
  168. ## sorting
  169.  
  170. number_l = [5,3,1,8,7,4,9]
  171. number_t = (5,3,1,8,7,4,9)
  172.  
  173. r = sorted(number_l)
  174. print(r)
  175.  
  176. r = sorted(number_t, reverse=True)
  177. print(r)
  178.  
  179. name = 'prince'
  180. print(sorted(name))
  181.  
  182. names = ['richard','prince','sam','kevin','jobin']
  183. print(sorted(names))
  184.  
  185. ## Sorting based on key length
  186.  
  187. names = ['richard','prince','sam','kevin','jobin']
  188. r = sorted(names,key = len)
  189. print(r)
  190.  
  191. ## Sorting based on salary
  192.  
  193. names = ['richard-25000','prince-20000','sam-40000','kevin-15000','jobin-50000']
  194.  
  195. def salary(item):
  196. return int(item.split('-')[-1])
  197.  
  198. r = sorted(names, key = salary)
  199. print(r)
  200.  
  201.  
  202. ##
  203.  
  204. person = [['sam',25],['prince',23],['kevin',30],['john',50]]
  205.  
  206. def age(x):
  207. return x[-1]
  208.  
  209. r = sorted(person, key = age)
  210. print(r)
  211.  
  212. ##
  213.  
  214. d = {'alex': 25, 'prince': 23, 'sam': 30,'kevin': 33}
  215. r = sorted(d.values())
  216. print(r)
  217.  
  218. ##
  219.  
  220. d = {'alex': 25, 'prince': 23, 'sam': 30,'kevin': 33}
  221.  
  222. def get_age(item):
  223. return item[-1]
  224. r = sorted(d.items(),key = get_age)
  225. print(r)
  226.  
  227. ##
  228.  
  229. access_log = "access.log"
  230. logfile = open(access_log,"r")
  231. ipcounter = {}
  232. for line in logfile:
  233. host = line.split()[0]
  234. if host not in ipcounter:
  235. ipcounter[host] = 1
  236. else:
  237. ipcounter[host] += 1
  238.  
  239. def ip_hit(item):
  240. return item[-1]
  241.  
  242. r = sorted(ipcounter.items(), key = ip_hit)
  243. print(r[-1])
  244.  
  245. print(r[-10:])
  246.  
  247. ##
  248.  
  249. for item in r[-10:]:
  250. ip,freq = item
  251. print("{:20} - {}".format(ip,freq))
  252.  
  253.  
Advertisement
Add Comment
Please, Sign In to add comment